Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    User input problem

    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.

    Code:
        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))
                    a+=1
                    b-=1
                    if a>1050:
                        a=120
                    if b<120:
                        b=1050
                    if event.key == K_r:
                        screen.blit(unionvic, (400,100))
                    if event.key == K_q:
                      pygame.event.post(pygame.event.Event(QUIT))
                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.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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.

    Code:
    userInput = ""
    
    while userInput != "r" or userInput !="q":
         performAnimation()
         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.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •