1

I am trying to make a platformer and I am stumped about how to code it so that however long you hold the up arrow, it will only cause the y change to change once until you let go and repress it.(There will be double jump but I haven't gotten there yet. I haven't gotten to gravity either.) Here is my code:

import pygame
import time
import random
pygame.init()
display_width = 800
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
car_width = 64
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Platformer')
clock = pygame.time.Clock()
game_loop():
    score = 0
    x = (display_width * 0.45)
    y = (display_height * 0.8)
    y_change = -5
    x_change = 0
    gameExit = False
    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                elif event.key == pygame.K_RIGHT:
                    x_change = 5
                elif event.key == pygame.K_UP:
                    y_change = 25
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0
gameloop()
pygame.quit()
quit()

Can anybody help?

PokeBros
  • 51
  • 1
  • 2
  • 7
  • There is quite a bit of code missing here. It is not clear to me what you are trying to accomplish. – Stephen Rauch Jan 04 '17 at 03:11
  • I am not done yet. I am just wondering how to not continue to add 25 to y_change while holding the up arrow – PokeBros Jan 04 '17 at 03:12
  • As I read the code you will not keep adding 25. You will only add 25 on the event for keydown. If you want to add 25 many times, you will need some sort of periodic event that adds 25 at each time period. Then you need a state variable that marks the key state, and only has the 25 added while the key is down. – Stephen Rauch Jan 04 '17 at 03:18
  • When I tested it with my other game, it continuously adds 25. – PokeBros Jan 04 '17 at 03:19
  • use `if event.type == pygame.KEYUP:` to set `y_change = 0` - like you do with `x_change` – furas Jan 04 '17 at 05:40
  • 1
    if you what to add `25` (from `y_change`) only once then always set `y_change = 0` after `y += y_change` – furas Jan 04 '17 at 05:45
  • As @furas already said: you have to increment or decrement the players y and x position instead of setting them to a absolute value. –  Jan 05 '17 at 09:34

0 Answers0