如何使这个精灵可点击然后关闭它以便渲染新地图?
How do I make this sprite clickable and then close it after so a new map can be rendered?
我正在尝试让这个名为 Button
的 sprite 可点击。它会在玩家到达目标后出现,点击它后精灵应该会在新地图上消失。我也会在点击精灵后加载新关卡,但现在让我们继续完成点击精灵并因此发生一些事情。
这是感兴趣的代码:
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)
请注意,事件在主 class 中,而 class 按钮显然不是。我的意思是 def 事件不在按钮 class.
中
编辑
下面的两个答案都有效,但是他的回答更适合我的个人项目,我的解决方案可能更适合你,这取决于你想要完成的事情。我建议将两者都看一遍。
所以我没有让用户点击精灵,而是创建了一个名为 continue_game
的函数,在该函数中游戏停止并等待用户按下 space 栏游戏继续,我没有实质性的代码是 运行 在 space 是 运行 之后,但是 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
之前提到的用户事件现在看起来像这样:
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()
我会给 Game
class 一个 self.button = None
属性,并在用户达到目标时分配按钮实例。要检查鼠标是否与按钮发生碰撞,可以使用 pg.MOUSEBUTTONDOWN
事件的 event.pos
并应用相机。我不得不给 Camera
class 一个 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)
我正在尝试让这个名为 Button
的 sprite 可点击。它会在玩家到达目标后出现,点击它后精灵应该会在新地图上消失。我也会在点击精灵后加载新关卡,但现在让我们继续完成点击精灵并因此发生一些事情。
这是感兴趣的代码:
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)
请注意,事件在主 class 中,而 class 按钮显然不是。我的意思是 def 事件不在按钮 class.
中编辑 下面的两个答案都有效,但是他的回答更适合我的个人项目,我的解决方案可能更适合你,这取决于你想要完成的事情。我建议将两者都看一遍。
所以我没有让用户点击精灵,而是创建了一个名为 continue_game
的函数,在该函数中游戏停止并等待用户按下 space 栏游戏继续,我没有实质性的代码是 运行 在 space 是 运行 之后,但是 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
之前提到的用户事件现在看起来像这样:
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()
我会给 Game
class 一个 self.button = None
属性,并在用户达到目标时分配按钮实例。要检查鼠标是否与按钮发生碰撞,可以使用 pg.MOUSEBUTTONDOWN
事件的 event.pos
并应用相机。我不得不给 Camera
class 一个 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)