Player jumps multiple times

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:

#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();
}

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:

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;

Thanks I’ll make the changes!

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.

Lazyfoo’s website is, in my opinion, a good source to learn SDL: Lazy Foo' Productions - Beginning Game Programming v2.0

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

Thank you I’ll check it out.

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.

Sure will write my own func() s