Python 动画制作

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

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值