|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 lzb1001 于 2022-6-14 11:31 编辑 import pygame
import sys
from pygame.locals import *
pygame.init()
size = width, height = 600, 400
speed = [-2, 1]
bg = (255, 255, 255)
fullscreen = False
screen = pygame.display.set_mode(siz
pygame.display.set_caption('Hello!初次见面,请大家多多关照~~')
turtle = pygame.image.load(r'd:\\work\\turtle_left.png')
position = turtle.get_rect()
l_head = turtle
r_head = pygame.transform.flip(turtle, True, False)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == KEYDOWN:
if event.key == K_LEFT:
turtle = l_head
speed = [-1, 0]
if event.key == K_RIGHT:
turtle = r_head
speed = [1, 0]
#speed = [2, 1]
if event.key == K_UP:
speed = [0, -1]
if event.key == K_DOWN:
speed = [0, 1]
if event.key == K_F11:
fullscreen = not fullscreen
if fullscreen:
screen = pygame.display.set_mode((1366, 768), FULLSCREEN | HWSURFACE)
else:
screen = pygame.display.set_mode(size)
position = position.move(speed)
if position.left < 0 or position.right > width:
turtle = pygame.transform.flip(turtle, True, False)
speed[0] = -speed[0]
if position.top < 0 or position.bottom > height:
speed[1] = -speed[1]
screen.fill(bg)
screen.blit(turtle, position)
pygame.display.flip()
pygame.time.delay(20)
------------------------------------------
Windows 10 专业版 | Python 3.7.6
------------------------------------------
【我的问题】
以上代码完全按照小甲鱼的教学视频敲的,但运行后发现bug已知的就至少有以下:
1、用左右方向键控制小乌龟后再让它自由移动,小乌龟就只能左右移动而不像之前那样可以走斜线
2、全屏后,小乌龟的移动范围并没有实际扩大
3、从全屏切换为普通窗口模式后发现也不是原来定义的窗口大小
4、……
******************************
感谢大神不吝赐教,为新手解疑释惑。
赠人玫瑰,手有余香,好人一生平安!
本帖最后由 白two 于 2022-6-14 11:51 编辑
1、用左右方向键控制小乌龟后再让它自由移动,小乌龟就只能左右移动而不像之前那样可以走斜线: 你按了左右键后改的肯定是整个速度列表, 只需要改横向速度就可以了, 就像这样 speed[0] = -speed[0]
2、全屏后,小乌龟的移动范围并没有实际扩大: 我猜测你可能只是扩大了屏幕, 但是没有扩大 size 的两个值, 扩大后重新获取一下 size 就行, 推荐自己设定 size 然后按照这个来最大化
3、从全屏切换为普通窗口模式后发现也不是原来定义的窗口大小: 同上, 重新设定 size 列表, 在设定屏幕大小
以下附上源码, 以前练手之作, 有些冗余, 将就着看吧
比如按左,就可以 speed[0] = -1, 就会省掉很多代码 import pygame
import sys
from pygame.constants import RESIZABLE, VIDEORESIZE
def change_pos(t_position, t_width, t_height):
if t_position.right > t_width:
t_position.right = t_width
if t_position.bottom > t_height:
t_position.bottom = t_height
pygame.init()
# size = width, height = (600, 400)
size = width, height = (1536, 864)
speed = [-2, 1]
toward = [True, False]
updown = [False, True]
bg = (255, 255, 255)
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size, RESIZABLE)
pygame.display.set_caption("初次见面,请多多关照!")
# post the turtle
turtle = pygame.image.load(r"turtle.jpeg")
position = turtle.get_rect()
fullscreen = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == VIDEORESIZE:
if event.size[0] < position.width:
event.size = (position.width, event.size[1])
size = event.size
width, height = size
screen = pygame.display.set_mode(size, RESIZABLE)
change_pos(position, width, height)
if event.size[1] < position.height:
event.size = (event.size[0], position.height)
size = event.size
width, height = size
screen = pygame.display.set_mode(size, RESIZABLE)
change_pos(position, width, height)
else:
size = event.size
width, height = size
screen = pygame.display.set_mode(size, RESIZABLE)
change_pos(position, width, height)
if event.type == pygame.KEYDOWN:
if position.left > 0 and (event.key == pygame.K_a or event.key == pygame.K_LEFT):
if not toward[0]:
toward[0] = True
toward[1] = False
turtle = pygame.transform.flip(turtle, True, False)
speed[0] = -speed[0]
if position.right < width and (event.key == pygame.K_d or event.key == pygame.K_RIGHT):
if not toward[1]:
toward[1] = True
toward[0] = False
turtle = pygame.transform.flip(turtle, True, False)
speed[0] = -speed[0]
if position.top > 0 and (event.key == pygame.K_w or event.key == pygame.K_UP):
if not updown[0]:
updown[0] = not updown[0]
updown[1] = not updown[1]
speed[1] = -speed[1]
if position.bottom < height and (event.key == pygame.K_s or event.key == pygame.K_DOWN):
if not updown[1]:
updown[0] = not updown[0]
updown[1] = not updown[1]
speed[1] = -speed[1]
if event.key == pygame.K_F11:
fullscreen = not fullscreen
if fullscreen:
# size = width, height = pygame.display.list_modes()[0]
size = width, height = (1536, 864)
screen = pygame.display.set_mode(size, pygame.FULLSCREEN | pygame.HWSURFACE)
else:
size = width, height = (600, 400)
screen = pygame.display.set_mode(size)
change_pos(position, width, height)
position = position.move(speed)
if position.left < 0 or position.right > width:
turtle = pygame.transform.flip(turtle, True, False)
toward[0] = not toward[0]
toward[1] = not toward[1]
speed[0] = -speed[0]
if position.top < 0 or position.bottom > height:
updown[0] = not updown[0]
updown[1] = not updown[1]
speed[1] = -speed[1]
screen.fill(bg)
screen.blit(turtle, position)
pygame.display.flip()
# pygame.time.delay(5)
clock.tick(200)
|
|