【问题标题】:Sprite elements not being updatedSprite 元素未更新
【发布时间】:2022-06-19 02:20:14
【问题描述】:

我正在尝试在 PYGAME 中对彩色单元格的动画进行编程,在每次迭代中,它们中的几个会交换各自的颜色。但是,我不太明白为什么,代码生成了单元格网格,但没有任何形式的移动。它只是保持静止。

import random
import pygame
import sys

colors = [(77,2,6),(180,5,180),(140,180,11)]

class CELL () :
    def __init__ (self,x,y,ide):
        self.color = random.choice(colors)
        self.ide = ide
        self.x = x
        self.y = y

n, m = 10, 10
block_width, block_height = 50, 50
win_width, win_height = block_width*m, block_height*n
# Matrix : 10x10
# Cell size : 50x50
# Window size : 500x500

cells = {}
for i in range(n):
    for j in range(m):
        cells[i*n+j] = CELL(i,j,i*n+j)
# We create a cells dictionary in which keys are the 
# ID attribute of each cell object stored.

class sprite_CELL (pygame.sprite.Sprite):
    def __init__ (self,cell_obj):
        pygame.sprite.Sprite.__init__(self)
        self.cell = cell_obj
        # Represents one of the cells.
        self.image = pygame.Surface([block_width,block_height])
        self.update_rect()
    def update_rect (self):
        self.image.fill(self.cell.color)
        self.rect = self.image.get_rect()
        # We update the rect according to the current color for the linked cell.
        self.rect.topleft = [self.cell.x*block_width, self.cell.y*block_height]

sprites_group = pygame.sprite.Group()
for cell in cells.values():
    sprites_group.add(sprite_CELL(cell))

# So far we have created a group of sprites containing sprites whose method
# "("update_rect" updates their graphical representation depending on the
# color of the cell to which the sprite is linked.

# API

pygame.init()
clock = pygame.time.Clock()
window = pygame.display.set_mode((win_width, win_height))

sprites_group.draw(window)

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    rand_cell_a = random.choice(list(cells.values()))
    rand_cell_b = random.choice(list(cells.values()))
    # We randomly choose two cells.
    color_a = cells[rand_cell_a.ide].color
    color_b = cells[rand_cell_b.ide].color
    # We extract the colors and...
    cells[rand_cell_a.ide].color = color_b
    cells[rand_cell_b.ide].color = color_a
    # Swap the colors for the cells.

    sprites_group.update()
    # Updating the sprites should update their linked
    # colors too.

    pygame.display.flip()
    clock.tick(60)
    # And that's done iteratively.

如果有人能帮我解决这个问题,我将不胜感激。

【问题讨论】:

  • 改变颜色属性是不够的。您还必须致电update_rect。因为你必须用新颜色(self.image.fill(self.cell.color))填充图像。

标签: python pygame sprite pygame-surface


【解决方案1】:

您需要将update_rect 更改为update,然后您需要将sprites_group.draw(window) 放入循环中(在sprites_group.update() 之后和pygame.display.flip() 之前),程序就可以正常工作了。 精灵正在改变颜色,但您只在脚本开头绘制了一次,因此它们保持相同的颜色。

【讨论】:

  • 您是否测试过再次复制原始代码并且只在正确的位置添加sprites_group.draw(window) 行以查看它是否有效?我只这样做了,它对我和@YagoMj 都有效。也许您将类定义中的函数名称从 update 更改为 update_rect ,这就是原因。很高兴我能帮上忙。
  • 现在我明白了,我不明白问题被修改了,对不起。
【解决方案2】:

首先,您必须在每一帧中重新绘制研磨。您必须在应用程序循环中调用sprites_group.draw(window)

while True:
    # [...]

    sprites_group.draw(window)           # <--- draw grid
    pygame.display.flip()
    clock.tick(60)

改变颜色属性是不够的。您还必须致电update_rect。因为你必须用新颜色(self.image.fill(self.cell.color))填充图像:

cells[rand_cell_a.ide].color = color_b
cells[rand_cell_b.ide].color = color_a
for s in sprites_group:
    s.update_rect() 

但是,请参阅pygame.sprite.Group.update:

对组中的所有 Sprite 调用 update() 方法。

我建议将方法从update_rect 重命名为update

class sprite_CELL (pygame.sprite.Sprite):
    def __init__ (self,cell_obj):
        pygame.sprite.Sprite.__init__(self)
        self.cell = cell_obj
        # Represents one of the cells.
        self.image = pygame.Surface([block_width,block_height])
        self.update()

    def update(self):
        self.image.fill(self.cell.color)
        self.rect = self.image.get_rect()
        # We update the rect according to the current color for the linked cell.
        self.rect.topleft = [self.cell.x*block_width, self.cell.y*block_height]

所以调用sprites_group.update()就足够了:

cells[rand_cell_a.ide].color = color_b
cells[rand_cell_b.ide].color = color_a
sprites_group.update()

【讨论】:

  • 在我最初发布的代码中,我已经使用了您提到的 pygame.sprite.Group() 对象,将它们分组并在所有它们上调用 update() 方法。不管怎样,不管更新精灵的方法的名称是什么(你会看到我已经在我附加的代码中更改了),动画仍然不起作用。彩色网格出现在屏幕上,但它们仍然不动。你对此有什么猜测吗?
  • @YagoMj 你必须在应用程序循环中调用sprites_group.draw(window)。 (答案已更新)
猜你喜欢
  • 1970-01-01
  • 2018-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多