鱼C论坛

 找回密码
 立即注册
查看: 1296|回复: 8

[已解决]pygame中我的程序为什么先得按下方向键才可以

[复制链接]
发表于 2021-9-12 23:43:54 | 显示全部楼层 |阅读模式
12鱼币
本帖最后由 飞花落尽 于 2021-9-13 10:07 编辑
  1. import pygame
  2. import sys
  3. from pygame.locals import *

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

  6. size = width,height = 1500,700
  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').convert_alpha()
  20. turtle = oturtle
  21. oturtle_rect = oturtle.get_rect()
  22. position = turtle_rect = oturtle_rect  
  23. #获得图片位置矩形
  24. #position = turtle.get_rect()
  25. ol_head = l_head = turtle
  26. or_head = r_head = pygame.transform.flip(turtle,True,False)
  27. #自动翻转

  28. #rotate方向为逆时针
  29. turtle_right = pygame.transform.rotate(oturtle,90)
  30. turtle_top = pygame.transform.rotate(oturtle,180)
  31. turtle_left = pygame.transform.rotate(oturtle,270)
  32. turtle_bottom = oturtle

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

  48.             #全屏a
  49.             if event.key == K_a:
  50.                 fullscreen = not fullscreen
  51.                 if fullscreen:
  52.                     screen = pygame.display.set_mode((3840, 2160),FULLSCREEN | HWSURFACE)
  53.                     width,height = 1920,1080
  54.                     #改尺寸
  55.                 else:
  56.                     screen = pygame.display.set_mode(size)
  57.             #放大缩小乌龟(=,-),空格键恢复尺寸
  58.             if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
  59.                 if event.key == K_EQUALS and ratio < 1.4:
  60.                     ratio += 0.1
  61.                 if event.key == K_MINUS and ratio > 0.5:
  62.                     ratio -= 0.1
  63.                 if event.key == K_SPACE:
  64.                     ratio = 1.0
  65.                 #此时注意乌龟大小会变
  66.                 if turtle == l_head:
  67.                     oturtle = ol_head
  68.                     oturtle_rect = oturtle.get_rect()
  69.                     l_head = turtle = pygame.transform.smoothscale(oturtle,\
  70.                                              (int(oturtle_rect.width * ratio),\
  71.                                               int(oturtle_rect.height * ratio)))
  72.                     r_head = pygame.transform.flip(turtle,True,False)
  73.                     
  74.                 if turtle == r_head:
  75.                     oturtle = or_head
  76.                     oturtle_rect = oturtle.get_rect()
  77.                     r_head = turtle = pygame.transform.smoothscale(oturtle,\
  78.                                              (int(oturtle_rect.width * ratio),\
  79.                                               int(oturtle_rect.height * ratio)))
  80.                     l_head = pygame.transform.flip(turtle,True,False)
  81.                     
  82.                
  83.                 #也同时防止变量被污染
  84.                 #放大缩小的头重新移动
  85.                 #按住进入绕边走模式
  86.         pressed_keys = pygame.key.get_pressed()
  87.         if pressed_keys[pygame.K_CAPSLOCK]:
  88.             if position.right > width:
  89.                 turtle = turtle_right
  90.                 position = turtle_rect = turtle.get_rect()
  91.                 #改变乌龟(0,0)的位置
  92.                 position.left = width- turtle_rect.width
  93.                 speed =[0,5]
  94.                 #之后同理
  95.             if position.bottom > height:
  96.                 turtle = turtle_bottom
  97.                 position = turtle_rect = turtle.get_rect()
  98.                 position.left = width - turtle_rect.width
  99.                 position.top = height - turtle_rect.height
  100.                 speed = [-5, 0]

  101.             if position.left < 0:
  102.                 turtle = turtle_left
  103.                 position = turtle_rect = turtle.get_rect()
  104.                 position.top = height - turtle_rect.height
  105.                 speed = [0, -5]

  106.             if position.top < 0:
  107.                 turtle = turtle_top
  108.                 position = turtle_rect = turtle.get_rect()
  109.                 speed = [5, 0]
  110.                     
  111.         if event.type == VIDEORESIZE:
  112.             size = event.size
  113.             width,height = size
  114.             print(size)
  115.             screen = pygame.display.set_mode(size,RESIZABLE)

  116.             
  117.     position = position.move(speed)
  118.     #调用rect对象的move方法

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

  123.     if position.top < 0 or position.bottom > height:
  124.         speed[1] = -speed[1]
  125.             

  126.     #填充背景(看不到乌龟了)
  127.     screen.fill(bg)
  128.     #更新图像,但是每刻都是一个图像(只有像素)
  129.     screen.blit(turtle,position)
  130.     #双缓冲flip更新界面,重要!!!
  131.     pygame.display.flip()
  132.     #延迟10毫秒
  133.     pygame.time.delay(10)
  134.     clock.tick(200)
  135.     #不高于200帧
