我正在尝试使这个名为Button
的精灵可点击。在玩家到达目标后出现,点击后,精灵应该为新地图消失。单击精灵后我也会这样做,新级别将被加载,但是现在让我们保持完成点击精灵并因此而发生一些事情。
这是感兴趣的代码:
class Button(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites, game.buttons
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = pg.Surface((450, 335))
self.image = game.alert_img
self.rect = self.image.get_rect()
self.x = x
self.y = y
self._layer = 2
self.rect.x = x * TILESIZE
self.rect.y = y * TILESIZE
def events(self):
# catch all events here
for event in pg.event.get():
if event.type == pg.QUIT:
self.quit()
if event.type == pg.KEYDOWN:
if event.key == pg.K_ESCAPE:
self.quit()
if event.key == pg.K_LEFT:
self.player.move(dx=-1)
if event.key == pg.K_RIGHT:
self.player.move(dx=1)
if event.key == pg.K_UP:
self.player.move(dy=-1)
if event.key == pg.K_DOWN:
self.player.move(dy=1)
if event.type == pg.USEREVENT + 1:
self.text_object.kill()
self.text_object = Text((1760, 570), self.player.actions, self.font)
self.all_sprites.add(self.text_object)
Button(self, self.player.x -1.5, self.player.y - 2.4)
请注意,事件是在主要类中,而类按钮,很明显,不是。我认为def事件不是按钮类。
修改 两个下来的工作都在下面工作,但skrx他的反应更适合我的个人项目,根据你想要完成的事情,我的解决方案可能更适合你。我建议仔细研究两者。
答案 0 :(得分:1)
因此,我没有让用户点击精灵,而是制作了一个名为continue_game
的函数,其中游戏停止并等待用户按下空格键以继续游戏,我有在运行空间后没有实质的代码运行,但是kill确实删除了我想要它的精灵,确保我做到了我想做的事。
def continue_game(self):
user_continue = False
while not user_continue:
for event in pg.event.get():
if event.type == pg.QUIT:
self.quit()
if event.type == pg.KEYDOWN:
if event.key == pg.K_ESCAPE:
self.quit()
if event.key == pg.K_SPACE:
self.button.kill()
user_continue = True
前面提到的userevent现在看起来像这样:
if event.type == pg.USEREVENT + 1:
self.text_object.kill()
self.text_object = Text((1760, 570), self.player.actions, self.font)
self.all_sprites.add(self.text_object)
self.button = Button(self, self.player.x -1.5, self.player.y - 2.4)
self.draw()
self.continue_game()
答案 1 :(得分:1)
我为Game
类提供self.button = None
属性,并在用户到达目标时分配按钮实例。要检查鼠标是否与按钮发生碰撞,您可以使用event.pos
事件的pg.MOUSEBUTTONDOWN
并应用相机。我必须为Camera
课程提供一个apply_mouse
方法,因为event.pos
只是一个元组,我需要负相机位置。
# In the `Game` class.
def events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.quit()
elif event.type == pg.MOUSEBUTTONDOWN:
if self.button is not None:
mouse = self.camera.apply_mouse(event.pos)
if self.button.rect.collidepoint(mouse):
print('Clicked!')
# Remove button from sprite groups and
# set self.button to None again.
button.kill()
self.button = None
# KEYDOWN events omitted.
elif event.type == pg.USEREVENT + 1:
self.text_object.kill()
self.text_object = Text((1760, 570), self.player.actions, self.font)
self.all_sprites.add(self.text_object)
# Check `if self.button is None` so that we don't add
# several buttons to the groups.
if self.button is None:
self.button = Button(self, 1060, 670)
class Button(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites, game.buttons
pg.sprite.Sprite.__init__(self, self.groups)
self.image = pg.Surface((450, 335))
self.image.fill((0, 40, 200))
self.rect = self.image.get_rect(topleft=(x, y))
self._layer = 6
class Camera:
# I just added this method to the Camera class.
def apply_mouse(self, pos):
return (pos[0]-self.camera.x, pos[1]-self.camera.y)