【问题标题】:The MOUSEBUTTONDOWN command activates itself without me clickingMOUSEBUTTONDOWN 命令无需我点击即可自行激活
【发布时间】:2015-05-15 17:37:42
【问题描述】:

所以我已经开始为一个项目制作我自己的游戏,当你点击鼠标时,宇宙飞船射击后它会再次发射,而我没有按下任何东西,它会永远持续下去。我已经在下面列出了到目前为止的内容

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()

【问题讨论】:

  • 您需要设置另一个监听器,用于释放鼠标按钮,然后停止触发。

标签: python pygame


【解决方案1】:

想一想,

由于你为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.

【讨论】:

    猜你喜欢
    • 2016-12-14
    • 2015-06-28
    • 2019-06-02
    • 1970-01-01
    • 2011-07-05
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    相关资源
    最近更新 更多