复制代码


另外,能否设置一个按键,将乌龟绕着屏幕边界走和碰到边界反弹的效果区分开?
最佳答案
2021-9-12 23:43:55
本帖最后由 blahblahfc 于 2021-9-13 18:24 编辑

原因是一开始 position.left 的值是 0,speed[0] 的值是 -2,向左运动。

第150行满足条件:
  1.         if position.left <= 0:
复制代码

执行 speed[0] = -speed[0] ,之后 speed[0] 变成 2,向右运动。
第150行又满足条件,speed[0] 不断取反,乌龟的水平位置在 -2 和 0 来回抖动

只要把 if position.left <= 0: 改为 if position.left < 0: 应该就正常了。


另外第96行至104行改为:
  1.                 l_head = turtle = pygame.transform.smoothscale(oturtle,\
  2.                                                                (int(oturtle_rect.width * ratio),\
  3.                                                                 int(oturtle_rect.height * ratio)))
  4.                 r_head = pygame.transform.flip(turtle,True,False)
  5.                
  6.                 if dir_x ==1:
  7.                     turtle = r_head
  8.                
  9.                 position.size = turtle.get_rect().size
复制代码


改变大小后更新位置信息

最佳答案

查看完整内容

原因是一开始 position.left 的值是 0,speed[0] 的值是 -2,向左运动。 第150行满足条件: 执行 speed[0] = -speed[0] ,之后 speed[0] 变成 2,向右运动。 第150行又满足条件,speed[0] 不断取反,乌龟的水平位置在 -2 和 0 来回抖动 只要把 if position.left
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-9-12 23:43:55 | 显示全部楼层    本楼为最佳答案   
本帖最后由 blahblahfc 于 2021-9-13 18:24 编辑

原因是一开始 position.left 的值是 0,speed[0] 的值是 -2,向左运动。

第150行满足条件:
  1.         if position.left <= 0:
复制代码

执行 speed[0] = -speed[0] ,之后 speed[0] 变成 2,向右运动。
第150行又满足条件,speed[0] 不断取反,乌龟的水平位置在 -2 和 0 来回抖动

只要把 if position.left <= 0: 改为 if position.left < 0: 应该就正常了。


另外第96行至104行改为:
  1.                 l_head = turtle = pygame.transform.smoothscale(oturtle,\
  2.                                                                (int(oturtle_rect.width * ratio),\
  3.                                                                 int(oturtle_rect.height * ratio)))
  4.                 r_head = pygame.transform.flip(turtle,True,False)
  5.                
  6.                 if dir_x ==1:
  7.                     turtle = r_head
  8.                
  9.                 position.size = turtle.get_rect().size
复制代码


改变大小后更新位置信息
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-9-13 09:00:28 | 显示全部楼层
为什么先得按下方向键才可以

先看第9行,乌龟初始速度设为 [-2,1],再看第129至138行,不按下方向键就是斜向碰壁反弹的效果。

