pygame实现方块动画

16 篇文章 2 订阅

导入组件

首先导入需要的组件,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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我的天才女友

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值