鱼C论坛

 找回密码
 立即注册
查看: 1399|回复: 5

为什么这段代码无法满足放大和缩小?

[复制链接]
发表于 2021-9-2 21:09:33 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 飞花落尽 于 2021-9-12 11:27 编辑
  1. import pygame
  2. import sys
  3. from pygame.locals import *

  4. #初始化pygame
  5. pygame.init()

  6. size = width,height = 1000,800
  7. speed = [-2,1]
  8. bg = (255,255,255) #RGB白色
  9. fullscreen = False
  10. ratio = 1.0

  11. clock = pygame.time.Clock()

  12. #创建指定大小窗口 Surface
  13. #Surface对象就是pygame中用来表示图像的对象
  14. screen = pygame.display.set_mode(size,RESIZABLE)
  15. #设置窗口标题
  16. pygame.display.set_caption('初次见面,请大家多多关照')

  17. #加载图片 也是Surface对象
  18. #一个Surface对象不能经过两次加工,否则会很难看
  19. oturtle = pygame.image.load('turtle.jpeg')
  20. turtle = oturtle
  21. oturtle_rect = oturtle.get_rect()
  22. position = turtle_rect = oturtle_rect  
  23. #获得图片位置矩形
  24. #position = turtle.get_rect()
  25. l_head = turtle
  26. r_head = pygame.transform.flip(turtle,True,False)
  27. #自动翻转

  28. while True:
  29.     for event in pygame.event.get():
  30.         if event.type == pygame.QUIT:
  31.             sys.exit()
  32.         if event.type == pygame.KEYDOWN:
  33.             if event.key == K_LEFT:
  34.                 turtle = l_head
  35.                 speed = [-2,0]
  36.             elif event.key == K_RIGHT:
  37.                 turtle = r_head
  38.                 speed = [2,0]
  39.             elif event.key == K_UP:
  40.                 speed = [0,-1]
  41.             elif event.key == K_DOWN:
  42.                 speed = [0,1]

  43.             #全屏a
  44.             if event.key == K_a:
  45.                 fullscreen = not fullscreen
  46.                 if fullscreen:
  47.                     screen = pygame.display.set_mode((3840, 2160),FULLSCREEN | HWSURFACE)
  48.                     width,height = 1920,1080
  49.                     #改尺寸
  50.                 else:
  51.                     screen = pygame.display.set_mode(size)
  52.             #放大缩小乌龟(=,-),空格键恢复尺寸
  53.             if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
  54.                 if event.key == K_EQUALS and ratio < 2:
  55.                     ratio += 0.1
  56.                 elif event.key == K_MINUS and ratio >0.5:
  57.                     ratio -= 0.1
  58.                 elif event.key == K_SPACE:
  59.                     ratio = 1.0
  60.                 if turtle == l_head:
  61.                     turtle = pygame.transform.smoothscale(oturtle,\
  62.                                              (int(oturtle_rect.width * ratio),\
  63.                                               int(oturtle_rect.height * ratio)))
  64.                 if turtle == r_head:
  65.                     oturtle = pygame.transform.flip(oturtle,True,False)
  66.                     turtle = pygame.transform.smoothscale(oturtle,\
  67.                                              (int(oturtle_rect.width * ratio),\
  68.                                               int(oturtle_rect.height * ratio)))
  69.                 #也同时防止变量被污染
  70.                 #放大缩小的头重新移动
  71.                     
  72.         if event.type == VIDEORESIZE:
  73.             size = event.size
  74.             width,height = size
  75.             print(size)
  76.             screen = pygame.display.set_mode(size,RESIZABLE)

  77.             
  78.     position = position.move(speed)
  79.     #调用rect对象的move方法

  80.     if position.left < 0 or position.right > width:
  81.         turtle = pygame.transform.flip(turtle,True,False)
  82.         #第一个参数是surface对象,第二个是水平翻转,第三个是竖直翻转
  83.         speed[0] = -speed[0]

  84.     if position.top < 0 or position.bottom > height:
  85.         speed[1] = -speed[1]

  86.     #填充背景(看不到乌龟了)
  87.     screen.fill(bg)
  88.     #更新图像,但是每刻都是一个图像(只有像素)
  89.     screen.blit(turtle,position)
  90.     #双缓冲flip更新界面,重要!!!
  91.     pygame.display.flip()
  92.     #延迟10毫秒
  93.     pygame.time.delay(10)
  94.     clock.tick(200)
  95.     #不高于200帧
复制代码

要求是乌龟放大缩小后都不能倒退行走(小甲鱼视频81讲)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-9-5 08:21:21 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-9-12 18:03:26 | 显示全部楼层
有没有哪位好心人?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-9-12 18:04:02 | 显示全部楼层
顶一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-9-12 22:31:07 | 显示全部楼层
有没有哪位好心人?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-9-12 22:31:37 | 显示全部楼层
有没有哪位好心人?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-10 23:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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