|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import pygame
pygame.init()
WIN_WIDTH = 400
WIN_HEIGHT = 600
window = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption("动画")
window.fill((255, 255, 255))
pygame.display.flip()
y = 100
pygame.draw.circle(window, (255, 0, 0), (100, y), 50)
pygame.display.update()
num = 1
running = True # 添加一个标志位,用于控制循环是否继续进行
while running:
num += 1
if num % 10 == 0:
pygame.draw.circle(window, (255, 255, 255), (100, y), 50)
pygame.draw.circle(window, (255, 0, 0), (100, y), 50)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False # 用户点击关闭按钮时,停止循环
pygame.quit()
为什么还是没有效果
if num % 10 == 0:
pygame.draw.circle(window, (255, 255, 255), (100, y), 50)
pygame.draw.circle(window, (255, 0, 0), (100, y), 50)
pygame.display.update()
相当于先画一个白圆,再画一个红圆盖住它,最后刷新屏幕。
这样得到的圆肯定是红色的,就不会有动画效果
将两处画红圆的代码pygame.draw.circle(window, (255, 0, 0), (100, y), 50)移到循环体开头就可以解决
此外,为了获得一个好的效果,建议尽量调低一下帧率 import pygame
pygame.init()
WIN_WIDTH = 400
WIN_HEIGHT = 600
window = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption("动画")
window.fill((255, 255, 255))
pygame.display.flip()
y = 100
pygame.display.update()
num = 1
running = True # 添加一个标志位,用于控制循环是否继续进行
clock = pygame.time.Clock()
while running:
pygame.draw.circle(window, (255, 0, 0), (100, y), 50)
num += 1
if num % 10 == 0:
pygame.draw.circle(window, (255, 255, 255), (100, y), 50)
pygame.display.update()
clock.tick(5)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False # 用户点击关闭按钮时,停止循环
pygame.quit()
|
|