如何在屏幕上从左到右为对象设置动画而不拖尾?

问题描述

我怎样才能让我的绘制云功能在我的屏幕上只从左到右移动一次而不创建轨迹?我尝试将我的 for 循环移动到我的 while 循环中,但没有奏效。

import pygame
pygame.init()
size = (640,480)

win = pygame.display.set_mode(size)

width = win.get_width()
height = win.get_height()

blue = (135,255,255)

def draw_cloud(x,y,size):
  pygame.draw.circle(win,(255,255),(x,y),int(size* .5))
  pygame.draw.circle(win,(int(x + size * .5),int(size * .6))
  pygame.draw.circle(win,(x + size,int(y - size * .1)),int(size * .4))

pygame.draw.rect(win,(0,160,3),400,640,80))
pygame.draw.rect(win,(135,400))

for i in range(60,600,100):
    draw_cloud(i,120,80)
    pygame.display.update()

running = True
myClock = pygame.time.Clock()

while running:
   for event in pygame.event.get():
     if event.type == pygame.QUIT:
       running = False
  
   myClock.tick(60)

pygame.quit()

解决方法

您已在应用程序循环中连续绘制整个场景,并且必须使用 fill() 清除背景:

import pygame
pygame.init()
size = (640,480)

win = pygame.display.set_mode(size)
myClock = pygame.time.Clock()

width = win.get_width()
height = win.get_height()
blue = (135,255,255)

def draw_cloud(x,y,size):
  pygame.draw.circle(win,(255,255),(x,y),int(size* .5))
  pygame.draw.circle(win,(int(x + size * .5),int(size * .6))
  pygame.draw.circle(win,(x + size,int(y - size * .1)),int(size * .4))

x = 640
running = True
while running:
   for event in pygame.event.get():
     if event.type == pygame.QUIT:
       running = False

   win.fill(0)
   pygame.draw.rect(win,(0,160,3),400,640,80))
   pygame.draw.rect(win,(135,400))  
   draw_cloud(x,120,80)
   pygame.display.update()
   
   x -= 1
   if x < -120:
       x = 640

   myClock.tick(60)

pygame.quit()

典型的 PyGame 应用程序循环必须: