所以我已经开始为一个项目制作我自己的游戏了,当你点击太空飞船射击后,当你点击鼠标它再次发射而没有按下任何东西时它会一直持续下去。我已经把我到目前为止的内容包括在内了
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.