先看第9行,乌龟初始速度设为 [-2,1],再看第129至138行,不按下方向键就是斜向碰壁反弹的效果。
能否设置一个按键,将乌龟绕着屏幕边界走和碰到边界反弹的效果区分开
可以设置一个 around_mode 保存状态,同时要保存之前的速度,位置和朝向等。
在第40行加上:around_mode = False
speed_0 = speed
position_0 = position
turtle_0 = turtle
再将第45行至55行方向键加上 around_mode 的条件,并加上 f 键切换模式: if event.key == K_f:
if around_mode:
speed = speed_0
position = position_0
turtle = turtle_0
else:
speed_0 = speed
position_0 = position
turtle_0 = turtle
around_mode = not around_mode
if not around_mode:
if event.key == K_LEFT:
turtle = l_head
speed = [-2,0]
elif event.key == K_RIGHT:
turtle = r_head
speed = [2,0]
elif event.key == K_UP:
speed = [0,-1]
elif event.key == K_DOWN:
speed = [0,1]
再将第95行至120行移动到下面132行的的位置,删去 pressed_keys 变量,
改为判断 around_mode,同时在标题栏上显示状态: if around_mode:
pygame.display.set_caption('初次见面,请大家多多关照 (环绕模式: 开启)')
if position.right > width:
turtle = turtle_right
position = turtle_rect = turtle.get_rect()
#改变乌龟(0,0)的位置
position.left = width- turtle_rect.width
speed =[0,5]
#之后同理
if position.bottom > height:
turtle = turtle_bottom
position = turtle_rect = turtle.get_rect()
position.left = width - turtle_rect.width
position.top = height - turtle_rect.height
speed = [-5, 0]
if position.left < 0:
turtle = turtle_left
position = turtle_rect = turtle.get_rect()
position.top = height - turtle_rect.height
speed = [0, -5]
if position.top < 0:
turtle = turtle_top
position = turtle_rect = turtle.get_rect()
speed = [5, 0]
else:
# 方向键控制
pygame.display.set_caption('初次见面,请大家多多关照 (环绕模式: 关闭)')
if position.left < 0 or position.right > width:
turtle = pygame.transform.flip(turtle,True,False)
#第一个参数是surface对象,第二个是水平翻转,第三个是竖直翻转
speed[0] = -speed[0]
if position.top < 0 or position.bottom > height:
speed[1] = -speed[1]
完整的代码如下:import pygame
import sys
from pygame.locals import *
#初始化pygame
pygame.init()
size = width,height = 1500,700
speed = [-2,1]
bg = (255,255,255) #RGB白色
fullscreen = False
ratio = 1.0
clock = pygame.time.Clock()
#创建指定大小窗口 Surface
#Surface对象就是pygame中用来表示图像的对象
screen = pygame.display.set_mode(size,RESIZABLE)
#设置窗口标题
pygame.display.set_caption('初次见面,请大家多多关照')
#加载图片 也是Surface对象
#一个Surface对象不能经过两次加工,否则会很难看
oturtle = pygame.image.load('turtle.jpeg').convert_alpha()
turtle = oturtle
oturtle_rect = oturtle.get_rect()
position = turtle_rect = oturtle_rect
#获得图片位置矩形
#position = turtle.get_rect()
ol_head = l_head = turtle
or_head = r_head = pygame.transform.flip(turtle,True,False)
#自动翻转
#rotate方向为逆时针
turtle_right = pygame.transform.rotate(oturtle,90)
turtle_top = pygame.transform.rotate(oturtle,180)
turtle_left = pygame.transform.rotate(oturtle,270)
turtle_bottom = oturtle
# 环绕模式
around_mode = False
speed_0 = speed
position_0 = position
turtle_0 = turtle
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
# f 键切换环绕模式
if event.key == K_f:
if around_mode:
speed = speed_0
position = position_0
turtle = turtle_0
else:
speed_0 = speed
position_0 = position
turtle_0 = turtle
around_mode = not around_mode
# 非环绕模式,方向键有效
if not around_mode:
if event.key == K_LEFT:
turtle = l_head
speed = [-2,0]
elif event.key == K_RIGHT:
turtle = r_head
speed = [2,0]
elif event.key == K_UP:
speed = [0,-1]
elif event.key == K_DOWN:
speed = [0,1]
#全屏a
if event.key == K_a:
fullscreen = not fullscreen
if fullscreen:
screen = pygame.display.set_mode((3840, 2160),FULLSCREEN | HWSURFACE)
width,height = 1920,1080
#改尺寸
else:
screen = pygame.display.set_mode(size)
#放大缩小乌龟(=,-),空格键恢复尺寸
if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
if event.key == K_EQUALS and ratio < 1.4:
ratio += 0.1
if event.key == K_MINUS and ratio > 0.5:
ratio -= 0.1
if event.key == K_SPACE:
ratio = 1.0
#此时注意乌龟大小会变
if turtle == l_head:
oturtle = ol_head
oturtle_rect = oturtle.get_rect()
l_head = turtle = pygame.transform.smoothscale(oturtle,\
(int(oturtle_rect.width * ratio),\
int(oturtle_rect.height * ratio)))
r_head = pygame.transform.flip(turtle,True,False)
if turtle == r_head:
oturtle = or_head
oturtle_rect = oturtle.get_rect()
r_head = turtle = pygame.transform.smoothscale(oturtle,\
(int(oturtle_rect.width * ratio),\
int(oturtle_rect.height * ratio)))
l_head = pygame.transform.flip(turtle,True,False)
#也同时防止变量被污染
#放大缩小的头重新移动
#按住进入绕边走模式 (代码移动到下面)
if event.type == VIDEORESIZE:
size = event.size
width,height = size
print(size)
screen = pygame.display.set_mode(size,RESIZABLE)
position = position.move(speed)
#调用rect对象的move方法
# 环绕模式
if around_mode:
pygame.display.set_caption('初次见面,请大家多多关照 (环绕模式: 开启)')
if position.right > width:
turtle = turtle_right
position = turtle_rect = turtle.get_rect()
#改变乌龟(0,0)的位置
position.left = width- turtle_rect.width
speed =[0,5]
#之后同理
if position.bottom > height:
turtle = turtle_bottom
position = turtle_rect = turtle.get_rect()
position.left = width - turtle_rect.width
position.top = height - turtle_rect.height
speed = [-5, 0]
if position.left < 0:
turtle = turtle_left
position = turtle_rect = turtle.get_rect()
position.top = height - turtle_rect.height
speed = [0, -5]
if position.top < 0:
turtle = turtle_top
position = turtle_rect = turtle.get_rect()
speed = [5, 0]
else:
# 方向键控制
pygame.display.set_caption('初次见面,请大家多多关照 (环绕模式: 关闭)')
if position.left < 0 or position.right > width:
turtle = pygame.transform.flip(turtle,True,False)
#第一个参数是surface对象,第二个是水平翻转,第三个是竖直翻转
speed[0] = -speed[0]
if position.top < 0 or position.bottom > height:
speed[1] = -speed[1]
#填充背景(看不到乌龟了)
screen.fill(bg)
#更新图像,但是每刻都是一个图像(只有像素)
screen.blit(turtle,position)
#双缓冲flip更新界面,重要!!!
pygame.display.flip()
#延迟10毫秒
pygame.time.delay(10)
clock.tick(200)
#不高于200帧
|