能否设置一个按键,将乌龟绕着屏幕边界走和碰到边界反弹的效果区分开

可以设置一个 around_mode 保存状态,同时要保存之前的速度,位置和朝向等。

在第40行加上:
  1. around_mode = False
  2. speed_0 = speed
  3. position_0 = position
  4. turtle_0 = turtle
复制代码


再将第45行至55行方向键加上 around_mode 的条件,并加上 f 键切换模式:
  1.             if event.key == K_f:
  2.                 if around_mode:
  3.                     speed = speed_0
  4.                     position = position_0
  5.                     turtle = turtle_0
  6.                 else:
  7.                     speed_0 = speed
  8.                     position_0 = position
  9.                     turtle_0 = turtle
  10.                 around_mode = not around_mode
  11.                
  12.             if not around_mode:
  13.                 if event.key == K_LEFT:
  14.                     turtle = l_head
  15.                     speed = [-2,0]
  16.                 elif event.key == K_RIGHT:
  17.                     turtle = r_head
  18.                     speed = [2,0]
  19.                 elif event.key == K_UP:
  20.                     speed = [0,-1]
  21.                 elif event.key == K_DOWN:
  22.                     speed = [0,1]
复制代码


再将第95行至120行移动到下面132行的的位置,删去 pressed_keys 变量,
改为判断 around_mode,同时在标题栏上显示状态:
  1.     if around_mode:
  2.         pygame.display.set_caption('初次见面,请大家多多关照 (环绕模式: 开启)')
  3.         if position.right > width:
  4.             turtle = turtle_right
  5.             position = turtle_rect = turtle.get_rect()
  6.             #改变乌龟(0,0)的位置
  7.             position.left = width- turtle_rect.width
  8.             speed =[0,5]
  9.             #之后同理
  10.         if position.bottom > height:
  11.             turtle = turtle_bottom
  12.             position = turtle_rect = turtle.get_rect()
  13.             position.left = width - turtle_rect.width
  14.             position.top = height - turtle_rect.height
  15.             speed = [-5, 0]

  16.         if position.left < 0:
  17.             turtle = turtle_left
  18.             position = turtle_rect = turtle.get_rect()
  19.             position.top = height - turtle_rect.height
  20.             speed = [0, -5]

  21.         if position.top < 0:
  22.             turtle = turtle_top
  23.             position = turtle_rect = turtle.get_rect()
  24.             speed = [5, 0]
  25.    
  26.     else:
  27.         # 方向键控制
  28.         pygame.display.set_caption('初次见面,请大家多多关照 (环绕模式: 关闭)')
  29.         if position.left < 0 or position.right > width:
  30.             turtle = pygame.transform.flip(turtle,True,False)
  31.             #第一个参数是surface对象,第二个是水平翻转,第三个是竖直翻转
  32.             speed[0] = -speed[0]

  33.         if position.top < 0 or position.bottom > height:
  34.             speed[1] = -speed[1]
复制代码


