鱼C论坛

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

[已解决]小乌龟移动的代码的bug真不少……

[复制链接]
发表于 2022-6-14 11:16:59 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 lzb1001 于 2022-6-14 11:31 编辑
  1. import pygame
  2. import sys
  3. from pygame.locals import *

  4. pygame.init()

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

  8. fullscreen =  False

  9. screen = pygame.display.set_mode(siz
  10. pygame.display.set_caption('Hello!初次见面,请大家多多关照~~')

  11. turtle = pygame.image.load(r'd:\\work\\turtle_left.png')
  12. position = turtle.get_rect()

  13. l_head = turtle
  14. r_head = pygame.transform.flip(turtle, True, False)

  15. while True:
  16.     for event in pygame.event.get():
  17.         if event.type == pygame.QUIT:
  18.             sys.exit()

  19.         if event.type == KEYDOWN:
  20.             if event.key == K_LEFT:
  21.                 turtle = l_head
  22.                 speed = [-1, 0]            
  23.             if event.key == K_RIGHT:
  24.                 turtle = r_head
  25.                 speed = [1, 0]
  26.                 #speed = [2, 1]
  27.             if event.key == K_UP:
  28.                 speed = [0, -1]
  29.             if event.key == K_DOWN:
  30.                 speed = [0, 1]

  31.             if event.key == K_F11:
  32.                 fullscreen = not fullscreen
  33.                 if fullscreen:
  34.                     screen = pygame.display.set_mode((1366, 768), FULLSCREEN | HWSURFACE)
  35.                 else:
  36.                     screen = pygame.display.set_mode(size)
  37.   
  38.     position = position.move(speed)
  39.    
  40.     if position.left < 0 or position.right > width:
  41.         turtle = pygame.transform.flip(turtle, True, False)
  42.         speed[0] = -speed[0]

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

  45.     screen.fill(bg)

  46.     screen.blit(turtle, position)
  47.    
  48.     pygame.display.flip()
  49.    
  50.     pygame.time.delay(20)
复制代码


------------------------------------------
Windows 10 专业版 | Python 3.7.6
------------------------------------------

【我的问题】

以上代码完全按照小甲鱼的教学视频敲的,但运行后发现bug已知的就至少有以下:

1、用左右方向键控制小乌龟后再让它自由移动,小乌龟就只能左右移动而不像之前那样可以走斜线

2、全屏后,小乌龟的移动范围并没有实际扩大

3、从全屏切换为普通窗口模式后发现也不是原来定义的窗口大小

4、……

******************************

感谢大神不吝赐教,为新手解疑释惑。

赠人玫瑰,手有余香,好人一生平安!
最佳答案
2022-6-14 11:42:20
本帖最后由 白two 于 2022-6-14 11:51 编辑

1、用左右方向键控制小乌龟后再让它自由移动,小乌龟就只能左右移动而不像之前那样可以走斜线: 你按了左右键后改的肯定是整个速度列表, 只需要改横向速度就可以了, 就像这样 speed[0] = -speed[0]
2、全屏后,小乌龟的移动范围并没有实际扩大: 我猜测你可能只是扩大了屏幕, 但是没有扩大 size 的两个值, 扩大后重新获取一下 size 就行, 推荐自己设定 size 然后按照这个来最大化
3、从全屏切换为普通窗口模式后发现也不是原来定义的窗口大小: 同上, 重新设定 size 列表, 在设定屏幕大小


以下附上源码, 以前练手之作, 有些冗余, 将就着看吧
比如按左,就可以 speed[0] = -1, 就会省掉很多代码
  1. import pygame
  2. import sys
  3. from pygame.constants import RESIZABLE, VIDEORESIZE


  4. def change_pos(t_position, t_width, t_height):
  5.     if t_position.right > t_width:
  6.         t_position.right = t_width
  7.                     
  8.     if t_position.bottom > t_height:
  9.         t_position.bottom = t_height


  10. pygame.init()
  11. # size = width, height = (600, 400)
  12. size = width, height = (1536, 864)
  13. speed = [-2, 1]
  14. toward = [True, False]
  15. updown = [False, True]
  16. bg = (255, 255, 255)
  17. clock = pygame.time.Clock()
  18. screen = pygame.display.set_mode(size, RESIZABLE)
  19. pygame.display.set_caption("初次见面,请多多关照!")
  20. # post the turtle
  21. turtle = pygame.image.load(r"turtle.jpeg")
  22. position = turtle.get_rect()
  23. fullscreen = False
  24. while True:
  25.     for event in pygame.event.get():
  26.         if event.type == pygame.QUIT:
  27.             sys.exit()
  28.         
  29.         if event.type == VIDEORESIZE:
  30.             if event.size[0] < position.width:
  31.                 event.size = (position.width, event.size[1])
  32.                 size = event.size
  33.                 width, height = size
  34.                 screen = pygame.display.set_mode(size, RESIZABLE)
  35.                 change_pos(position, width, height)
  36.             
  37.             if event.size[1] < position.height:
  38.                 event.size = (event.size[0], position.height)
  39.                 size = event.size
  40.                 width, height = size
  41.                 screen = pygame.display.set_mode(size, RESIZABLE)
  42.                 change_pos(position, width, height)
  43.             else:   
  44.                 size = event.size
  45.                 width, height = size
  46.                 screen = pygame.display.set_mode(size, RESIZABLE)
  47.                 change_pos(position, width, height)

  48.         if event.type == pygame.KEYDOWN:
  49.             if position.left > 0 and (event.key == pygame.K_a or event.key == pygame.K_LEFT):
  50.                 if not toward[0]:
  51.                     toward[0] = True
  52.                     toward[1] = False
  53.                     turtle = pygame.transform.flip(turtle, True, False)
  54.                     speed[0] = -speed[0]
  55.             
  56.             if position.right < width and (event.key == pygame.K_d or event.key == pygame.K_RIGHT):
  57.                 if not toward[1]:
  58.                     toward[1] = True
  59.                     toward[0] = False
  60.                     turtle = pygame.transform.flip(turtle, True, False)
  61.                     speed[0] = -speed[0]
  62.             
  63.             if position.top > 0 and (event.key == pygame.K_w or event.key == pygame.K_UP):
  64.                 if not updown[0]:
  65.                     updown[0] = not updown[0]
  66.                     updown[1] = not updown[1]
  67.                     speed[1] = -speed[1]
  68.             
  69.             if position.bottom < height and (event.key == pygame.K_s or event.key == pygame.K_DOWN):
  70.                 if not updown[1]:
  71.                     updown[0] = not updown[0]
  72.                     updown[1] = not updown[1]
  73.                     speed[1] = -speed[1]
  74.             
  75.             if event.key == pygame.K_F11:
  76.                 fullscreen = not fullscreen
  77.                 if fullscreen:
  78.                     # size = width, height = pygame.display.list_modes()[0]
  79.                     size = width, height = (1536, 864)
  80.                     screen = pygame.display.set_mode(size, pygame.FULLSCREEN | pygame.HWSURFACE)

  81.                 else:
  82.                     size = width, height = (600, 400)
  83.                     screen = pygame.display.set_mode(size)
  84.                     change_pos(position, width, height)

  85.     position = position.move(speed)
  86.     if position.left < 0 or position.right > width:
  87.         turtle = pygame.transform.flip(turtle, True, False)
  88.         toward[0] = not toward[0]
  89.         toward[1] = not toward[1]
  90.         speed[0] = -speed[0]
  91.     if position.top < 0 or position.bottom > height:
  92.         updown[0] = not updown[0]
  93.         updown[1] = not updown[1]
  94.         speed[1] = -speed[1]

  95.     screen.fill(bg)
  96.     screen.blit(turtle, position)
  97.     pygame.display.flip()
  98.     # pygame.time.delay(5)
  99.     clock.tick(200)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-6-14 11:42:20 | 显示全部楼层    本楼为最佳答案   
本帖最后由 白two 于 2022-6-14 11:51 编辑

1、用左右方向键控制小乌龟后再让它自由移动,小乌龟就只能左右移动而不像之前那样可以走斜线: 你按了左右键后改的肯定是整个速度列表, 只需要改横向速度就可以了, 就像这样 speed[0] = -speed[0]
2、全屏后,小乌龟的移动范围并没有实际扩大: 我猜测你可能只是扩大了屏幕, 但是没有扩大 size 的两个值, 扩大后重新获取一下 size 就行, 推荐自己设定 size 然后按照这个来最大化
3、从全屏切换为普通窗口模式后发现也不是原来定义的窗口大小: 同上, 重新设定 size 列表, 在设定屏幕大小


以下附上源码, 以前练手之作, 有些冗余, 将就着看吧
比如按左,就可以 speed[0] = -1, 就会省掉很多代码
  1. import pygame
  2. import sys
  3. from pygame.constants import RESIZABLE, VIDEORESIZE


  4. def change_pos(t_position, t_width, t_height):
  5.     if t_position.right > t_width:
  6.         t_position.right = t_width
  7.                     
  8.     if t_position.bottom > t_height:
  9.         t_position.bottom = t_height


  10. pygame.init()
  11. # size = width, height = (600, 400)
  12. size = width, height = (1536, 864)
  13. speed = [-2, 1]
  14. toward = [True, False]
  15. updown = [False, True]
  16. bg = (255, 255, 255)
  17. clock = pygame.time.Clock()
  18. screen = pygame.display.set_mode(size, RESIZABLE)
  19. pygame.display.set_caption("初次见面,请多多关照!")
  20. # post the turtle
  21. turtle = pygame.image.load(r"turtle.jpeg")
  22. position = turtle.get_rect()
  23. fullscreen = False
  24. while True:
  25.     for event in pygame.event.get():
  26.         if event.type == pygame.QUIT:
  27.             sys.exit()
  28.         
  29.         if event.type == VIDEORESIZE:
  30.             if event.size[0] < position.width:
  31.                 event.size = (position.width, event.size[1])
  32.                 size = event.size
  33.                 width, height = size
  34.                 screen = pygame.display.set_mode(size, RESIZABLE)
  35.                 change_pos(position, width, height)
  36.             
  37.             if event.size[1] < position.height:
  38.                 event.size = (event.size[0], position.height)
  39.                 size = event.size
  40.                 width, height = size
  41.                 screen = pygame.display.set_mode(size, RESIZABLE)
  42.                 change_pos(position, width, height)
  43.             else:   
  44.                 size = event.size
  45.                 width, height = size
  46.                 screen = pygame.display.set_mode(size, RESIZABLE)
  47.                 change_pos(position, width, height)

  48.         if event.type == pygame.KEYDOWN:
  49.             if position.left > 0 and (event.key == pygame.K_a or event.key == pygame.K_LEFT):
  50.                 if not toward[0]:
  51.                     toward[0] = True
  52.                     toward[1] = False
  53.                     turtle = pygame.transform.flip(turtle, True, False)
  54.                     speed[0] = -speed[0]
  55.             
  56.             if position.right < width and (event.key == pygame.K_d or event.key == pygame.K_RIGHT):
  57.                 if not toward[1]:
  58.                     toward[1] = True
  59.                     toward[0] = False
  60.                     turtle = pygame.transform.flip(turtle, True, False)
  61.                     speed[0] = -speed[0]
  62.             
  63.             if position.top > 0 and (event.key == pygame.K_w or event.key == pygame.K_UP):
  64.                 if not updown[0]:
  65.                     updown[0] = not updown[0]
  66.                     updown[1] = not updown[1]
  67.                     speed[1] = -speed[1]
  68.             
  69.             if position.bottom < height and (event.key == pygame.K_s or event.key == pygame.K_DOWN):
  70.                 if not updown[1]:
  71.                     updown[0] = not updown[0]
  72.                     updown[1] = not updown[1]
  73.                     speed[1] = -speed[1]
  74.             
  75.             if event.key == pygame.K_F11:
  76.                 fullscreen = not fullscreen
  77.                 if fullscreen:
  78.                     # size = width, height = pygame.display.list_modes()[0]
  79.                     size = width, height = (1536, 864)
  80.                     screen = pygame.display.set_mode(size, pygame.FULLSCREEN | pygame.HWSURFACE)

  81.                 else:
  82.                     size = width, height = (600, 400)
  83.                     screen = pygame.display.set_mode(size)
  84.                     change_pos(position, width, height)

  85.     position = position.move(speed)
  86.     if position.left < 0 or position.right > width:
  87.         turtle = pygame.transform.flip(turtle, True, False)
  88.         toward[0] = not toward[0]
  89.         toward[1] = not toward[1]
  90.         speed[0] = -speed[0]
  91.     if position.top < 0 or position.bottom > height:
  92.         updown[0] = not updown[0]
  93.         updown[1] = not updown[1]
  94.         speed[1] = -speed[1]

  95.     screen.fill(bg)
  96.     screen.blit(turtle, position)
  97.     pygame.display.flip()
  98.     # pygame.time.delay(5)
  99.     clock.tick(200)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-6-14 12:08:33 | 显示全部楼层
本帖最后由 lzb1001 于 2022-6-14 12:12 编辑
白two 发表于 2022-6-14 11:42
1、用左右方向键控制小乌龟后再让它自由移动,小乌龟就只能左右移动而不像之前那样可以走斜线: 你按了左右 ...


谢谢大神回复和指点。

刚用你的代码测试,发现还存在一个bug:

代码运行后,进入普通窗口模式-正常
按F11进入全屏显示模式-正常
再按F11恢复普通窗口模式-正常
再次按F11进入全屏显示模式-不正常:此时页面既不是全屏显示模式,也不是普通窗口模式
再次按F11恢复普通窗口模式-正常


建议大神按我上面说的自己测试下看看是否这样
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-6-14 13:30:18 | 显示全部楼层
lzb1001 发表于 2022-6-14 12:08
谢谢大神回复和指点。

刚用你的代码测试,发现还存在一个bug:

我是正常的
我感觉你说的情况是不是屏幕大小设置了,但是没有设置 FULLSCREEN
这好久以前写的,有些 bug 我也不清楚了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-6-14 13:52:25 | 显示全部楼层
本帖最后由 lzb1001 于 2022-6-14 17:42 编辑
白two 发表于 2022-6-14 13:30
我是正常的
我感觉你说的情况是不是屏幕大小设置了,但是没有设置 FULLSCREEN
这好久以前写的,有些 b ...


大神,你一开始就默认设置全屏模式,我刚试了这样全程没问题

但你试下一开始就默认设置普通窗口模式600*400再试下看看,应该就存在我说的问题,但这样又冒出另一个问题:初次点击Fn+F11好像没反应!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-6-14 23:11:34 | 显示全部楼层
lzb1001 发表于 2022-6-14 13:52
大神,你一开始就默认设置全屏模式,我刚试了这样全程没问题

但你试下一开始就默认设置普通窗口模式 ...

不清楚,我还是没问题,是不是版本的原因?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-4 14:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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