1
我創建了一個模擬盧瑟福散射實驗的物理模擬。我試圖每次循環運行時創建一個alpha粒子(精靈),我可以創建它(顯示在屏幕上),但它不會向前移動,而如果我只創建一個粒子,它可以正常工作。PyGame循環與每個循環中的精靈創建不更新
我已經將精靈的類和循環附加到它創建的位置。
循環:
while running:
clock.tick(20)
allparticles = pygame.sprite.Group(Particle(speed, bwidthmin, bwidthmax, background))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
allparticles.clear(screen, background)
nucgroup.draw(screen)
allparticles.update()
allparticles.draw(screen)
pygame.display.flip()
Sprite類:
class Particle(pygame.sprite.Sprite):
def __init__(self, speed, bwidthmin, bwidthmax, background):
pygame.sprite.Sprite.__init__(self)
self.background = background
self.image = pygame.Surface((16,16))
self.rect = self.image.get_rect()
currenty = random.randint(bwidthmin,bwidthmax)
self.rect.centery = currenty
self.rect.centerx = 0
pygame.draw.circle(self.image, yellow, (8,8), 5)
self.dx=speed
self.dy = 0
def update(self):
c1 = (self.rect.centerx,self.rect.centery)
self.rect.centerx += self.dx
if self.rect.right >= 570:
pygame.sprite.Sprite.kill(self)
pygame.draw.line(self.background, white, c1, (self.rect.centerx,self.rect.centery), 1)
我要去哪裏錯了?
我也有我的tkinter窗口的問題,這個pygame嵌入掛起(按鈕不按下,標籤不變,在pygame停止之前不能做任何事情)。循環是否永久運行導致這種情況發生?我希望能夠在運行時更新變量以影響仿真,或者這是不可能的?
感謝您的幫助。
感謝您的幫助和它的工作很大。另一個問題是如何去除粒子一旦到達終點時留下的線?我不介意它消失,但我希望它消失。 –