How can I disable my player from jumping multiple times in a row? Pygame

Viewed 179

Hello so I am making a platform and have most things setup (movement and collision) but I need to make it so when my player is jumping he has to wait until he hits the ground again to jump (I want to remove double jumps)

#load images
bg_img = pygame.image.load('img/background1.jpg')
bg_img = pygame.transform.scale(bg_img, (1000,1000))
rect = bg_img.get_rect()
#class for player
class Player():
    def __init__(self, x, y):
        self.images_right = []
        self.images_left = []
        self.index = 0
        self.counter = 0
        for num in range(1,6):
            img_right = pygame.image.load(f'img/guy{num}.png')
            img_right = pygame.transform.scale(img_right, (40 , 80))
            img_left = pygame.transform.flip(img_right,True,False) #flips right image on the x axis {true} and not y axis {false}
            self.images_right.append(img_right)
            self.images_left.append(img_left)
        self.image = self.images_right[self.index]
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.width = self.image.get_width()
        self.height = self.image.get_height()
        self.vel_y = 0
        self.jumped = False
        self.direction = 0

    def update(self):
        dx = 0
        dy = 0
        walk_cooldown = 5

        #get keypresses
        key = pygame.key.get_pressed()
        if key[pygame.K_SPACE] and self.jumped == False:
            self.vel_y = -15
            self.jumped = True
        if key[pygame.K_SPACE] == False:
            self.jumped = False
        if key[pygame.K_LEFT]:
            dx -= 5
            self.counter += 1
            self.direction = -1
        if key[pygame.K_RIGHT]:
            dx += 5
            self.counter += 1
            self.direction = 1
        if key[pygame.K_LEFT] == False and key[pygame.K_RIGHT] == False:
            self.counter = 0
            self.index = 0
            if self.direction == 1:
                self.image = self.images_right[self.index]
            if self.direction == -1:
                self.image = self.images_left[self.index]



        #animation
        if self.counter > walk_cooldown:
            self.counter = 0
            self.index += 1
            if self.index >= len(self.images_right):
                self.index = 0
            if self.direction == 1:
                self.image = self.images_right[self.index]
            if self.direction == -1:
                self.image = self.images_left[self.index]



        #add gravity
        self.vel_y += 1
        if self.vel_y > 10:
            self.vel_y = 10
        dy += self.vel_y

        #check for collision

        for tile in world.tile_list:
            #x direction collision
            if tile[1].colliderect(self.rect.x + dx, self.rect.y, self.width, self.height):
                dx=0




            #y direction collision
            if tile[1].colliderect(self.rect.x, self.rect.y + dy, self.width, self.height):
                #check if below ground (jumping)
                if self.vel_y <0:
                    dy = tile[1].bottom - self.rect.top
                    self.vel_y = 0
                #check if above ground(falling)
                elif self.vel_y >= 0:
                    dy = tile[1].top - self.rect.bottom

        #update player coordinates
        self.rect.x += dx
        self.rect.y += dy

above is the code I think is important for this question, if you need different code let me know.

Im relatively new so anything helps thanks!

1 Answers

The issue is caused by

if key[pygame.K_SPACE] == False:
   self.jumped = False

This code set self.jumped = False immediately when SPACE is relased and this enables the next jump.

Remove this 2 lines of code, but set self.jumped = False when the palyer collides with the ground:

class Player():
    # [...]

    def update(self):
        # [...]

        key = pygame.key.get_pressed()
        if key[pygame.K_SPACE] and self.jumped == False:
            self.vel_y = -15
            self.jumped = True

        # if key[pygame.K_SPACE] == False:    
        #     self.jumped = False                        # <--- DELETE

        # [...]

        #check for collision
        for tile in world.tile_list:
            # [...]

            # y direction collision
            if tile[1].colliderect(self.rect.x, self.rect.y + dy, self.width, self.height):
                #check if below ground (jumping)
                if self.vel_y < 0:
                    dy = tile[1].bottom - self.rect.top
                    self.vel_y = 0
             
                #check if above ground(falling)
                elif self.vel_y >= 0:
                    dy = tile[1].top - self.rect.bottom

                    self.jumped = False                  # <--- INSERT
       
Related