0

When trying to create a double I either can jump infinitely or jump once, struggling to find a solution to only jump once.

I was able to create a double jump using two different keys however I want it to where I only have to use the 'W' key.

here's my code:

import pygame

pygame.init()

class Fighter:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.rect = pygame.Rect((self.x, self.y, 70, 200))
        self.x_vel = 0
        self.y_vel = 0
        self.jump = False

    def draw_player(self, surface):
        pygame.draw.rect(surface, "Red", self.rect)

    def detect_collisions(self, platforms):
        collisions = []
        for platform in platforms:
            if self.rect.colliderect(platform.rect):
                collisions.append(platform)
        return collisions

    def movement(self, player, platforms, screen_height, screen_width):
        self.x = 0
        self.y = 0
        self.y_vel = 0
        pixel_move = 30
        gravity = 5

        # get key presses
        key = pygame.key.get_pressed()

        # player one inputs
        if player == 1:
            if key[pygame.K_a]:
                self.x = -pixel_move
            if key[pygame.K_d]:
                self.x = pixel_move
            if key[pygame.K_w] and self.jump == False:
                self.y_vel = -pixel_move
                self.jump = True
            if key[pygame.K_s]:
                self.y_vel = pixel_move
        # player two inputs
        elif player == 2:
            if key[pygame.K_LEFT]:
                self.x = -pixel_move
            if key[pygame.K_RIGHT]:
                self.x = pixel_move

        # apply gravity
        self.y_vel += gravity

        # makes sure the player actually moves
        self.rect.x += self.x
        self.rect.y += self.y_vel

        # make sure player stays onto the screen
        self.platform_interaction(platforms)

        # make sure the player doesn't fall off the screen
        if self.rect.bottom + self.y_vel > screen_height:
            self.y_vel = 0
            self.rect.bottom = screen_height
            self.jump = False
        # make sure the player doesn't go above the screen
        if self.rect.top < 0:
            self.rect.top = 0
        # make sure the player doesn't go across the right side of the screen
        if self.rect.right > screen_width:
            self.rect.right = screen_width
        # makes sure the player doesn't go across the left side of the screen
        if self.rect.left <= 0:
            self.rect.left = 0

    def platform_interaction(self, platforms):
        collisions = self.detect_collisions(platforms)

        for platform in collisions:
            # player lands on top off platform
            if abs(self.rect.bottom - platform.rect.top) < 10:
                self.rect.bottom = platform.rect.top
                self.jump = False
            # player collides with the sides of the platform
            elif abs(self.rect.right - platform.rect.left) < 20:
                self.rect.right = platform.rect.left
            elif abs(self.rect.left - platform.rect.right) < 20:
                self.rect.left = platform.rect.right
            elif platform.state:
                # player hits themselves on bottom of the platform
                if abs(self.rect.top + self.y_vel) < platform.rect.bottom:
                    self.rect.top = platform.rect.bottom
                # make sure player doesn't fall through solid platforms
                elif abs(self.rect.bottom + self.y_vel) > platform.rect.top:
                    self.rect.bottom = platform.rect.top
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • You can use an additional variable (`num_jumps_in_air`) to count how many times your player jumps and reset it to 0 every time it is on the ground. That variable can then be used to enable/disable jumping. – Tristan Dec 19 '22 at 13:15

0 Answers0