I am coding a game in Python / Pygame. Currently I am working on player movement but I have realised that if I hold the space bar(i.e The button to activate my jump movement) it keeps going up and never comes down. I have implemented gravity but it only takes place when I press the button once. I want this to be so that I can only jump once and my character doesnt keep flying. Here's my code (Please Help!)
# This is my player file
import pygame
class Player(pygame.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.image = pygame.Surface((32,64))
self.image.fill('red')
self.rect = self.image.get_rect(topleft = pos)
# Player Movement
self.direction = pygame.math.Vector2(0, 0)
self.speed = 7
self.gravity = 0.7
self.jump_speed = -14
# player status
self.status = 'idle'
self.facing_right = True
self.on_ground = False
self.on_ceiling = False
self.on_left = False
self.on_right = False
# Key Pressed
def get_input(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
self.direction.x = 1
elif keys[pygame.K_LEFT]:
self.direction.x = -1
else:
self.direction.x = 0
if keys[pygame.K_SPACE]:
self.jump()
# Gravity
def apply_gravity(self):
self.direction.y += self.gravity
self.rect.y += self.direction.y
# Jumping
def jump(self):
self.direction.y = self.jump_speed
# Updating The Player
def update(self):
self.get_input()
self.apply_gravity()