导入组件
首先导入需要的组件,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}
根据矩形的移动方向改变对应的起始位置。根据矩形不同的位置改变矩形的起始位置。
if rect['direction'] == UPLEFT:
rect['rect'].left -= MOVESPEED
rect['rect'].top -= MOVESPEED
if rect['direction'] == UPRIGHT:
rect['rect'].left += MOVESPEED
rect['rect'].top -= MOVESPEED
if rect['direction'] == DOWNLEFT:
rect['rect'].left -= MOVESPEED
rect['rect'].top += MOVESPEED
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)
if rect['direction'] == UPLEFT:
rect['rect'].left -= MOVESPEED
rect['rect'].top -= MOVESPEED
if rect['direction'] == UPRIGHT:
rect['rect'].left += MOVESPEED
rect['rect'].top -= MOVESPEED
if rect['direction'] == DOWNLEFT:
rect['rect'].left -= MOVESPEED
rect['rect'].top += MOVESPEED
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)
|