I want to make my sprite jump like in mario bros.
Is this code a good start?
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;
}
}
}
}
}
}