2017-01-19 120 views
0

努力是绘制在不同速度的速度在屏幕上移动rects [气泡]的群集。当谈到渲染动画片时,我遇到了障碍。动画pygame反向移动屏幕

到目前为止,我已经去过的方式是使用我的Bubble类的类实例填充列表。从那里我遍历实例列表并调用它们的blow_bubble方法,有效地在屏幕上的不同位置绘制每个气泡,并使用自己的值为其移动速度初始化每个气泡。然后将这些气泡附加到名为“绘制”的单独列表中,以表示它们确实已绘制(尽管未呈现)。

接下来的部分就是颠簸的地方。

我有一个while循环,它接受绘制列表的长度大于零作为其运行条件。按照这篇文章的形式:http://programarcadegames.com/index.php?chapter=introduction_to_animation 屏幕表面设置为填充循环的开始,在它的结尾,我通过pygame.display.flip()更新了屏幕表面。在while循环的中间,我遍历绘制列表中的类实例,并通过实例的归因移动速率递减表示它们的y值的属性。

不知何故,这是行不通的。

我检查过,以确保y值实际递减;在print语句中,输出是预期的结果:y值下降到负数。但是这些交换仍然是静态的。

一如既往,任何见解都非常感谢。

#colors, screen, clock, etc defined above but omitted 
pygame.init() 

class Bubble(): 
    def __init__(self, screenheight, screenwidth): 
     self.w = self.h = 0 
     self.x, self.y = 0, 0 
     self.moverate = 0 

    def reset_bubble(self): 
     self.w = self.h = random.randrange(2, int(screenwidth*1/4)) 
     self.x = random.randrange(0, (screenwidth-self.w)) 
     self.y = screenheight-self.w 
     self.moverate = 1 

    def blow_bubble(self): 
     self.reset_bubble() 
     pygame.draw.rect(screen, WHITE, (self.x, self.y, self.w, self.h), 10) 

bubbles = [] 
drawn = [] 
i = 0 

for i in range(10):   #creates list of bubble objects 
     bubble = Bubble(screenheight, screenwidth) 
     bubbles.append(bubble) 

for i in range(len(bubbles)):  #draws bubbles without rendering them 
    bubbles[i].blow_bubble() 
    drawn.append(bubbles[i])  #appends objects to a new list (drawn) 

while len(drawn) > 0: 
    screen.fill((BLACK)) 
    drawn[i].y -= drawn[i].moverate  #moves the bubble up the screen 
    pygame.display.flip()    #updates the screen 

    if i >= 0: #counts up til len(drawn) then counts down [then up again] 
     i+=1 #to make sure we move every bubble a little each iteration 
    if i ==len(drawn): 
     i-= 1 
clock.tick(FPS) 
pygame.quit() 

回答

0

除非我非常误解pygame的工作方式,否则您误解了它是如何工作的。对于oygamr来说,它需要一个“渲染循环”,一个代码循环,您可以在其中重复移动对象,使用pygame.draw.rect等东西绘制它,然后使用pygame.display.flip翻转显示。你一直这样做直到你完成动画。

所以,你需要做一堆改变。

  • 你“吹泡”功能需要不复位气泡的位置,只是增加自己的立场,并呼吁pygame.draw.rect
  • 你并不需要一个“曙光”名单,仅仅是第一泡泡列表。在你的while循环中,你需要遍历每一个调用“吹泡泡”的泡泡列表。
  • 你需要在while循环中移动'pygame.display.flip',但是在所有的气泡都被吹掉之后。

您的索引超出范围错误是因为在for循环退出后,我没有神奇地重置它,它离开了我,就像for循环结束时一样。

+0

没问题!希望你的第二天编码变得更好! – ashbygeek