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にあります。何らかの理由で、コメントされていないものだけが正しく機能します。他の2つの線は、私が知る限り同じであるはずですが、最初のバーストの粒子を正しく描画するだけです。その後のバーストでは、画面上のすべてのパーティクルが色に一致するように変更されます。
より多くのコードを提供できますが、それ以上のものはありません。基本的に、キーを押すとバーストが配列に追加され、ティックごとにupdate()とdraw()を呼び出してその配列をステップスルーします。
誰かが私が間違ったことを知っていて、それから誤って修正しましたか?