View Full Version : User input problem

01-12-2012, 03:48 AM
It is my first game and I need to know how to, for lack of a better word, pause the program but still run my animations and only stop them and continue the program when it receives use input.

for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_g:
screen.blit(btlgrd, (0,0))
screen.blit(bluect, (30,441))
screen.blit(greyct, (1042,441))
screen.blit(bulet1, (a,502))
screen.blit(bulet2, (b,502))
if a>1050:
if b<120:
if event.key == K_r:
screen.blit(unionvic, (400,100))
if event.key == K_q:
if event.key == K_i:
screen.blit(info, (0,0))

When I run it and press "g" it does as shown BUT I don't know how to make it wait until either "r" or "q" is pressed. Thank you and yes I am a noob.

01-12-2012, 06:32 AM
You would have to run your animation code inside a sentinel type loop such as a while loop. The sentinel condition being that you received the correct user input which would cause the loop to exit and continue on. This probably won't work exactly as you are expecting because Python is after all a scripting language and executes the commands in the script line by line. You might be able to achieve something more like you would want if you involved threading in which case you could perform multiple tasks simultaneously but threading can be difficult to get right.

userInput = ""

while userInput != "r" or userInput !="q":
userInput = lower(raw_input("Enter letter: "))

This setup would run the animation once per iteration of the loop and at the end of the loop ask the user for input. Until they enter r or q it will continue to loop.