PyGame: player not jumping

Viewed 62

I am trying to get my player to jump when I press the up key. I have created a jump method in the Player class that gets called in the game loop. When user presses the UP key, value of player.isJump should go from its default value of False, to True. and the code inside player.jump() should run until the jump is completed and player.isJump is set back to False. But when I press the up key, nothing happens.

import pygame

pygame.init()
screen = pygame.display.set_mode([800, 600])
pygame.display.set_caption('Stick Quest')


class Player():
    def __init__(self):
        self.playerImg = pygame.image.load('player.png')
        self.playerX = 400
        self.playerY = 300
        self.playerX_change = 0
        self.isJump = False
        self.jumpCount = 10

    def displayPlayer(self):
        screen.blit(self.playerImg, (self.playerX,self.playerY))

    def updateLocation(self):
        self.playerX += self.playerX_change

    def jump(self):
        if self.isJump:
            if self.jumpCount >= -10:
                neg = 1
                if player.jumpCount < 0:
                    neg = -1
                self.playerY -= (self.jumpCount ** 2) * 0.5 * neg
                self.jumpCount = -1
            else:
                self.isJump = False
                self.jumpCount = 10


player = Player()
running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player.playerX_change = -1.0
            if event.key == pygame.K_RIGHT:
                player.playerX_change = 1.0
            if event.key == pygame.K_UP:
                player.isJump == True
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                player.playerX_change = 0


    player.updateLocation()
    player.jump()
    player.displayPlayer()
    pygame.display.update()
    screen.fill((255,255,255))

pygame.quit()
1 Answers

Your problem is in this line:

player.isJump == True

instead of assign the player.isJump to True, you just checked if it is true or not. nothing changed. To fix it use the assignment sign:

player.isJump = True
Related