完整的代码如下:
  1. import pygame
  2. import sys
  3. from pygame.locals import *

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

  6. size = width,height = 1500,700
  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').convert_alpha()
  20. turtle = oturtle
  21. oturtle_rect = oturtle.get_rect()
  22. position = turtle_rect = oturtle_rect  
  23. #获得图片位置矩形
  24. #position = turtle.get_rect()
  25. ol_head = l_head = turtle
  26. or_head = r_head = pygame.transform.flip(turtle,True,False)
  27. #自动翻转

  28. #rotate方向为逆时针
  29. turtle_right = pygame.transform.rotate(oturtle,90)
  30. turtle_top = pygame.transform.rotate(oturtle,180)
  31. turtle_left = pygame.transform.rotate(oturtle,270)
  32. turtle_bottom = oturtle

  33. # 环绕模式
  34. around_mode = False
  35. speed_0 = speed
  36. position_0 = position
  37. turtle_0 = turtle

  38. while True:
  39.     for event in pygame.event.get():
  40.         if event.type == pygame.QUIT:
  41.             sys.exit()
  42.         if event.type == pygame.KEYDOWN:
  43.             # f 键切换环绕模式
  44.             if event.key == K_f:
  45.                 if around_mode:
  46.                     speed = speed_0
  47.                     position = position_0
  48.                     turtle = turtle_0
  49.                 else:
  50.                     speed_0 = speed
  51.                     position_0 = position
  52.                     turtle_0 = turtle
  53.                 around_mode = not around_mode
  54.                
  55.             # 非环绕模式,方向键有效
  56.             if not around_mode:
  57.                 if event.key == K_LEFT:
  58.                     turtle = l_head
  59.                     speed = [-2,0]
  60.                 elif event.key == K_RIGHT:
  61.                     turtle = r_head
  62.                     speed = [2,0]
  63.                 elif event.key == K_UP:
  64.                     speed = [0,-1]
  65.                 elif event.key == K_DOWN:
  66.                     speed = [0,1]

  67.             #全屏a
  68.             if event.key == K_a:
  69.                 fullscreen = not fullscreen
  70.                 if fullscreen:
  71.                     screen = pygame.display.set_mode((3840, 2160),FULLSCREEN | HWSURFACE)
  72.                     width,height = 1920,1080
  73.                     #改尺寸
  74.                 else:
  75.                     screen = pygame.display.set_mode(size)
  76.             #放大缩小乌龟(=,-),空格键恢复尺寸
  77.             if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
  78.                 if event.key == K_EQUALS and ratio < 1.4:
  79.                     ratio += 0.1
  80.                 if event.key == K_MINUS and ratio > 0.5:
  81.                     ratio -= 0.1
  82.                 if event.key == K_SPACE:
  83.                     ratio = 1.0
  84.                 #此时注意乌龟大小会变
  85.                 if turtle == l_head:
  86.                     oturtle = ol_head
  87.                     oturtle_rect = oturtle.get_rect()
  88.                     l_head = turtle = pygame.transform.smoothscale(oturtle,\
  89.                                              (int(oturtle_rect.width * ratio),\
  90.                                               int(oturtle_rect.height * ratio)))
  91.                     r_head = pygame.transform.flip(turtle,True,False)
  92.                     
  93.                 if turtle == r_head:
  94.                     oturtle = or_head
  95.                     oturtle_rect = oturtle.get_rect()
  96.                     r_head = turtle = pygame.transform.smoothscale(oturtle,\
  97.                                              (int(oturtle_rect.width * ratio),\
  98.                                               int(oturtle_rect.height * ratio)))
  99.                     l_head = pygame.transform.flip(turtle,True,False)
  100.                     
  101.                
  102.                 #也同时防止变量被污染
  103.                 #放大缩小的头重新移动
  104.                 #按住进入绕边走模式 (代码移动到下面)
  105.                
  106.         if event.type == VIDEORESIZE:
  107.             size = event.size
  108.             width,height = size
  109.             print(size)
  110.             screen = pygame.display.set_mode(size,RESIZABLE)

  111.     position = position.move(speed)
  112.     #调用rect对象的move方法

  113.     # 环绕模式
  114.     if around_mode:
  115.         pygame.display.set_caption('初次见面,请大家多多关照 (环绕模式: 开启)')
  116.         if position.right > width:
  117.             turtle = turtle_right
  118.             position = turtle_rect = turtle.get_rect()
  119.             #改变乌龟(0,0)的位置
  120.             position.left = width- turtle_rect.width
  121.             speed =[0,5]
  122.             #之后同理
  123.         if position.bottom > height:
  124.             turtle = turtle_bottom
  125.             position = turtle_rect = turtle.get_rect()
  126.             position.left = width - turtle_rect.width
  127.             position.top = height - turtle_rect.height
  128.             speed = [-5, 0]

  129.         if position.left < 0:
  130.             turtle = turtle_left
  131.             position = turtle_rect = turtle.get_rect()
  132.             position.top = height - turtle_rect.height
  133.             speed = [0, -5]

  134.         if position.top < 0:
  135.             turtle = turtle_top
  136.             position = turtle_rect = turtle.get_rect()
  137.             speed = [5, 0]
  138.    
  139.     else:
  140.         # 方向键控制
  141.         pygame.display.set_caption('初次见面,请大家多多关照 (环绕模式: 关闭)')
  142.         if position.left < 0 or position.right > width:
  143.             turtle = pygame.transform.flip(turtle,True,False)
  144.             #第一个参数是surface对象,第二个是水平翻转,第三个是竖直翻转
  145.             speed[0] = -speed[0]

  146.         if position.top < 0 or position.bottom > height:
  147.             speed[1] = -speed[1]
  148.                                 
  149.    
  150.     #填充背景(看不到乌龟了)
  151.     screen.fill(bg)
  152.     #更新图像,但是每刻都是一个图像(只有像素)
  153.     screen.blit(turtle,position)
  154.     #双缓冲flip更新界面,重要!!!
  155.     pygame.display.flip()
  156.     #延迟10毫秒
  157.     pygame.time.delay(10)
  158.     clock.tick(200)
  159.     #不高于200帧
