# Player jumps multiple times

**URL:** <https://discourse.libsdl.org/t/player-jumps-multiple-times/44777>\
**Category:** SDL Development\
**Created:** [July 10, 2023, 3:12pm UTC](https://discourse.libsdl.org/t/player-jumps-multiple-times/44777 "2023-07-10T15:12:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lark\_sh](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/l/e99b99/32.png) [@Lark\_sh](https://discourse.libsdl.org/u/Lark_sh)\
**Post date:** [July 10, 2023, 3:12pm UTC](https://discourse.libsdl.org/t/player-jumps-multiple-times/44777/1 "2023-07-10T15:12:12Z")

</div>

I want the player to jump only once per key press but it jumps multiple times essentially making the player fly. I don’t want that.  
Here’s the Game.cpp:

```auto
#include "Game.h"

SDL_Window* gWindow = nullptr;
SDL_Renderer* gRenderer = nullptr;

// HORIZONTAL
float speedX = 2.0f;
float rt_runStr = 8.5f;
float lt_runStr = 8.5f;
float maxX = 15.5f;

// VERTICAL
float speedY = 0.0f;
float gravity = 2.2f;
float jumpStr = 14.2f;

// flags
int rightK = 0;
int leftK = 0;
int up = 0;
int collision = 1;

Entity* player = nullptr;

Entity* tile = nullptr;

void Game::init(const char* title ,int x, int y, int w, int h, int flags) {
	if (SDL_Init(SDL_INIT_EVERYTHING) == 0) {

		// create window
		gWindow = SDL_CreateWindow(title, x, y, w, h, flags);

		if (gWindow != NULL) {

			// create renderer
			gRenderer = SDL_CreateRenderer(gWindow, -1, 0);

			isRunning = true;
		}
		else {
			isRunning = false;
		}
	}
	else {
		isRunning = false;
	}
	player = new Entity;
	player->loadTexture("assets/doux_00.png");
	tile = new Entity;
}

void Game::handleEvent() {

	SDL_Event event;

	while (SDL_PollEvent(&event)) {

		switch (event.type) {

		case SDL_QUIT:
			isRunning = false;
			break;
		}
	}
}

void Game::update() {
	if (SDL_GetKeyboardState(0)[SDL_GetScancodeFromKey(SDLK_d)]) {
		rightK = 1;
	}
	else {
		rightK = 0;
	}

	if (SDL_GetKeyboardState(0)[SDL_GetScancodeFromKey(SDLK_a)]) {
		leftK = 1;
	}
	else {
		leftK = 0;
	}

	if (SDL_GetKeyboardState(0)[SDL_GetScancodeFromKey(SDLK_SPACE)]) {
		up = 1;
	}
	else {
		up = 0;
	}

	if (up == 1) {
		gravity = 0;
	}

	if (rightK == 1) {
		lt_runStr = 0.0f;
		rt_runStr += 0.6f;
	}

	if (leftK == 1) {
		rt_runStr = 0.0f;
		lt_runStr += 0.6f;
	}

	if (rt_runStr >= maxX) {
		rt_runStr = maxX;
		lt_runStr = 0.0f;
	}

	if (lt_runStr >= maxX) {
		lt_runStr = maxX;
		rt_runStr = 0.0f;
	}

	if (SDL_HasIntersection(player->getRect(), tile->getTile())) {
		collision = 0;
	}
	else {
		collision = 1;
	}

	if (collision == 0) {
		gravity = 0;
		jumpStr = 10.2f;
	}

	speedX += (rightK * rt_runStr) - (leftK * lt_runStr);
	gravity += 0.6f;
	speedY += (gravity * collision) - (up * jumpStr);

	player->setRect(250, 200);
	tile->setTile(250, 300, 150, 30);
}

void Game::render() {
	SDL_RenderClear(gRenderer);
	// ------------------------------------------
	
	player->drawTexture();
	tile->drawTile();

	// ------------------------------------------
	SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 1);
	SDL_RenderPresent(gRenderer);
}

void Game::clean() {
	SDL_DestroyWindow(gWindow);
	SDL_DestroyRenderer(gRenderer);
	SDL_DestroyTexture(player->getTexture());
	delete player;
	delete tile;
	SDL_Quit();
}

```

---

<div class="post-metadata">

**Author:** ![Daniel1985](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@Daniel1985](https://discourse.libsdl.org/u/Daniel1985)\
**Post date:** [July 10, 2023, 3:50pm UTC](https://discourse.libsdl.org/t/player-jumps-multiple-times/44777/2 "2023-07-10T15:50:45Z")

</div>

Add a boolean `isJumping` variable that is set when the player starts to jump. If the player tries to press space to jump, `up` will only be set to `1` if the player hasn’t already started a jump.

Note that you also need to set back `isJumping` to false whenever the player is standing on a tile.

I don’t know how your collision code works so this is just some example code:

```auto
if (SDL_GetKeyboardState(0)[SDL_GetScancodeFromKey(SDLK_SPACE)] && !isJumping) {
	up = 1;
	isJumping = true;
}

// Move player up with the jump strength etc
DoJump();

// Move player down again, with gravity
MovePlayerDown();

// Check collision between the player and the tiles
ResolveCollision();

if(PlayerIsStandingOnATile())
	isJumping = false;

```

---

<div class="post-metadata">

**Author:** ![Lark\_sh](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/l/e99b99/32.png) [@Lark\_sh](https://discourse.libsdl.org/u/Lark_sh)\
**Post date:** [July 10, 2023, 6:02pm UTC](https://discourse.libsdl.org/t/player-jumps-multiple-times/44777/3 "2023-07-10T18:02:03Z")

</div>

Thanks I’ll make the changes!

---

<div class="post-metadata">

**Author:** ![Lark\_sh](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/l/e99b99/32.png) [@Lark\_sh](https://discourse.libsdl.org/u/Lark_sh)\
**Post date:** [July 10, 2023, 6:06pm UTC](https://discourse.libsdl.org/t/player-jumps-multiple-times/44777/4 "2023-07-10T18:06:32Z")

</div>

Could you please suggest sources to learn sdl collision. I have collision but it’s kinda shabby since, I came up with it. I’ve searched youtube but there’s good videos only on Pygame’s collision.

---

<div class="post-metadata">

**Author:** ![Daniel1985](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@Daniel1985](https://discourse.libsdl.org/u/Daniel1985)\
**Post date:** [July 10, 2023, 6:15pm UTC](https://discourse.libsdl.org/t/player-jumps-multiple-times/44777/5 "2023-07-10T18:15:54Z")

</div>

Lazyfoo’s website is, in my opinion, a good source to learn SDL: [Lazy Foo' Productions - Beginning Game Programming v2.0](https://lazyfoo.net/tutorials/SDL/)

In the case of collision detection, this specific tutorial is a good start:

> **[Lazy Foo' Productions - Collision Detection](https://lazyfoo.net/tutorials/SDL/27_collision_detection/index.php)**
>
> Check for collision SDL2.

---

<div class="post-metadata">

**Author:** ![Lark\_sh](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/l/e99b99/32.png) [@Lark\_sh](https://discourse.libsdl.org/u/Lark_sh)\
**Post date:** [July 10, 2023, 7:42pm UTC](https://discourse.libsdl.org/t/player-jumps-multiple-times/44777/6 "2023-07-10T19:42:47Z")

</div>

Thank you I’ll check it out.

---

<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:** [July 11, 2023, 5:16am UTC](https://discourse.libsdl.org/t/player-jumps-multiple-times/44777/7 "2023-07-11T05:16:16Z")

</div>

> [@Lark\_sh](#):
>
> Could you please suggest sources to learn sdl collision. […] I’ve searched youtube but there’s good videos only on Pygame’s collision.

I don’t think it is necessary to limit yourself to only material that explains Collision detection/handling in combination with SDL. The collision detection itself is just math and works the same everywhere.

You’ve used SDL\_HasIntersection, nothing wrong with that, but it’s not always enough. You might want to do collision detection between other shapes than rectangles, or only in a certain direction, or you might want to know how much things are overlapping so that you can move them apart, etc. There might not always be a library function that you can use so don’t be afraid of writing code yourself if necessary to get what you want.

---

<div class="post-metadata">

**Author:** ![Lark\_sh](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/l/e99b99/32.png) [@Lark\_sh](https://discourse.libsdl.org/u/Lark_sh)\
**Post date:** [July 13, 2023, 1:49pm UTC](https://discourse.libsdl.org/t/player-jumps-multiple-times/44777/8 "2023-07-13T13:49:46Z")

</div>

Sure will write my own func() s
