Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使pygame精灵在python中消失_Python_Pygame - Fatal编程技术网

使pygame精灵在python中消失

使pygame精灵在python中消失,python,pygame,Python,Pygame,我正在做一个rpg游戏,我希望我的playbutton一按下就消失。有什么方法可以让我做到这一点吗? 我有不同的游戏状态:游戏,菜单,开始 游戏按钮将出现在开始游戏状态,我希望它在按下或游戏状态改变时消失。 感谢您的贡献如果按钮是真正的精灵,您可以: 将精灵添加到名为Buttons的组中 渲染屏幕上的按钮 使用kill()方法从按钮组中删除精灵 在下一轮重新渲染屏幕 要从屏幕上删除某些内容,需要在其上绘制其他内容。因此,最基本的答案是停止渲染按钮,开始在其上渲染其他内容 一个很好的方法是让所

我正在做一个rpg游戏,我希望我的playbutton一按下就消失。有什么方法可以让我做到这一点吗? 我有不同的游戏状态:游戏,菜单,开始 游戏按钮将出现在开始游戏状态,我希望它在按下或游戏状态改变时消失。
感谢您的贡献

如果按钮是真正的精灵,您可以:

  • 将精灵添加到名为Buttons的组中
  • 渲染屏幕上的按钮
  • 使用kill()方法从按钮组中删除精灵
  • 在下一轮重新渲染屏幕

要从屏幕上删除某些内容,需要在其上绘制其他内容。因此,最基本的答案是停止渲染按钮,开始在其上渲染其他内容

一个很好的方法是让所有可见对象继承并放入其中。从这里,您可以轻松绘制、更新和删除精灵

这里有一个有效的例子。按1、2或3键,使“按钮”再次出现:

import pygame
pygame.init()

screen = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()


class Button(pygame.sprite.Sprite):
    def __init__(self, pos, size=(32, 32), image=None):
        super(Button, self).__init__()
        if image is None:
            self.rect = pygame.Rect(pos, size)
            self.image = pygame.Surface(size)
        else:
            self.image = image
            self.rect = image.get_rect(topleft=pos)
        self.pressed = False

    def update(self):
        mouse_pos = pygame.mouse.get_pos()
        mouse_clicked = pygame.mouse.get_pressed()[0]
        if self.rect.collidepoint(*mouse_pos) and mouse_clicked:
            print("BUTTON PRESSED!")
            self.kill()  # Will remove itself from all pygame groups.

image = pygame.Surface((100, 40))
image.fill((255, 0, 0))
buttons = pygame.sprite.Group()
buttons.add(
    Button(pos=(50, 25), image=image),
    Button(pos=(50, 75), image=image),
    Button(pos=(50, 125), image=image)
)

while True:
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_1:
                buttons.add(Button(pos=(50, 25), image=image))
            elif event.key == pygame.K_2:
                buttons.add(Button(pos=(50, 75), image=image))
            elif event.key == pygame.K_3:
                buttons.add(Button(pos=(50, 125), image=image))

    buttons.update()  # Calls the update method on every sprite in the group.

    screen.fill((0, 0, 0))
    buttons.draw(screen)  # Draws all sprites to the given Surface.
    pygame.display.update()

在tkinter中,我会将start.unxyz()放在start click处理程序中,其中xyz是用来显示它的位置、包或网格。pygame必须为精灵提供类似的消失方法。再次搜索文档。@TerryJanReedy在pygame中没有任何等价物。一旦有东西被绘制到屏幕上,它将在程序的其余部分出现,除非与其他东西一起绘制。@TedKleinBergman tk geometry方法将一个小部件放入小部件列表中,而un方法将其从列表中删除。这似乎相当于pygame group.add和sprite.kill方法。然而,物理屏幕重画是不同的,在这里你是对的。Tk与操作系统窗口管理器配合使用,用户代码不直接涉及“抽离”自动发生。Pygame需要用户明确的屏幕绘制:更灵活,更负责。