复制代码

评分

参与人数 1鱼币 +5 收起 理由
飞花落尽 + 5 鱼C有你更精彩^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-9-13 09:58:07 | 显示全部楼层
本帖最后由 飞花落尽 于 2021-9-13 10:03 编辑
blahblahfc 发表于 2021-9-13 09:00
先看第9行,乌龟初始速度设为 [-2,1],再看第129至138行,不按下方向键就是斜向碰壁反弹的效果。


谢谢对第二个问题的解答,但是第一个问题我想问的是为什么要先按下方向键后才可以放大缩小乌龟?
(不知道为什么没打全)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-9-13 10:03:02 | 显示全部楼层
blahblahfc 发表于 2021-9-13 09:00
先看第9行,乌龟初始速度设为 [-2,1],再看第129至138行,不按下方向键就是斜向碰壁反弹的效果。

还有放大缩小后新的乌龟好像会在新的边界返回?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-9-13 14:17:49 | 显示全部楼层
为什么要先按下方向键后才可以放大缩小乌龟?

这个功能我这里不能复现,一直都不能放大和缩小乌龟。

原因是第75行
  1.                 if turtle == l_head:
复制代码


和第83行
  1.                 if turtle == r_head:
复制代码


if 的条件始终为假

开头定义的 oturtle, turtle, ol_head, l_head, or_head, r_head 的类型都是 Surface,
在进行 pygame.transform.flip 或者 pygame.transform.rotate 等操作之后,其结果就不是同一个 Surface 对象

建议用标志状态(int 或者 bool)来判断朝向,dir_x = 0
再将 if turtle == r_head: 之类的改为 if dir_x == 1: 之类的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-9-13 15:38:05 | 显示全部楼层
blahblahfc 发表于 2021-9-13 14:17
这个功能我这里不能复现,一直都不能放大和缩小乌龟。

原因是第75行

我换成这样好像更不行了不知道哪里出问题了
  1. import pygame
  2. import sys
  3. from pygame.locals import *

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

  6. size = width,height = 1500,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').convert_alpha()
  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. dir_x = 1
  28. #自动翻转

  29. #rotate方向为逆时针
  30. turtle_right = pygame.transform.rotate(oturtle,90)
  31. turtle_top = pygame.transform.rotate(oturtle,180)
  32. turtle_left = pygame.transform.rotate(oturtle,270)
  33. turtle_bottom = oturtle

  34. # 环绕模式
  35. around_mode = False
  36. speed_0 = speed
  37. position_0 = position
  38. turtle_0 = turtle

  39. while True:
  40.     for event in pygame.event.get():
  41.         if event.type == pygame.QUIT:
  42.             sys.exit()
  43.         if event.type == pygame.KEYDOWN:
  44.             # f 键切换环绕模式
  45.             if event.key == K_f:
  46.                 if around_mode:
  47.                     speed = speed_0
  48.                     position = position_0
  49.                     turtle = turtle_0
  50.                 else:
  51.                     speed_0 = speed
  52.                     position_0 = position
  53.                     turtle_0 = turtle
  54.                 around_mode = not around_mode
  55.                
  56.             # 非环绕模式,方向键有效
  57.             if not around_mode:
  58.                 if event.key == K_LEFT:
  59.                     turtle = l_head
  60.                     speed = [-2,0]
  61.                 elif event.key == K_RIGHT:
  62.                     turtle = r_head
  63.                     speed = [2,0]
  64.                 elif event.key == K_UP:
  65.                     speed = [0,-1]
  66.                 elif event.key == K_DOWN:
  67.                     speed = [0,1]

  68.             #全屏a
  69.             if event.key == K_a:
  70.                 fullscreen = not fullscreen
  71.                 if fullscreen:
  72.                     screen = pygame.display.set_mode((3840, 2160),FULLSCREEN | HWSURFACE)
  73.                     width,height = 1920,1080
  74.                     #改尺寸
  75.                 else:
  76.                     screen = pygame.display.set_mode(size)
  77.             #放大缩小乌龟(=,-),空格键恢复尺寸
  78.             if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
  79.                 if event.key == K_EQUALS and ratio < 1.4:
  80.                     ratio += 0.1
  81.                 if event.key == K_MINUS and ratio > 0.5:
  82.                     ratio -= 0.1
  83.                 if event.key == K_SPACE:
  84.                     ratio = 1.0
  85.                 #此时注意乌龟大小会变
  86.                 if dir_x == 0:
  87.                     turtle = pygame.transform.smoothscale(oturtle,\
  88.                                         (int(oturtle_rect.width * ratio),\
  89.                                         int(oturtle_rect.height * ratio)))
  90.                     
  91.                 if dir_x == 1:
  92.                     turtle = pygame.transform.smoothscale(r_head,\
  93.                                              (int(oturtle_rect.width * ratio),\
  94.                                               int(oturtle_rect.height * ratio)))
  95.                
  96.                 #也同时防止变量被污染
  97.                 #放大缩小的头重新移动
  98.                 #按住进入绕边走模式 (代码移动到下面)
  99.                
  100.         if event.type == VIDEORESIZE:
  101.             size = event.size
  102.             width,height = size
  103.             print(size)
  104.             screen = pygame.display.set_mode(size,RESIZABLE)

  105.     position = position.move(speed)
  106.     #调用rect对象的move方法

  107.     # 环绕模式
  108.     if around_mode:
  109.         pygame.display.set_caption('初次见面,请大家多多关照 (环绕模式: 开启)')
  110.         if position.right > width:
  111.             turtle = turtle_right
  112.             position = turtle_rect = turtle.get_rect()
  113.             #改变乌龟(0,0)的位置
  114.             position.left = width- turtle_rect.width
  115.             speed =[0,5]
  116.             #之后同理
  117.         if position.bottom > height:
  118.             turtle = turtle_bottom
  119.             position = turtle_rect = turtle.get_rect()
  120.             position.left = width - turtle_rect.width
  121.             position.top = height - turtle_rect.height
  122.             speed = [-5, 0]

  123.         if position.left < 0:
  124.             turtle = turtle_left
  125.             position = turtle_rect = turtle.get_rect()
  126.             position.top = height - turtle_rect.height
  127.             speed = [0, -5]

  128.         if position.top < 0:
  129.             turtle = turtle_top
  130.             position = turtle_rect = turtle.get_rect()
  131.             speed = [5, 0]
  132.    
  133.     else:
  134.         # 方向键控制
  135.         pygame.display.set_caption('初次见面,请大家多多关照 (环绕模式: 关闭)')
  136.         if position.left <= 0:
  137.             turtle = pygame.transform.flip(turtle,True,False)
  138.             #第一个参数是surface对象,第二个是水平翻转,第三个是竖直翻转
  139.             speed[0] = -speed[0]
  140.             dir_x = 1

  141.         if position.right > width:
  142.             turtle = pygame.transform.flip(turtle,True,False)
  143.             #第一个参数是surface对象,第二个是水平翻转,第三个是竖直翻转
  144.             speed[0] = -speed[0]
  145.             dir_x = 0

  146.         if position.top < 0 or position.bottom > height:
  147.             speed[1] = -speed[1]
  148.                                 
  149.    
  150.     #填充背景(看不到乌龟了)
  151.     screen.fill(bg)
  152.     #更新图像,但是每刻都是一个图像(只有像素)
  153.     screen.blit(turtle,position)
  154.     #双缓冲flip更新界面,重要!!!
  155.     pygame.display.flip()
  156.     #延迟10毫秒
  157.     pygame.time.delay(10)
  158.     clock.tick(200)
  159.     #不高于200帧
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-9-13 18:49:16 | 显示全部楼层
blahblahfc 发表于 2021-9-13 18:05
原因是一开始 position.left 的值是 0,speed[0] 的值是 -2,向左运动。

