pygame提高游戏的颜值1之:放大缩小乌龟会改变方向
import pygameimport sys
from pygame.locals import *
#这里把position = position.move(speed)放到判断图片到达边界的两个if语句后面,
#就可以避免出现BUG
pygame.init()
size = width,height = 600,400
bg = (255,255,255)
speed =
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size,RESIZABLE)
pygame.display.set_caption('你好呀 我是赛利亚')
ratio = 1.0 #设置放大缩小比率
oball = pygame.image.load('ball.jpg')
ball = oball
oball_rect = ball.get_rect()
position = ball_rect=oball_rect
#全屏设置
fullscreen = False
list_srcrren=pygame.display.list_modes()
orient = 1#解决缩放的调头的BUG参数
l_head = ball
r_head = pygame.transform.flip(ball,True,False)#水平翻转的图像
while True:
for event in pygame.event.get():
#用户调整窗口尺寸
if event.type == VIDEORESIZE and not fullscreen:
size = event.size
width,height = size
print(size)
screen = pygame.display.set_mode(size,RESIZABLE)
#又是靠复位解决BUG没有按比例来重现位置
if position.left <0 or position.right>width:
position = ball.get_rect()
if position.top <0 or position.bottom>height:
position = ball.get_rect()
if event.type == QUIT:
sys.exit()
if event.type == KEYDOWN:
if event.key == K_LEFT:
speed = [-1,0]
ball = l_head
if event.key == K_RIGHT:
speed =
ball = r_head
if event.key == K_UP:
speed =
if event.key == K_DOWN:
speed =
if event.key ==K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
if event.key == K_EQUALS and ratio < 2:
if orient == -1:
ratio += 0.1
ball = pygame.transform.flip(ball,True,False)
else:
ratio += 0.1
if event.key == K_MINUS and ratio > 0.5:
if orient == -1:
ratio -= 0.1
ball = pygame.transform.flip(ball,True,False)
else:
ratio -= 0.1
if event.key == K_SPACE:
ratio = 1.0
ball=pygame.transform.smoothscale(oball,(int(oball_rect.width * ratio),\
int(oball_rect.height * ratio)))
l_head= ball
r_head = pygame.transform.flip(ball,True,False)
if event.key == K_F11:
fullscreen = not fullscreen
if fullscreen:
screen = pygame.display.set_mode(list_srcrren,\
FULLSCREEN|HWSURFACE)
size=width,height=list_srcrren,list_srcrren
else:
position = ball.get_rect()
#按下F11时复位 解决了反复横跳 但理想方法是根据比例还原位置才对
size = width,height = 600,400
screen = pygame.display.set_mode(size)
#position = position.move(speed) 放if前面会有一直按'下'会卡进边缘里面BUG
if position.left <0 or position.right>width:
#翻转图像
ball = pygame.transform.flip(ball,True,False)
speed = -speed
if position.top <0 or position.bottom>height:
speed = -speed
position = position.move(speed)
#填充背景
screen.fill(bg)
#更新图像
screen.blit(ball,position)
#更新界面
pygame.display.flip()
#延迟10毫秒
pygame.time.delay(10)
放大缩小时会复位,所以会改变方向,请问下各位这个BUG如何解决
弹幕说在缩放前加个判断 若放缩前orient = -1, 则对放缩后图片进行水平翻转,否则方向不做改变。
但我没有修改成功麻烦大佬们解答了 import pygame
import sys
from pygame.locals import *
#初始化pygame
pygame.init()
size = width,height = 600,400
speed = [-2,1]
bg=(255,255,255)#RGB
fullscreen=False
#创建指定大小的窗口 得到surface对象
screen = pygame.display.set_mode(size,RESIZABLE)
#设置窗口标题
pygame.display.set_caption("初次见面,请大家多多关照!")
#设置放大缩小的比率
ratio = 1.0
oturtle = pygame.image.load("123.png")
turtle = oturtle
oturtle_rect = oturtle.get_rect()
turtle_rect = oturtle_rect
position = turtle_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==pygame.K_LEFT:
turtle=l_head
speed=[-1,0]
if event.key==pygame.K_RIGHT:
turtle=r_head
speed=
if event.key==pygame.K_UP:
speed=
if event.key==pygame.K_DOWN:
speed=
if event.key==K_F11:
fullscreen = not fullscreen
if fullscreen:
screen = pygame.display.set_mode(pygame.display.list_modes(),FULLSCREEN | HWSURFACE)
width,height=pygame.display.list_modes()
else:
screen = pygame.display.set_mode(size)
width,height=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)))
l_head=turtle
r_head=pygame.transform.flip(turtle,True,False)
(temp1,temp2,position_width,position_height)=turtle.get_rect()
position.width=position_width
position.height=position_height
#用户调整窗口尺寸
if event.type == VIDEORESIZE:
size = event.size
width,height=size
screen = pygame.display.set_mode(size,RESIZABLE)
pygame.display.set_caption("Window resized to " + str(event.size))
position=position.move(speed)
if position.left<0 or position.right>width:
#翻转图像
turtle = pygame.transform.flip(turtle,True,False)
#反方向移动
speed = -speed
if position.top < 0 or position.bottom > height:
speed = -speed
#填充背景
screen.fill(bg)
#更新图片
screen.blit(turtle,position)#双缓冲,在内存中写好
#更新界面
pygame.display.flip()
#延迟10毫秒
pygame.time.delay(10) fihh 发表于 2021-9-13 21:31
import pygame
import sys
from pygame.locals import *
我是改了他的width和height,居然成功了
页:
[1]