0

Hi I'm working on a very basic game for a summer course I'm taking and I've recently discovered a bug that does not allow me to move and shoot at the same time. Here is the event handling code:

for event in pygame.event.get():
        #create quit event
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        #checks for keyup events to stop movement
        if event.type == KEYUP:
            if event.key == pygame.K_w:
                #stops player moving up
                player.yVelocity += 8
            if event.key == pygame.K_d:
                #stops player moving right
                player.xVelocity -= 8
            if event.key == pygame.K_s:
                #stops player moving down
                player.yVelocity -= 8
            if event.key == pygame.K_a:
                #stops player moving left
                player.xVelocity += 8

        #checks for key presses
        if event.type == KEYDOWN:
            if event.key == pygame.K_w:
                #sets variable to move player up
                player.yVelocity -= 8
            if event.key == pygame.K_d:
                #sets variable to move player right
                player.xVelocity += 8
            if event.key == pygame.K_s:
                #sets variable to move player down
                player.yVelocity += 8
            if event.key == pygame.K_a:
                #sets variable to move player left
                player.xVelocity -= 8
            if event.key == pygame.K_ESCAPE:
                #calls gameInit() to take player back to main menu
                mainGame.gameInit()

    #checks for clicks
    if (pygame.mouse.get_pressed())[0] == 1:
        #tells the game it should be firing
        print("bam")
        firing = True
    if (pygame.mouse.get_pressed())[1] == 1:
        #fires secondary ability
        player.useAbility((0,0))
    if (pygame.mouse.get_pressed())[0] == 0:
            firing = False

    if firing == True:
        mainGame.bulletList = player.shoot(pygame.mouse.get_pos(), mainGame.bulletList)

Sorry if it's a mess I'm fairly new to all this. However, when I put a test print statement inside the bit that detects mouse clicks, I found that it wouldn't take mouse input while a key was being pressed. I have no idea as to the cause of this.

furas
  • 119,752
  • 10
  • 94
  • 135
  • BTW: you can use `if/elif` when you check events and keys. – furas Jul 12 '14 at 04:51
  • You can use `event.type == MOUSEBUTTONDOWN` to check mouse buttons. See [events in documentation](http://www.pygame.org/docs/ref/event.html) – furas Jul 12 '14 at 04:53
  • 1
    See [pygame.mouse.get_pressed](http://www.pygame.org/docs/ref/mouse.html#pygame.mouse.get_pressed) - `"Note, remember to call pygame.event.get() before this function. Otherwise it will not work."` – furas Jul 12 '14 at 04:58
  • As I know `for event in pygame.event.get()` check all events (even mouse events) and remove from list of events so maybe `pygame.mouse.get_pressed()` has nothing to check. – furas Jul 12 '14 at 05:01
  • no the mous.get_pressed() function works fine as is it only stops working when im pressing keys and clicking at the same time, and wont resume working for at least 30 ticks after i stop pressing a key – VengefulLimaBean Jul 12 '14 at 23:42
  • Well, add `print` to see what value are in variables and which `if` is executed. – furas Jul 12 '14 at 23:44
  • like i said i tried that and i found that the get_Pressed function didnt return any keypresses while i was moving with the arrow keys – VengefulLimaBean Jul 13 '14 at 00:38
  • What system do you use - Mac/Win/Linux ? – furas Jul 13 '14 at 01:08
  • windows. why would that be a problem? – VengefulLimaBean Jul 13 '14 at 02:36

0 Answers0