第150行满足条件:

好像还是有朝向问题(放大缩小的时候只有一边
  1. import pygame
  2. import sys
  3. from pygame.locals import *

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

  6. size = width,height = 1500,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').convert_alpha()
  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. dir_x = 1
  28. #自动翻转

  29. #rotate方向为逆时针
  30. turtle_right = pygame.transform.rotate(oturtle,90)
  31. turtle_top = pygame.transform.rotate(oturtle,180)
  32. turtle_left = pygame.transform.rotate(oturtle,270)
  33. turtle_bottom = oturtle

  34. # 环绕模式
  35. around_mode = False
  36. speed_0 = speed
  37. position_0 = position
  38. turtle_0 = turtle

  39. while True:
  40.     for event in pygame.event.get():
  41.         if event.type == pygame.QUIT:
  42.             sys.exit()
  43.         if event.type == pygame.KEYDOWN:
  44.             # f 键切换环绕模式
  45.             if event.key == K_f:
  46.                 if around_mode:
  47.                     speed = speed_0
  48.                     position = position_0
  49.                     turtle = turtle_0
  50.                 else:
  51.                     speed_0 = speed
  52.                     position_0 = position
  53.                     turtle_0 = turtle
  54.                 around_mode = not around_mode
  55.                
  56.             # 非环绕模式,方向键有效
  57.             if not around_mode:
  58.                 if event.key == K_LEFT:
  59.                     turtle = l_head
  60.                     speed = [-2,0]
  61.                 elif event.key == K_RIGHT:
  62.                     turtle = r_head
  63.                     speed = [2,0]
  64.                 elif event.key == K_UP:
  65.                     speed = [0,-1]
  66.                 elif event.key == K_DOWN:
  67.                     speed = [0,1]

  68.             #全屏a
  69.             if event.key == K_a:
  70.                 fullscreen = not fullscreen
  71.                 if fullscreen:
  72.                     screen = pygame.display.set_mode((3840, 2160),FULLSCREEN | HWSURFACE)
  73.                     width,height = 1920,1080
  74.                     #改尺寸
  75.                 else:
  76.                     screen = pygame.display.set_mode(size)
  77.             #放大缩小乌龟(=,-),空格键恢复尺寸
  78.             if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
  79.                 if event.key == K_EQUALS and ratio < 1.4:
  80.                     ratio += 0.1
  81.                 if event.key == K_MINUS and ratio > 0.5:
  82.                     ratio -= 0.1
  83.                 if event.key == K_SPACE:
  84.                     ratio = 1.0
  85.                 #此时注意乌龟大小会变(下面是错误示范)
  86.                 #if dir_x == 0:
  87.                     #turtle = pygame.transform.smoothscale(oturtle,\
  88.                                         #(int(oturtle_rect.width * ratio),\
  89.                                         #int(oturtle_rect.height * ratio)))
  90.                     
  91.                 #if dir_x == 1:
  92.                     #turtle = pygame.transform.smoothscale(r_head,\
  93.                                              #(int(oturtle_rect.width * ratio),\
  94.                                               #int(oturtle_rect.height * ratio)))
  95.                     
  96.                 l_head = turtle = pygame.transform.smoothscale(oturtle,\
  97.                                         (int(oturtle_rect.width * ratio),\
  98.                                         int(oturtle_rect.height * ratio)))
  99.                 r_head = pygame.transform.flip(turtle,True,False)
  100.                
  101.                 if dir_x == 1:
  102.                     turtle = r_head
  103.                
  104.                 position.size = turtle.get_rect().size
  105.                 #也同时防止变量被污染
  106.                 #放大缩小的头重新移动
  107.                 #按住进入绕边走模式 (代码移动到下面)
  108.                
  109.         if event.type == VIDEORESIZE:
  110.             size = event.size
  111.             width,height = size
  112.             print(size)
  113.             screen = pygame.display.set_mode(size,RESIZABLE)

  114.     position = position.move(speed)
  115.     #调用rect对象的move方法

  116.     # 环绕模式
  117.     if around_mode:
  118.         pygame.display.set_caption('初次见面,请大家多多关照 (环绕模式: 开启)')
  119.         if position.right > width:
  120.             turtle = turtle_right
  121.             position = turtle_rect = turtle.get_rect()
  122.             #改变乌龟(0,0)的位置
  123.             position.left = width- turtle_rect.width
  124.             speed =[0,5]
  125.             #之后同理
  126.         if position.bottom > height:
  127.             turtle = turtle_bottom
  128.             position = turtle_rect = turtle.get_rect()
  129.             position.left = width - turtle_rect.width
  130.             position.top = height - turtle_rect.height
  131.             speed = [-5, 0]

  132.         if position.left < 0:
  133.             turtle = turtle_left
  134.             position = turtle_rect = turtle.get_rect()
  135.             position.top = height - turtle_rect.height
  136.             speed = [0, -5]

  137.         if position.top < 0:
  138.             turtle = turtle_top
  139.             position = turtle_rect = turtle.get_rect()
  140.             speed = [5, 0]
  141.    
  142.     else:
  143.         # 方向键控制
  144.         pygame.display.set_caption('初次见面,请大家多多关照 (环绕模式: 关闭)')
  145.         if position.left < 0:
  146.             turtle = pygame.transform.flip(turtle,True,False)
  147.             #第一个参数是surface对象,第二个是水平翻转,第三个是竖直翻转
  148.             speed[0] = -speed[0]
  149.             dir_x = 1

  150.         if position.right > width:
  151.             turtle = pygame.transform.flip(turtle,True,False)
  152.             #第一个参数是surface对象,第二个是水平翻转,第三个是竖直翻转
  153.             speed[0] = -speed[0]
  154.             dir_x = 0

  155.         if position.top < 0 or position.bottom > height:
  156.             speed[1] = -speed[1]
  157.                                 
  158.    
  159.     #填充背景(看不到乌龟了)
  160.     screen.fill(bg)
  161.     #更新图像,但是每刻都是一个图像(只有像素)
  162.     screen.blit(turtle,position)
  163.     #双缓冲flip更新界面,重要!!!
  164.     pygame.display.flip()
  165.     #延迟10毫秒
  166.     pygame.time.delay(10)
  167.     clock.tick(200)
  168.     #不高于200帧
复制代码
)?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-9-13 18:53:13 | 显示全部楼层
blahblahfc 发表于 2021-9-13 18:05
原因是一开始 position.left 的值是 0,speed[0] 的值是 -2,向左运动。

第150行满足条件:

没事了,方向键的那里忘记加了,谢谢大神
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-29 00:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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