我正在用 Pygame 写一个简单的玩具。当您按下主行上的一个键时,它会发出一点粒子。
class Particle():
x = 0
y = 0
size = 0
colour = (255, 255, 255)
rect = None
def __init__(self, x, y, size, colour):
self.x = x
self.y = y
self.size = size
self.colour = colour # Particle has its own colour
self.rect = pygame.Rect(self.x, self.y, self.size, self.size)
class Burst():
x = 0
y = 0
colour = (255, 255, 255)
count = 0
sound = None
particles = []
def __init__(self, x, y, colour, count, sound):
self.x = x
self.y = y
self.colour = colour # Burst has its own colour, too - all its particles should have the same colour as it
self.count = count
self.sound = sound
self.particles.append(Particle(self.x, self.y, 5, self.colour))
def update(self):
self.particles.append(Particle(random.randint(1, 30) + self.x, random.randint(1, 30) + self.y, 5, self.colour))
def draw(self):
global screen
for p in self.particles:
pygame.draw.rect(screen, p.colour, p.rect) # This draws the particles with the correct colours
#pygame.draw.rect(screen, self.colour, (60, 60, 120, 120), 4) # This draws the particles all the same colour
#screen.fill(p.colour, p.rect) # This draws the particles all the same colour
您要查找的线在 Burst.draw 中。出于某种原因,只有未注释的才能正常工作。另外两条线,据我所知应该是一样的,只是正确地绘制了第一个爆发的粒子。任何后续的爆发都会改变屏幕上的所有粒子以匹配它们的颜色。
我可以提供更多代码,但没有更多的。基本上按键将 Bursts 添加到一个数组中,并且我每一步通过该数组调用 update() 和 draw()。
有谁知道我做错了什么,然后不小心修复了?