python可以利用pygame库实现动画制作,以下是一个基本操作,演示一个模拟飞机(长方形)移动,基本涵盖动画制作的要素。
import pygame import sys # 初始化pygame pygame.init() # 设置屏幕大小 screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) # 设置标题 pygame.display.set_caption('飞机动画') # 飞机的颜色 plane_color = (255, 0, 0) # 红色 # 飞机的初始位置 plane_x = 100 plane_y = screen_height // 2 # 飞机的移动速度 plane_speed = 5 # 游戏主循环 running = True while running: # 检查事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 填充背景色 screen.fill((0, 128, 0)) # 更新飞机位置 plane_x += plane_speed if plane_x > screen_width: plane_x = -50 # 飞机飞出屏幕后重新从左侧进入 # 绘制飞机 pygame.draw.rect(screen, plane_color, (plane_x, plane_y, 50, 20)) # 飞机尺寸 # 更新屏幕显示 pygame.display.flip() # 控制游戏帧率 pygame.time.Clock().tick(60) # 退出pygame pygame.quit() sys.exit()
深入学习可以参考pygame 社区网页, https://www.pygame.org/contribute.html