I have come across a problem in my code for my game. Currently i am developing a 2D platformer game in pygame. I have gotten the character to move left and right, although i haven't been able to get him to jump. I have experimented with a number of possibilities that all haven't worked.
while True: #handles user input for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == KEYDOWN: if (event.key == K_LEFT or event.key == K_a) and player.direction != RIGHT: #if the left key is pressed player.direction = LEFT #change the direction to left player.playerPicture = player.runLeft player.playerPicture = player.playerPicture.convert_alpha() DISPLAYSURF.blit(player.playerPicture, (player.playerx, player.playery)) elif (event.key == K_RIGHT or event.key == K_d) and player.direction != LEFT: #if the right key is pressed player.direction = RIGHT #change the directions to right player.playerPicture = player.runRight player.playerPicture = player.playerPicture.convert_alpha() DISPLAYSURF.blit(player.playerPicture, (player.playerx, player.playery)) elif (event.key == K_UP or event.key == K_w) and player.direction != LEFT and player.direction != RIGHT: player.direction = UP player.playerPicture = player.jump player.playerPicture = player.playerPicture.convert_alpha() DISPLAYSURF.blit(player.playerPicture, (player.playerx, player.playery)) elif event.key == K_ESCAPE: #if the escape key is pressed terminate() #quit the game else: player.playerPicture = player.standingStill player.playerPicture = player.playerPicture.convert_alpha() player.direction = None DISPLAYSURF.fill(Black) #update game state if player.direction == LEFT: #if the players position is greater than the left boundary if (player.playerx > boundaryLeft): player.playerx -= 8 #move the player to the left #if the direction is right elif player.direction == RIGHT: if (player.playerx < boundaryRight): #player is less than the right boundary player.playerx += 8 #move the player to the right elif player.direction == UP:
This code is in the mainloop of the game. It pretty much checks what key has been pressed, applies the appropriate image for the character and changes the direction. Afterwards the character is manipulated based on the direction that he is facing. If he is facing right, he will walk right etc.
class Player(pygame.sprite.Sprite): def __init__(self): #constructor pygame.sprite.Sprite.__init__(self) #constructor for the sprite self.playerPicture = pygame.image.load('JumpLeft.png') self.runRight = pygame.image.load('RunRight2.png') self.runLeft = pygame.image.load('RunLeft2.png') self.jump = pygame.image.load('Jump.png') self.playerPicture = self.playerPicture.convert_alpha() self.rect = self.playerPicture.get_rect() self.standingStill = pygame.image.load('JumpLeft.png') self.playerx = windowWidth / 2 self.playery = windowHeight - 200 self.direction = None self.onGround = False self.isJumping = False; self.gravity = 1.2 self.velocity = 0 self.rect.center = (self.playerx, self.playery)
Current attempts: So far i've tried
if direction == 'UP': player.playery -= 2
which the character pretty much just continuously moves up until the user releases the space button