Controling Sprite with keys and mouse

Controling Sprite with keys and mouse

Postby ac.smith » Wed Jul 23, 2014 7:54 am

Hi,

I have been following a book and created a small fruit shooting game using keyboard inputs to shoot fruit falling from the sky. I now want to make some changes to the code so the user can also use the mouse as well as the keyboard to move the gun. My code is as follows:

Code: Select all
while game_over == False:
     for event in pygame.event.get():
          if event.type==pygame.KEYDOWN:
               if event.key==pygame.K_ESCAPE:
                    game_over=True
               elif event.key==pygame.K_LEFT:
                    turret.update_position("left",WINDOWWIDTH)
               elif event.key==pygame.K_RIGHT:
                    turret.update_position("right",WINDOWWIDTH)
               elif event.key==pygame.K_SPACE:
                    bullet=Bullet(turret.get_gun_position())
                    shot.play()
                    bullet_sprites.add(bullet)
          else:
               mpos=pygame.mouse.get_pos()
               turret.mouse_position(mpos,WINDOWWIDTH)
-------------------------------------------------------------------------------------

class Turret(pygame.spriet.Sprite):
     def __init__(self,WINDOWWIDTH,WINDOWHEIGHT):
          ect
          ect


     def update_position(self,direction,WINDOWWIDTH):
          if direction=="left" and self.rect.x>10:
               self.rect.x-=10
          if direction=="right" and self.rect.x<(WINDOWWIDTH-10):
               self.rect.x+=10

     def get_gun_position
          ect
          ect

     def mouse_position(self,mpos,WINDOWWIDTH):
          self.rect.x=mpos[0]


My problem is that when I run the code the mouse works fine and if I press the left or right arrow key the turret will move but as soon as I stop pressing the arrow key the turret returns to where the mouse last was. Can someone please provide me with a solution as it is driving me crazy!

Thanks

Adam
ac.smith
 
Posts: 7
Joined: Fri Jul 11, 2014 10:34 am

Re: Controling Sprite with keys and mouse

Postby DrakeMagi » Wed Jul 23, 2014 11:50 am

Using else to control mouse in event loop is causing it to go back to mouse position.
As soon as you left the arrow key. pygame.KEYUP event is sent and the else take care of it.
Which is your mouse control.
Use mousebuttondown event.
Code: Select all
for event in pygame.event.get():
    if event.type==pygame.KEYDOWN:
        if event.key==pygame.K_ESCAPE:
            game_over=True
        elif event.key==pygame.K_LEFT:
            turret.update_position("left",WINDOWWIDTH)
        elif event.key==pygame.K_RIGHT:
            turret.update_position("right",WINDOWWIDTH)
        elif event.key==pygame.K_SPACE:
            bullet=Bullet(turret.get_gun_position())
            shot.play()
            bullet_sprites.add(bullet)
    elif event.type == pygame.MOUSEBUTTONDOWN:
        if event.button == 1: # left mouse button
            turret.mouse_position(event.pos,WINDOWWIDTH)
Linux: won't find windows here.
Linux: the choice of a GNU generation.
https://github.com/DrakeMagi
DrakeMagi
 
Posts: 106
Joined: Sun May 12, 2013 8:36 pm


Return to Game Development

Who is online

Users browsing this forum: No registered users and 3 guests