2015-05-15 34 views
2

,所以我已經開始做我自己的遊戲項目後,當你點擊它再次閃光沒有我按下任何鼠標飛船拍攝,它永遠繼續下去。我已經包括了我有這麼遠低於的MOUSEBUTTONDOWN命令激活本身沒有我點擊

from livewires import games 
import pygame 
games.init(screen_width = 840, screen_height = 480, fps = 50) 

class Spaceship(games.Sprite): 
    """Creates the Spaceship""" 

    ship_image = games.load_image("spaceship.bmp") 

    def __init__(self): 

     super(Spaceship, self).__init__(image = Spaceship.ship_image, 
             x = games.mouse.x, 
             bottom = games.screen.height) 
    def update(self): 
     """ Move to mouse x position. """ 
     self.x = games.mouse.x 

     if self.left < 0: 
      self.left = 0 

     if self.right > games.screen.width: 
      self.right = games.screen.width 
     #Makes the laser spawn on the spaceship 
     for event in pygame.event.get(): 
      if event.type == pygame.MOUSEBUTTONDOWN: 
       new_laser = Laser(self.x, self.y) 
       games.screen.add(new_laser) 

class Laser(games.Sprite): 
    """Creates the laser""" 
    laser_image = games.load_image("laser1.png") 

    def __init__(self,spaceship_x, spaceship_y): 


     super(Laser, self).__init__(image = Laser.laser_image, 
            x = spaceship_x, y = spaceship_y, 
            dy = -6) 


#class enemy(game.Sprite): 


def main(): 
    """Runs the game""" 
    background = games.load_image("featured-space-policy.jpg", transparent = False) 
    games.screen.background = background 

    ship = Spaceship() 
    games.screen.add(ship) 
    games.mouse.is_visible = False 
    games.screen.mainloop() 


main() 
+0

你需要有其它監聽設置爲當鼠標按鈕釋放,然後停止射擊。 –

回答

0

試想一下,

既然你設置了一個事件偵聽MOUSEBUTTONDOWN,所以這將觸發激光功能,但怎麼會停下來?

所以,你需要建立另一個偵聽MOUSEBUTTONUP將停止激光的發射,是這樣的:

for event in pygame.event.get(): 
      if event.type == pygame.MOUSEBUTTONDOWN: 
       new_laser = Laser(self.x, self.y) 
       games.screen.add(new_laser) 
      else event.type == pygame.MOUSEBUTTONUP: 
       remove_laser()#something like that, I'm not sure if it's the right method.