pygame key hold down

Started by
6 comments, last by doctorsixstring 16 years, 8 months ago
I'm using pygame, and I can't seem to figure this out. I want the user to be able to move an object on the screen, well I've tried.

for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keepGoing = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_DOWN:
                box_y += 5
            if event.key == pygame.K_UP:
                box_y += -5
            if event.key == pygame.K_LEFT:
                box_x += -5
            if event.key == pygame.K_RIGHT:
                box_x += 5
but you have to press the button repeatedly to move the box. How do I make it so that you can hold the button down and it continue to move? Thanks, Joe [Edited by - avgprogramingjoe on August 5, 2007 9:47:35 PM]
Visit my general programming site to get help, tutorials, as well as get experience by joining projects or making your own - My Site
Advertisement
I think you would use a while? (I'm new so this is probably BS.)
I'm using a for statement, which gets the job done just as well. I think I'm just doing it wrong all together. Theres probably a good method or function that helps with it, but I just can't seem to figure it out or find it.
Visit my general programming site to get help, tutorials, as well as get experience by joining projects or making your own - My Site
pygame.key.get_pressed() will return a tuple containing the current state of every key:

keystate = pygame.key.get_pressed()if keystate[K_DOWN]:    box_y += 5if keystate[K_UP]:    box_y -= 5if keystate[K_LEFT]:    box_x -= 5#etc
for event in pygame.event.get():        if event.type == pygame.QUIT:            keepGoing = False        keystate = pygame.key.get_pressed()        if keystate[K_UP]:            box_y -= 5        if keystate[K_DOWN]:            box_y += 5        if keystate[K_LEFT]:            box_x -= 5        if keystate[K_RIGHT]:            box_x += 5

did not work at all
    for event in pygame.event.get():        if event.type == pygame.QUIT:            keepGoing = False        if event.type == pygame.KEYDOWN:            keystate = pygame.key.get_pressed()            if keystate[K_UP]:                box_y -= 5            if keystate[K_DOWN]:                box_y += 5            if keystate[K_LEFT]:                box_x -= 5            if keystate[K_RIGHT]:                box_x += 5

That gave me an error after I made one of the keydowns.

Anybody know whats wrong?
Thanks,

Joe
Visit my general programming site to get help, tutorials, as well as get experience by joining projects or making your own - My Site
Key states have nothing to do with the event queue - you are polling the states of every key for every event in your queue. You need to poll their states outside of your event-handling code.
How would I do it out of the event loop?
I figured out also that since it is a tuple you can't do
if keystate[K_UP]:    box_y -=5

it's got to be:
if keystate[273]:    box_y -=5

now what I have done is made a while loop that totally updates everything
    for event in pygame.event.get():        if event.type == pygame.QUIT:            keepGoing = False        if event.type == pygame.KEYDOWN:            keystate = pygame.key.get_pressed()            while keystate[273]:                pygame.event.get()                box_y -= 5                keystate = pygame.key.get_pressed()                screen.blit(background, (0,0))                screen.blit(bar, pos)                screen.blit(label, (50,50))                screen.blit(mouse_pos, (200, 200))                screen.blit(box, (box_x, box_y))                pygame.display.flip()            while keystate[274]:                pygame.event.get()                box_y += 5                keystate = pygame.key.get_pressed()                screen.blit(background, (0,0))                screen.blit(bar, pos)                screen.blit(label, (50,50))                screen.blit(mouse_pos, (200, 200))                screen.blit(box, (box_x, box_y))                pygame.display.flip()            while keystate[276]:                pygame.event.get()                box_x -= 5                keystate = pygame.key.get_pressed()                screen.blit(background, (0,0))                screen.blit(bar, pos)                screen.blit(label, (50,50))                screen.blit(mouse_pos, (200, 200))                screen.blit(box, (box_x, box_y))                pygame.display.flip()            while keystate[275]:                pygame.event.get()                box_x += 5                keystate = pygame.key.get_pressed()                screen.blit(background, (0,0))                screen.blit(bar, pos)                screen.blit(label, (50,50))                screen.blit(mouse_pos, (200, 200))                screen.blit(box, (box_x, box_y))                pygame.display.flip()

How can I do it so that it isn't in a crappy while loop like I made.

Thanks,


Joe
Visit my general programming site to get help, tutorials, as well as get experience by joining projects or making your own - My Site
Quote:Original post by avgprogramingjoe
How would I do it out of the event loop?


Keep using the event loop for some events, like WM_QUIT, but add another section of code before or after that for checking the state of the buttons.

Quote:Original post by avgprogramingjoe
I figured out also that since it is a tuple you can't do
if keystate[K_UP]:    box_y -=5

it's got to be:
if keystate[273]:    box_y -=5



You need to import all objects from pygame.locals ("from pygame.locals import *") for "K_UP" to work. Or, you could avoid polluting your namespace by importing the module ("import pygame.locals") and using "pygame.locals.K_UP", instead of just "K_UP".

Does that make sense?

- Mike

This topic is closed to new replies.

Advertisement