精灵元素未更新
我正在尝试以彩色单元格的平均方式编程动画,在每次迭代中,其中几个交换了各自的颜色。但是,我不太明白为什么,代码会生成单元格的网格,但没有任何运动。它只是保持静态。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将
update_rect
更改为update
之后,您需要在循环中添加sprites_group.draw(window(window)
> sprites_group.update()和pygame.display.flip()
)之前,该程序可以正常工作。精灵正在改变颜色,但您只会在脚本的开头绘制一次,因此它们保持相同的颜色。
You need to change the
update_rect
toupdate
after that you need need to putsprites_group.draw(window)
inside the loop (after thesprites_group.update()
and before thepygame.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.
首先,您必须在每一帧中重新磨练。您必须在应用程序循环中调用
sprites_group.draw(window(window)
:它不足以更改颜色属性。您还必须调用
update_rect
。因为您必须用新颜色填充图像(self.image.fill(self.cell.color)
):但是,请参见
pygame.sprite.group.group.update.update
:我建议将方法从
update_Rect
重命名为update
:因此,调用
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: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)
):However, see
pygame.sprite.Group.update
:I suggest renaming the method from
update_rect
toupdate
:So it is sufficient to call
sprites_group.update()
: