努力是绘制在不同速度的速度在屏幕上移动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()
没问题!希望你的第二天编码变得更好! – ashbygeek