pygame中我的程序为什么先得按下方向键才可以
本帖最后由 飞花落尽 于 2021-9-13 10:07 编辑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
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == K_LEFT:
turtle = l_head
speed = [-2,0]
elif event.key == K_RIGHT:
turtle = r_head
speed =
elif event.key == K_UP:
speed =
elif event.key == K_DOWN:
speed =
#全屏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)
#也同时防止变量被污染
#放大缩小的头重新移动
#按住进入绕边走模式
pressed_keys = pygame.key.get_pressed()
if pressed_keys:
if position.right > width:
turtle = turtle_right
position = turtle_rect = turtle.get_rect()
#改变乌龟(0,0)的位置
position.left = width- turtle_rect.width
speed =
#之后同理
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 =
if position.top < 0:
turtle = turtle_top
position = turtle_rect = turtle.get_rect()
speed =
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 position.left < 0 or position.right > width:
turtle = pygame.transform.flip(turtle,True,False)
#第一个参数是surface对象,第二个是水平翻转,第三个是竖直翻转
speed = -speed
if position.top < 0 or position.bottom > height:
speed = -speed
#填充背景(看不到乌龟了)
screen.fill(bg)
#更新图像,但是每刻都是一个图像(只有像素)
screen.blit(turtle,position)
#双缓冲flip更新界面,重要!!!
pygame.display.flip()
#延迟10毫秒
pygame.time.delay(10)
clock.tick(200)
#不高于200帧
另外,能否设置一个按键,将乌龟绕着屏幕边界走和碰到边界反弹的效果区分开? 本帖最后由 blahblahfc 于 2021-9-13 18:24 编辑
原因是一开始 position.left 的值是 0,speed 的值是 -2,向左运动。
第150行满足条件:
if position.left <= 0:
执行 speed = -speed ,之后 speed 变成 2,向右运动。
第150行又满足条件,speed 不断取反,乌龟的水平位置在 -2 和 0 来回抖动
只要把 if position.left <= 0: 改为 if position.left < 0: 应该就正常了。
另外第96行至104行改为:
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 dir_x ==1:
turtle = r_head
position.size = turtle.get_rect().size
改变大小后更新位置信息 为什么先得按下方向键才可以
先看第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 =
elif event.key == K_UP:
speed =
elif event.key == K_DOWN:
speed =
再将第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 =
#之后同理
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 =
if position.top < 0:
turtle = turtle_top
position = turtle_rect = turtle.get_rect()
speed =
else:
# 方向键控制
pygame.display.set_caption('初次见面,请大家多多关照 (环绕模式: 关闭)')
if position.left < 0 or position.right > width:
turtle = pygame.transform.flip(turtle,True,False)
#第一个参数是surface对象,第二个是水平翻转,第三个是竖直翻转
speed = -speed
if position.top < 0 or position.bottom > height:
speed = -speed
完整的代码如下:
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 =
elif event.key == K_UP:
speed =
elif event.key == K_DOWN:
speed =
#全屏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 =
#之后同理
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 =
if position.top < 0:
turtle = turtle_top
position = turtle_rect = turtle.get_rect()
speed =
else:
# 方向键控制
pygame.display.set_caption('初次见面,请大家多多关照 (环绕模式: 关闭)')
if position.left < 0 or position.right > width:
turtle = pygame.transform.flip(turtle,True,False)
#第一个参数是surface对象,第二个是水平翻转,第三个是竖直翻转
speed = -speed
if position.top < 0 or position.bottom > height:
speed = -speed
#填充背景(看不到乌龟了)
screen.fill(bg)
#更新图像,但是每刻都是一个图像(只有像素)
screen.blit(turtle,position)
#双缓冲flip更新界面,重要!!!
pygame.display.flip()
#延迟10毫秒
pygame.time.delay(10)
clock.tick(200)
#不高于200帧 本帖最后由 飞花落尽 于 2021-9-13 10:03 编辑
blahblahfc 发表于 2021-9-13 09:00
先看第9行,乌龟初始速度设为 [-2,1],再看第129至138行,不按下方向键就是斜向碰壁反弹的效果。
谢谢对第二个问题的解答,但是第一个问题我想问的是为什么要先按下方向键后才可以放大缩小乌龟?
(不知道为什么没打全) blahblahfc 发表于 2021-9-13 09:00
先看第9行,乌龟初始速度设为 [-2,1],再看第129至138行,不按下方向键就是斜向碰壁反弹的效果。
还有放大缩小后新的乌龟好像会在新的边界返回? 为什么要先按下方向键后才可以放大缩小乌龟?
这个功能我这里不能复现,一直都不能放大和缩小乌龟。
原因是第75行
if turtle == l_head:
和第83行
if turtle == r_head:
if 的条件始终为假
开头定义的 oturtle, turtle, ol_head, l_head, or_head, r_head 的类型都是 Surface,
在进行 pygame.transform.flip 或者 pygame.transform.rotate 等操作之后,其结果就不是同一个 Surface 对象
建议用标志状态(int 或者 bool)来判断朝向,dir_x = 0
再将 if turtle == r_head: 之类的改为 if dir_x == 1: 之类的。 blahblahfc 发表于 2021-9-13 14:17
这个功能我这里不能复现,一直都不能放大和缩小乌龟。
原因是第75行
我换成这样好像更不行了{:9_241:}不知道哪里出问题了import pygame
import sys
from pygame.locals import *
#初始化pygame
pygame.init()
size = width,height = 1500,800
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()
l_head = turtle
r_head = pygame.transform.flip(turtle,True,False)
dir_x = 1
#自动翻转
#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 =
elif event.key == K_UP:
speed =
elif event.key == K_DOWN:
speed =
#全屏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 dir_x == 0:
turtle = pygame.transform.smoothscale(oturtle,\
(int(oturtle_rect.width * ratio),\
int(oturtle_rect.height * ratio)))
if dir_x == 1:
turtle = pygame.transform.smoothscale(r_head,\
(int(oturtle_rect.width * ratio),\
int(oturtle_rect.height * ratio)))
#也同时防止变量被污染
#放大缩小的头重新移动
#按住进入绕边走模式 (代码移动到下面)
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 =
#之后同理
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 =
if position.top < 0:
turtle = turtle_top
position = turtle_rect = turtle.get_rect()
speed =
else:
# 方向键控制
pygame.display.set_caption('初次见面,请大家多多关照 (环绕模式: 关闭)')
if position.left <= 0:
turtle = pygame.transform.flip(turtle,True,False)
#第一个参数是surface对象,第二个是水平翻转,第三个是竖直翻转
speed = -speed
dir_x = 1
if position.right > width:
turtle = pygame.transform.flip(turtle,True,False)
#第一个参数是surface对象,第二个是水平翻转,第三个是竖直翻转
speed = -speed
dir_x = 0
if position.top < 0 or position.bottom > height:
speed = -speed
#填充背景(看不到乌龟了)
screen.fill(bg)
#更新图像,但是每刻都是一个图像(只有像素)
screen.blit(turtle,position)
#双缓冲flip更新界面,重要!!!
pygame.display.flip()
#延迟10毫秒
pygame.time.delay(10)
clock.tick(200)
#不高于200帧 blahblahfc 发表于 2021-9-13 18:05
原因是一开始 position.left 的值是 0,speed 的值是 -2,向左运动。
第150行满足条件:
好像还是有朝向问题(放大缩小的时候只有一边import pygame
import sys
from pygame.locals import *
#初始化pygame
pygame.init()
size = width,height = 1500,800
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()
l_head = turtle
r_head = pygame.transform.flip(turtle,True,False)
dir_x = 1
#自动翻转
#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 =
elif event.key == K_UP:
speed =
elif event.key == K_DOWN:
speed =
#全屏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 dir_x == 0:
#turtle = pygame.transform.smoothscale(oturtle,\
#(int(oturtle_rect.width * ratio),\
#int(oturtle_rect.height * ratio)))
#if dir_x == 1:
#turtle = pygame.transform.smoothscale(r_head,\
#(int(oturtle_rect.width * ratio),\
#int(oturtle_rect.height * ratio)))
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 dir_x == 1:
turtle = r_head
position.size = turtle.get_rect().size
#也同时防止变量被污染
#放大缩小的头重新移动
#按住进入绕边走模式 (代码移动到下面)
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 =
#之后同理
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 =
if position.top < 0:
turtle = turtle_top
position = turtle_rect = turtle.get_rect()
speed =
else:
# 方向键控制
pygame.display.set_caption('初次见面,请大家多多关照 (环绕模式: 关闭)')
if position.left < 0:
turtle = pygame.transform.flip(turtle,True,False)
#第一个参数是surface对象,第二个是水平翻转,第三个是竖直翻转
speed = -speed
dir_x = 1
if position.right > width:
turtle = pygame.transform.flip(turtle,True,False)
#第一个参数是surface对象,第二个是水平翻转,第三个是竖直翻转
speed = -speed
dir_x = 0
if position.top < 0 or position.bottom > height:
speed = -speed
#填充背景(看不到乌龟了)
screen.fill(bg)
#更新图像,但是每刻都是一个图像(只有像素)
screen.blit(turtle,position)
#双缓冲flip更新界面,重要!!!
pygame.display.flip()
#延迟10毫秒
pygame.time.delay(10)
clock.tick(200)
#不高于200帧
)?{:9_222:} blahblahfc 发表于 2021-9-13 18:05
原因是一开始 position.left 的值是 0,speed 的值是 -2,向左运动。
第150行满足条件:
没事了,方向键的那里忘记加了,谢谢大神{:9_228:}
页:
[1]