pygame实现方块动画实例讲解
作者:我的天才女友 时间:2022-11-01 13:37:46
导入组件
首先导入需要的组件,pygame游戏组件,time是时间组件
import pygame, time, sys
from pygame.locals import *
绘制窗口
这里定义了一个480*600的屏幕,设置了窗口标题,并设置了屏幕的填充颜色为白色
# 屏幕的宽高
WIDTH = 480
HEIGHT = 600
# 颜色的常量
WHITE = (255, 255, 255)
surface = pygame.display.set_mode((WIDTH, HEIGHT), 0, 30)
pygame.display.set_caption("矩形动画")
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
surface.fill(WHITE)
pygame.display.update()
绘制矩形
RED = (255, 0, 0)
# 定义矩形字典变量
rect = {'rect': pygame.Rect(200, 200, 60, 46), 'color': RED}
---
surface.fill(WHITE)
pygame.draw.rect(surface, rect['color'], rect['rect'])
---
让矩形动起来
从运动来看只有中心向四方移动的四种可能。定义四个方向的常量。
# 方向常量
UPLEFT = 'upleft'
UPRIGHT = 'upright'
DOWNLEFT = 'downleft'
DOWNRIGHT = 'downright'
然后定义矩形的移动速度
# 矩形移动的速度
MOVESPEED = 2
给矩形设置一个初始的移动方向
# 定义矩形字典变量
rect = {'rect': pygame.Rect(200, 200, 60, 46), 'color': RED, 'direction': UPLEFT}
根据矩形的移动方向改变对应的起始位置。根据矩形不同的位置改变矩形的起始位置。
# 如果是上左,则x y 都减少
if rect['direction'] == UPLEFT:
rect['rect'].left -= MOVESPEED
rect['rect'].top -= MOVESPEED
# 如果是上右,则x 增加 y 减少
if rect['direction'] == UPRIGHT:
rect['rect'].left += MOVESPEED
rect['rect'].top -= MOVESPEED
# 如果是下左,则x 减少 y 增加
if rect['direction'] == DOWNLEFT:
rect['rect'].left -= MOVESPEED
rect['rect'].top += MOVESPEED
# 如果是下右,则x y 都增加
if rect['direction'] == DOWNRIGHT:
rect['rect'].left += MOVESPEED
rect['rect'].top += MOVESPEED
不断的改变矩形的位置,要注意防止移出窗口,当触及到边缘,沿来的方向将矩形改变方向即可。
# 如果上溢出窗口
if rect['rect'].top <= 0:
if rect['direction'] == UPLEFT:
rect['direction'] = DOWNLEFT
if rect['direction'] == UPRIGHT:
rect['direction'] = DOWNRIGHT
# 如果下溢出窗口
if rect['rect'].bottom >= HEIGHT:
if rect['direction'] == DOWNRIGHT:
rect['direction'] = UPRIGHT
if rect['direction'] == DOWNLEFT:
rect['direction'] = UPLEFT
# 如果右溢出窗口
if rect['rect'].right >= WIDTH:
if rect['direction'] == DOWNRIGHT:
rect['direction'] = DOWNLEFT
if rect['direction'] == UPRIGHT:
rect['direction'] = UPLEFT
# 如果右溢出窗口
if rect['rect'].left <= 0:
if rect['direction'] == DOWNLEFT:
rect['direction'] = DOWNRIGHT
if rect['direction'] == UPLEFT:
rect['direction'] = UPRIGHT
这样就实现了功能。每次循环让时间暂停一会即可。
这里附上所有代码
import pygame, time, sys
from pygame.locals import *
# 屏幕的宽高
WIDTH = 480
HEIGHT = 600
# 颜色的常量
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# 方向常量
UPLEFT = 'upleft'
UPRIGHT = 'upright'
DOWNLEFT = 'downleft'
DOWNRIGHT = 'downright'
# 矩形移动的速度
MOVESPEED = 2
# 定义矩形字典变量
rect = {'rect': pygame.Rect(200, 200, 60, 46), 'color': RED, 'direction': UPLEFT}
surface = pygame.display.set_mode((WIDTH, HEIGHT), 0, 30)
pygame.display.set_caption("矩形动画")
while True:
print(rect)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
surface.fill(WHITE)
# 如果是上左,则x y 都减少
if rect['direction'] == UPLEFT:
rect['rect'].left -= MOVESPEED
rect['rect'].top -= MOVESPEED
# 如果是上右,则x 增加 y 减少
if rect['direction'] == UPRIGHT:
rect['rect'].left += MOVESPEED
rect['rect'].top -= MOVESPEED
# 如果是下左,则x 减少 y 增加
if rect['direction'] == DOWNLEFT:
rect['rect'].left -= MOVESPEED
rect['rect'].top += MOVESPEED
# 如果是下右,则x y 都增加
if rect['direction'] == DOWNRIGHT:
rect['rect'].left += MOVESPEED
rect['rect'].top += MOVESPEED
# 如果上溢出窗口
if rect['rect'].top <= 0:
if rect['direction'] == UPLEFT:
rect['direction'] = DOWNLEFT
if rect['direction'] == UPRIGHT:
rect['direction'] = DOWNRIGHT
# 如果下溢出窗口
if rect['rect'].bottom >= HEIGHT:
if rect['direction'] == DOWNRIGHT:
rect['direction'] = UPRIGHT
if rect['direction'] == DOWNLEFT:
rect['direction'] = UPLEFT
# 如果右溢出窗口
if rect['rect'].right >= WIDTH:
if rect['direction'] == DOWNRIGHT:
rect['direction'] = DOWNLEFT
if rect['direction'] == UPRIGHT:
rect['direction'] = UPLEFT
# 如果右溢出窗口
if rect['rect'].left <= 0:
if rect['direction'] == DOWNLEFT:
rect['direction'] = DOWNRIGHT
if rect['direction'] == UPLEFT:
rect['direction'] = UPRIGHT
pygame.draw.rect(surface, rect['color'], rect['rect'])
pygame.display.update()
time.sleep(0.02)
来源:https://blog.csdn.net/qq_40801987/article/details/121651961
标签:pygame,方块动画
0
投稿
猜你喜欢
JavaScript Distilled 基础知识与函数
2024-05-02 16:29:23
最新版 Windows10上安装Python 3.8.5的步骤详解
2021-12-31 00:50:29
教你快速掌握数据库查询优化的实用技巧
2008-11-28 15:10:00
Python实现简单的俄罗斯方块游戏
2022-11-17 16:56:58
Yii框架学习笔记之应用组件操作示例
2024-05-11 09:23:07
Java并发编程数据库与缓存数据一致性方案解析
2024-01-21 21:09:47
关于vscode 默认添加python项目的源目录路径到执行环境的问题
2022-01-29 06:00:34
Django数据库迁移常见使用方法
2024-01-17 09:57:26
在Python中处理字符串之isdecimal()方法的使用
2021-03-18 04:25:18
整理的比较全的一句话后门代码(方面大家查找后门)
2023-06-17 18:30:29
php+lottery.js实现九宫格抽奖功能
2024-04-17 10:34:28
python代数式括号有效性检验示例代码
2022-04-29 04:49:49
使用numba对Python运算加速的方法
2022-05-12 20:30:57
Linux下安装PHP MSSQL扩展教程
2024-03-11 10:41:47
Python实现注册登录系统
2021-10-21 20:01:05
MySQL的意向共享锁、意向排它锁和死锁
2024-01-14 18:16:02
Dialog 按照顺序弹窗的优雅写法
2021-10-18 18:35:25
Python简单实现自动删除目录下空文件夹的方法
2021-07-28 20:28:39
微信公众平台开发教程③ PHP实现微信公众号支付功能图文详解
2023-11-18 19:09:27
bootstrapValidator bootstrap-select验证不可用的解决办法
2024-04-10 13:53:06