鱼C论坛

 找回密码
 立即注册
查看: 1147|回复: 2

pygame提高游戏的颜值1之:放大缩小乌龟会改变方向

[复制链接]
发表于 2020-12-2 19:56:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. #这里把position = position.move(speed)放到判断图片到达边界的两个if语句后面,
  5. #就可以避免出现BUG

  6. pygame.init()


  7. size = width,height = 600,400
  8. bg = (255,255,255)
  9. speed =[2,-1]

  10. clock = pygame.time.Clock()

  11. screen = pygame.display.set_mode(size,RESIZABLE)

  12. pygame.display.set_caption('你好呀 我是赛利亚')

  13. ratio = 1.0 #设置放大缩小比率

  14. oball = pygame.image.load('ball.jpg')
  15. ball = oball
  16. oball_rect = ball.get_rect()
  17. position = ball_rect=oball_rect
  18. #全屏设置
  19. fullscreen = False
  20. list_srcrren=pygame.display.list_modes()

  21. orient = 1#解决缩放的调头的BUG参数
  22. l_head = ball
  23. r_head = pygame.transform.flip(ball,True,False)#水平翻转的图像

  24. while True:
  25.     for event in pygame.event.get():
  26.         #用户调整窗口尺寸
  27.         if event.type == VIDEORESIZE and not fullscreen:
  28.             size = event.size
  29.             width,height = size
  30.             print(size)
  31.             screen = pygame.display.set_mode(size,RESIZABLE)
  32.             #又是靠复位解决BUG没有按比例来重现位置
  33.             if position.left <0 or position.right>width:
  34.                position = ball.get_rect()
  35.             if position.top <0 or position.bottom>height:
  36.                position = ball.get_rect()
  37.         if event.type == QUIT:
  38.             sys.exit()
  39.         if event.type == KEYDOWN:
  40.             if event.key == K_LEFT:
  41.                 speed = [-1,0]
  42.    
  43.                 ball = l_head
  44.   
  45.             if event.key == K_RIGHT:
  46.                 speed = [1,0]

  47.                 ball = r_head

  48.             if event.key == K_UP:
  49.                 speed = [0,-1]
  50.             if event.key == K_DOWN:
  51.                 speed = [0,1]
  52.             if event.key ==K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
  53.                 if event.key == K_EQUALS and ratio < 2:
  54.                     if orient == -1:
  55.                         ratio += 0.1
  56.                         ball = pygame.transform.flip(ball,True,False)
  57.                     else:
  58.                         ratio += 0.1
  59.                 if event.key == K_MINUS and ratio > 0.5:
  60.                     if orient == -1:
  61.                         ratio -= 0.1
  62.                         ball = pygame.transform.flip(ball,True,False)
  63.                         
  64.                     else:
  65.                         ratio -= 0.1
  66.                 if event.key == K_SPACE:
  67.                     ratio = 1.0

  68.                 ball=pygame.transform.smoothscale(oball,(int(oball_rect.width * ratio),\
  69.                                              int(oball_rect.height * ratio)))
  70.                 l_head  = ball
  71.                 r_head = pygame.transform.flip(ball,True,False)
  72.             if event.key == K_F11:
  73.                 fullscreen = not fullscreen
  74.                 if fullscreen:
  75.                     
  76.                     screen = pygame.display.set_mode(list_srcrren[1],\
  77.                     FULLSCREEN|HWSURFACE)
  78.                     size=width,height=list_srcrren[1][0],list_srcrren[1][1]
  79.                 else:
  80.                     position = ball.get_rect()
  81.                     #按下F11时复位 解决了反复横跳 但理想方法是根据比例还原位置才对
  82.                     size = width,height = 600,400
  83.                     screen = pygame.display.set_mode(size)
  84.     #position = position.move(speed) 放if前面会有一直按'下'会卡进边缘里面BUG
  85.     if position.left <0 or position.right>width:
  86.         #翻转图像

  87.         ball = pygame.transform.flip(ball,True,False)
  88.         speed[0] = -speed[0]
  89.     if position.top <0 or position.bottom>height:
  90.         speed[1] = -speed[1]
  91.         
  92.     position = position.move(speed)
  93.     #填充背景   
  94.     screen.fill(bg)
  95.     #更新图像
  96.     screen.blit(ball,position)
  97.     #更新界面
  98.     pygame.display.flip()
  99.     #延迟10毫秒
  100.     pygame.time.delay(10)
复制代码


放大缩小时会复位,所以会改变方向,请问下各位这个BUG如何解决
弹幕说在缩放前加个判断 若放缩前orient = -1, 则对放缩后图片进行水平翻转,否则方向不做改变。
但我没有修改成功  麻烦大佬们解答了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-9-13 21:31:06 | 显示全部楼层
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=[1,0]
            if event.key==pygame.K_UP:
                speed=[0,-1]
            if event.key==pygame.K_DOWN:
                speed=[0,1]

            if event.key==K_F11:
                fullscreen = not fullscreen
                if fullscreen:
                    screen = pygame.display.set_mode(pygame.display.list_modes()[0],FULLSCREEN | HWSURFACE)
                    width,height=pygame.display.list_modes()[0]
                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[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()
    #延迟10毫秒
    pygame.time.delay(10)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-13 21:32:04 | 显示全部楼层
fihh 发表于 2021-9-13 21:31
import pygame
import sys
from pygame.locals import *

我是改了他的width和height,居然成功了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-10 19:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表