本帖最后由 wuqramy 于 2020-4-21 17:19 编辑
这一段 if position.top<0:
turtle = turtle_top
position = turtle_rect = turtle.get_rect()
speed = [-5,0]
speed应该是[5,0]
正确代码:import pygame
import sys
from pygame.locals import *
#初始化Pygame
pygame.init()
size=width,height=600,400
bg=(255,255,255)
fullscreen = False
clock = pygame.time.Clock()
#创建指定大小的窗口
screen=pygame.display.set_mode(size,RESIZABLE)
#设置窗口标特
pygame.display.set_caption('初次见面,请大家多多关照!')
#加载图片
turtle = pygame.image.load('turtle.png')
#获得图像的位置矩形
position = turtle.get_rect()
speed=[5,0]
turtle_right = pygame.transform.rotate(turtle,90)
turtle_top = pygame.transform.rotate(turtle,180)
turtle_left = pygame.transform.rotate(turtle,270)
turtle_bottom = turtle
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 == pygame.KEYDOWN:
#全屏(F11)
if event.key == K_F11:
fullscreen = not fullscreen
if fullscreen:
screen = pygame.display.set_mode((1024,768),FULLSCREEN | HWSURFACE)
width,height = 1024,768
else:
screen=pygame.display.set_mode(size)
#用户调整窗口尺寸
if event.type == VIDEORESIZE:
size = event.size
width,height = size
print(size)
screen=pygame.display.set_mode(size,RESIZABLE)
#移动图像
position = position.move(speed)
if position.right > width:
turtle = turtle_right
position = turtle_rect = turtle.get_rect()
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]
#填充背景
screen.fill(bg)
#更新图像
screen.blit(turtle,position)
#更新界面
pygame.display.flip()
#延迟10毫秒
pygame.time.delay(10)
|