Jump to content
  • Advertisement
Sign in to follow this  
avgprogramingjoe

pygame key hold down

This topic is 4003 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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 += 5
if keystate[K_UP]:
box_y -= 5
if keystate[K_LEFT]:
box_x -= 5
#etc

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!