为什么这段代码无法满足放大和缩小?
本帖最后由 飞花落尽 于 2021-9-12 11:27 编辑import pygame
import sys
from pygame.locals import *
#初始化pygame
pygame.init()
size = width,height = 1000,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')
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)
#自动翻转
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 < 2:
ratio += 0.1
elif event.key == K_MINUS and ratio >0.5:
ratio -= 0.1
elif event.key == K_SPACE:
ratio = 1.0
if turtle == l_head:
turtle = pygame.transform.smoothscale(oturtle,\
(int(oturtle_rect.width * ratio),\
int(oturtle_rect.height * ratio)))
if turtle == r_head:
oturtle = pygame.transform.flip(oturtle,True,False)
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)
#调用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帧
要求是乌龟放大缩小后都不能倒退行走(小甲鱼视频81讲) {:5_109:} 有没有哪位好心人? 顶一下 有没有哪位好心人?
有没有哪位好心人?
页:
[1]