# jumping sprite

**URL:** <https://discourse.libsdl.org/t/jumping-sprite/59652>\
**Category:** Game Development\
**Created:** [May 10, 2025, 11:19pm UTC](https://discourse.libsdl.org/t/jumping-sprite/59652 "2025-05-10T23:19:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pbivens85](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/85e7bf/32.png) [@pbivens85](https://discourse.libsdl.org/u/pbivens85)\
**Post date:** [May 10, 2025, 11:19pm UTC](https://discourse.libsdl.org/t/jumping-sprite/59652/1 "2025-05-10T23:19:05Z")

</div>

I am trying to get a sprite to jump like in mario bros,  
Character player;  
player.yVelocity = 0;  
player.isJumping = false;  
player.x = 0;  
player.y = 0;  
//Hack to get window to stay up  
bool quit = false;  
while (quit == false) {  
//Apply the image  
SDL\_Rect rect;  
rect.x = 462 + move\_sprite;  
rect.y = 686 + player.y;  
SDL\_BlitSurface(gHelloWorld\_one, &rects[frame], gScreenSurface, &rect);  
frame++;  
if (frame == 10)  
{  
frame = 0;  
}  
SDL\_UpdateWindowSurface(gWindow);  
SDL\_Event event;  
while (SDL\_PollEvent(&event)) {  
if (event.type == SDL\_KEYDOWN)  
{  
Uint8 const\* keys = SDL\_GetKeyboardState(nullptr);  
if (keys[SDL\_SCANCODE\_ESCAPE] == 1)  
{  
quit = true;  
}  
if (keys[SDL\_SCANCODE\_SPACE] == 1 && !player.isJumping)  
{  
player.isJumping = true;  
player.yVelocity = JUMP\_VELOCITY;  
//quit = true;  
}  
if (keys[SDL\_SCANCODE\_LEFT] == 1)  
{  
move\_sprite–;  
}  
if (keys[SDL\_SCANCODE\_RIGHT] == 1)  
{  
move\_sprite++;  
}  
}  
if (player.isJumping)  
{  
player.yVelocity += GRAVITY;  
player.y += player.yVelocity;  
if (player.y \>= 400)  
{  
player.y = 400;  
player.yVelocity = 0;  
player.isJumping = false;  
}

```
				}
			}
		}
	}
}

```

here is my code so far it jumps straight up but leaves pixels behind of the sprite

---

<div class="post-metadata">

**Author:** ![Sward](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/sward/32/5719_2.png) [@Sward](https://discourse.libsdl.org/u/Sward)\
**Post date:** [May 11, 2025, 4:48am UTC](https://discourse.libsdl.org/t/jumping-sprite/59652/2 "2025-05-11T04:48:25Z")

</div>

Hello and welcome. Next time you post code, select it and hit “control E” on your keyboard, it will tell the website to wrap it in code formatting.

For your problem:  
Try drawing a large background image before drawing the sprite, or use [SDL\_FillRect](https://wiki.libsdl.org/SDL2/SDL_FillRect) to fill the window background with a solid color.  
Using either of these methods will clear the afterimage of the sprite.

---

<div class="post-metadata">

**Author:** ![pbivens85](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/85e7bf/32.png) [@pbivens85](https://discourse.libsdl.org/u/pbivens85)\
**Post date:** [May 12, 2025, 12:53am UTC](https://discourse.libsdl.org/t/jumping-sprite/59652/3 "2025-05-12T00:53:34Z")

</div>

```auto
				SDL_Rect rect;
				rect.x = 462 + move_sprite;
				rect.y = 686 + player.y;
				SDL_BlitSurface(gHelloWorld_one, &rects[frame], gScreenSurface, &rect);
				SDL_FillRect(gHelloWorld_one, &rect, 0x00);
				frame++;
well I tried this but I am still getting afterimages

```

---

<div class="post-metadata">

**Author:** ![Peter87](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/f07891/32.png) [@Peter87](https://discourse.libsdl.org/u/Peter87)\
**Post date:** [May 12, 2025, 7:59am UTC](https://discourse.libsdl.org/t/jumping-sprite/59652/4 "2025-05-12T07:59:24Z")

</div>

You forgot to initialize `rect.w` and `rect.h` before passing `&rect` to `SDL_FillRect`.

```auto
...
rect.w = rects[frame].w;
rect.h = rects[frame].h;
SDL_FillRect(gHelloWorld_one, &rect, 0x00);
...

```

It might be easier to just redraw everything each frame (the whole background, all the sprites, etc.), especially if you add more complicated backgrounds and sprites to your game. This might seem like it would be inefficient, which it can be if you continue to do “software rendering” (with `SDL_Surface`). It can often be made to perform good enough by implementing a “dirty rectangle system” (and use `SDL_UpdateWindowSurfaceRects` instead of `SDL_UpdateWindowSurface`) assuming there is not too much moving around all the time, but if you instead start using the [Render API](https://wiki.libsdl.org/SDL2/CategoryRender) (by using `SDL_Texture` instead of `SDL_Surface`, `SDL_RenderCopy` instead of `SDL_BlitSurface`, `SDL_RenderPresent` instead of `SDL_UpdateWindowSurface`, etc.) then you’ll take better advantage of your GPU (graphics card) so redrawing everything on the screen each frame should no longer be a performance problem (especially not for a simple 2D game). I believe most modern SDL games do it this way.

---

<div class="post-metadata">

**Author:** ![pbivens85](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/85e7bf/32.png) [@pbivens85](https://discourse.libsdl.org/u/pbivens85)\
**Post date:** [May 12, 2025, 7:38pm UTC](https://discourse.libsdl.org/t/jumping-sprite/59652/5 "2025-05-12T19:38:44Z")

</div>

Well I tried the new code but it still does not work

```auto
				SDL_Rect rect;
				rect.x = 462 + move_sprite;
				rect.y = 686 + player.y;
				rect.w = rects[frame].w;
				rect.h = rects[frame].h;
				SDL_FillRect(gScreenSurface, &rect, 0x000000);
				SDL_BlitSurface(gHelloWorld_one, &rects[frame], gScreenSurface, &rect);
				frame++;
				if (frame == 10)
				{
					frame = 0;
				}
				SDL_UpdateWindowSurface(gWindow);

```

---

<div class="post-metadata">

**Author:** ![Peter87](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/f07891/32.png) [@Peter87](https://discourse.libsdl.org/u/Peter87)\
**Post date:** [May 12, 2025, 8:18pm UTC](https://discourse.libsdl.org/t/jumping-sprite/59652/6 "2025-05-12T20:18:43Z")

</div>

Hmm, didn’t think about this earlier, but you actually want to fill the rect from the _previous_ iteration and not the current. And if you ever add more than one sprite you will have to do this at the start of the loop, before drawing any sprites, because you don’t want one sprite to accidentally draw the background on top of another sprite.

Consider simply redrawing the whole background once on each iteration by passing null for the rect.

```c
SDL_FillRect(gScreenSurface, NULL, 0);

```

---

<div class="post-metadata">

**Author:** ![pbivens85](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/85e7bf/32.png) [@pbivens85](https://discourse.libsdl.org/u/pbivens85)\
**Post date:** [May 12, 2025, 11:00pm UTC](https://discourse.libsdl.org/t/jumping-sprite/59652/7 "2025-05-12T23:00:11Z")

</div>

well I have figured out my jumping problem I just have to solve how to jump onto the upper platform and move there thanks for the help.
