1

I am trying to add the pygame KEYUP and KEYDOWN function into my controls for my pygame. I am having trouble inserting it in, would enjoy help with this simple problem. I have my main game loop and then a program wide if else statement, (if gun_select == True), which creates my starting screen. Much help appreciated!

# Loop as long as done == False
while not done:

    for event in pygame.event.get():  # User did something
        if event.type == pygame.QUIT:  # If user clicked close
            done = True  # Flag that we are done so we exit this loop


    # Clear the screen and set the screen background
    screen.fill(GREEN)

    if gun_select == True:
        #Controls
        if j == True:    
            keys = pygame.key.get_pressed()

        #log
        last_x1 = x1
        last_x2 = x2
        last_y1 = y1
        last_y2 = y2


        if keys[pygame.K_LEFT]:
            p1option -=1
            print p1option
        if keys[pygame.K_RIGHT]:
            p1option += 1
            print p1option
        if keys[pygame.K_a]:
            p2option -=1
        if keys[pygame.K_d]:
            p2option +=1
        if keys[pygame.K_r]:
            print "begin"
            gun_select = False
furas
  • 134,197
  • 12
  • 106
  • 148
mansa
  • 271
  • 1
  • 2
  • 6
  • edit question and use button `{}` to correctly format code. – furas Jan 16 '17 at 22:13
  • [simple template](https://github.com/furas/python-examples/blob/master/pygame/__templates__/1__simple__.py) which shows how to use `KEYDOWN` in `mainloop` and in class `Player` – furas Jan 16 '17 at 22:16
  • you have to use `KEYDOWN`/`KEYUP` inside `for event in pygame.event.get(): ` – furas Jan 16 '17 at 22:18
  • if you need more then one screen and every screen use own keys then maybe create separated `while` loop (`game event loop`) for evey screen: http://imgur.com/MT7tZ4s – furas Jan 16 '17 at 22:22
  • [Here](http://stackoverflow.com/documentation/pygame/5110/event-handling/18046/event-loop#t=201701170031385317961) is a basic walkthrough of the event loop. – Ted Klein Bergman Jan 17 '17 at 00:32

0 Answers0