精灵元素未更新

发布于 2025-02-04 18:55:34 字数 2442 浏览 2 评论 0原文

我正在尝试以彩色单元格的平均方式编程动画,在每次迭代中,其中几个交换了各自的颜色。但是,我不太明白为什么,代码会生成单元格的网格,但没有任何运动。它只是保持静态。

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.

如果有人能为我解决这个问题,我会非常感激。

I am trying to program an animation in PYGAME of colored cells that at each iteration a couple of them exchange their respective colors. However, I don't quite understand why, the code generates the grid of cells but there is no movement of any kind. It just stays static.

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.

I would be very grateful if anyone could solve this for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左岸枫 2025-02-11 18:55:34

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

You need to change the update_rect to update after that you need need to put sprites_group.draw(window) inside the loop (after the sprites_group.update() and before the pygame.display.flip()) and the program will work just fine.
The sprites were changing the color but you only draw them once in the beginning of the script so they kept the same color.

被翻牌 2025-02-11 18:55:34

首先,您必须在每一帧中重新磨练。您必须在应用程序循环中调用sprites_group.draw(window(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.group.update.update

在组中的所有精灵上调用 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()

First of all you have to redraw the grind in every frame. You have to call sprites_group.draw(window) in the application loop:

while True:
    # [...]

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

It is not enough to change the color attribute. You must also call update_rect. because you have to fill the image with the new color (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() 

However, see pygame.sprite.Group.update:

Calls the update() method on all Sprites in the Group.

I suggest renaming the method from update_rect to 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]

So it is sufficient to call sprites_group.update():

cells[rand_cell_a.ide].color = color_b
cells[rand_cell_b.ide].color = color_a
sprites_group.update()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文