|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import pygame
import sys
from pygame.locals import *
# 初始化pygame
pygame.init()
size = width, height = 600, 400
bg = (255, 255, 255) #RGB
fullscreen = False
# 指定窗口大小
screen = pygame.display.set_mode(size, RESIZABLE)
# 设置窗口标题
pygame.display.set_caption("初次见面,请大家多多关照!")
# 设置放大缩小的比率
ratio = 1.0
# 加载图片
oturtle = pygame.image.load("2.jpeg")
turtle = oturtle
# 获得图像的位置矩形
oturtle_rect = oturtle.get_rect()
position = turtle_rect = oturtle_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_buttom = 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 == KEYDOWN:
# 全屏
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.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
# 最大只能放大一倍,缩小50%
if event.key == K_EQUALS and ratio < 2:
ratio += 0.1
if event.key == K_MINUS and ratio > 0.5:
ratio -= 0.1
if event.key == K_SPACE:
ratio = 1.0
turtle = pygame.transform.smoothscale(oturtle, (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)
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_height
speed = [-5 , 0]
if position.left < 0:
turtle = turtle_left
position = turtle_rect = turtle.get_rect()
position.top = height - turtle_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)
第82行中
if position.bottom > height:
turtle = turtle_bottom
position = turtle_rect = turtle.get_rect()
position.left = width - turtle_rect.width
position.top = height - turtle_height
speed = [-5 , 0]
小甲鱼在视频中写的是这个,但是明明turtle_height没有定义,为什么他的可以运行而我的不可以
1、82 行 是 turtle_buttom 不是 turtle_bottom
2、85、91行 是 turtle_rect 中的 height 属性,不是 turtle_height
参考代码:
import pygame
import sys
from pygame.locals import *
# 初始化pygame
pygame.init()
size = width, height = 600, 400
bg = (255, 255, 255) #RGB
fullscreen = False
# 指定窗口大小
screen = pygame.display.set_mode(size, RESIZABLE)
# 设置窗口标题
pygame.display.set_caption("初次见面,请大家多多关照!")
# 设置放大缩小的比率
ratio = 1.0
# 加载图片
oturtle = pygame.image.load("2.jpeg")
turtle = oturtle
# 获得图像的位置矩形
oturtle_rect = oturtle.get_rect()
position = turtle_rect = oturtle_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_buttom = 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 == KEYDOWN:
# 全屏
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.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
# 最大只能放大一倍,缩小50%
if event.key == K_EQUALS and ratio < 2:
ratio += 0.1
if event.key == K_MINUS and ratio > 0.5:
ratio -= 0.1
if event.key == K_SPACE:
ratio = 1.0
turtle = pygame.transform.smoothscale(oturtle, (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)
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_buttom
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)
|
|