代码之家  ›  专栏  ›  技术社区  ›  K. Groot

如何使此精灵可单击,然后关闭它,以便可以渲染新地图?

  •  2
  • K. Groot  · 技术社区  · 7 年前

    我想把这个精灵叫做 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事件不在button类中。

    两个人都在工作下面,但他的回答更适合我的个人项目,根据你想要完成的任务,我的解决方案可能更适合你。我建议把两者都看一遍。

    2 回复  |  直到 7 年前
        1
  •  1
  •   skrx    7 年前

    我会给 Game a类 self.button = None 属性并在用户达到目标时分配按钮实例。要检查鼠标是否与按钮发生碰撞,可以使用 event.pos pg.MOUSEBUTTONDOWN 事件并应用摄像机。我不得不给 Camera a类 apply_mouse 方法,因为 只是一个元组,我需要负摄像机位置。

    # 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)
    
        2
  •  1
  •   K. Groot    7 年前

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