鱼C论坛

 找回密码
 立即注册
查看: 1547|回复: 3

[已解决]小甲鱼老教程82讲 pygame

[复制链接]
发表于 2022-5-21 17:12:38 | 显示全部楼层 |阅读模式

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

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

x
  1. import pygame
  2. import sys
  3. from pygame.locals import *

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

  6. size = width, height = 600, 400

  7. bg = (255, 255, 255) #RGB

  8. fullscreen = False

  9. # 指定窗口大小
  10. screen = pygame.display.set_mode(size, RESIZABLE)
  11. # 设置窗口标题
  12. pygame.display.set_caption("初次见面,请大家多多关照!")

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


  15. # 加载图片
  16. oturtle = pygame.image.load("2.jpeg")
  17. turtle = oturtle
  18. # 获得图像的位置矩形
  19. oturtle_rect = oturtle.get_rect()
  20. position = turtle_rect = oturtle_rect

  21. speed = [5, 0]
  22. turtle_right = pygame.transform.rotate(turtle, 90)
  23. turtle_top = pygame.transform.rotate(turtle, 180)
  24. turtle_left = pygame.transform.rotate(turtle, 270)
  25. turtle_buttom = turtle

  26. l_head = turtle
  27. r_head = pygame.transform.flip(turtle, True, False)

  28. while True:
  29.     for event in pygame.event.get():
  30.         if event.type == pygame.QUIT:
  31.             sys.exit()

  32.         if event.type == KEYDOWN:

  33.             # 全屏
  34.             if event.key == K_F11:
  35.                 fullscreen = not  fullscreen
  36.                 if fullscreen:
  37.                     screen = pygame.display.set_mode((1024, 768), FULLSCREEN | HWSURFACE)
  38.                     width, height = 1024, 768
  39.                 else:
  40.                     screen = pygame.display.set_mode(size)
  41.             # 放大、缩小小乌龟(=、-),空格键恢复原始尺寸
  42.             if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
  43.                 # 最大只能放大一倍,缩小50%
  44.                 if event.key == K_EQUALS and ratio < 2:
  45.                     ratio += 0.1
  46.                 if event.key == K_MINUS and ratio > 0.5:
  47.                     ratio -= 0.1
  48.                 if event.key  == K_SPACE:
  49.                     ratio = 1.0

  50.                 turtle = pygame.transform.smoothscale(oturtle, (int(oturtle_rect.width*ratio), int(oturtle_rect.height*ratio)))
  51.         # 用户调整窗口尺寸
  52.         if event.type == VIDEORESIZE:
  53.             size = event.size
  54.             width, height = size
  55.             print(size)
  56.             screen = pygame.display.set_mode(size, RESIZABLE)

  57.     # 图像移动
  58.     position = position.move(speed)

  59.     if position.right > width :
  60.         turtle = turtle_right
  61.         position = turtle_rect = turtle.get_rect()
  62.         position.left = width - turtle_rect.width
  63.         speed = [0, 5]

  64.     if position.bottom > height:
  65.         turtle = turtle_bottom
  66.         position = turtle_rect = turtle.get_rect()
  67.         position.left = width - turtle_rect.width
  68.         position.top = height - turtle_height
  69.         speed = [-5 , 0]

  70.     if position.left < 0:
  71.         turtle = turtle_left
  72.         position = turtle_rect = turtle.get_rect()
  73.         position.top = height - turtle_height
  74.         speed = [0, -5]

  75.     if position.top < 0:
  76.         turtle = turtle_top
  77.         position = turtle_rect = turtle. get_rect()
  78.         speed = [5, 0]

  79.     # 填充背景
  80.     screen.fill(bg)
  81.     # 更新图像
  82.     screen.blit(turtle, position)
  83.     # 更新界面
  84.     pygame.display.flip()
  85.     # 延迟10毫秒
  86.     pygame.time.delay(10)
复制代码

第82行中
  1. if position.bottom > height:
  2.         turtle = turtle_bottom
  3.         position = turtle_rect = turtle.get_rect()
  4.         position.left = width - turtle_rect.width
  5.         position.top = height - turtle_height
  6.         speed = [-5 , 0]
复制代码

小甲鱼在视频中写的是这个,但是明明turtle_height没有定义,为什么他的可以运行而我的不可以
最佳答案
2022-5-21 17:25:15
1、82 行 是 turtle_buttom 不是 turtle_bottom

2、85、91行 是 turtle_rect 中的 height 属性,不是 turtle_height

参考代码:

  1. import pygame
  2. import sys
  3. from pygame.locals import *

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

  6. size = width, height = 600, 400

  7. bg = (255, 255, 255) #RGB

  8. fullscreen = False

  9. # 指定窗口大小
  10. screen = pygame.display.set_mode(size, RESIZABLE)
  11. # 设置窗口标题
  12. pygame.display.set_caption("初次见面,请大家多多关照!")

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


  15. # 加载图片
  16. oturtle = pygame.image.load("2.jpeg")
  17. turtle = oturtle
  18. # 获得图像的位置矩形
  19. oturtle_rect = oturtle.get_rect()
  20. position = turtle_rect = oturtle_rect

  21. speed = [5, 0]
  22. turtle_right = pygame.transform.rotate(turtle, 90)
  23. turtle_top = pygame.transform.rotate(turtle, 180)
  24. turtle_left = pygame.transform.rotate(turtle, 270)
  25. turtle_buttom = turtle

  26. l_head = turtle
  27. r_head = pygame.transform.flip(turtle, True, False)

  28. while True:
  29.     for event in pygame.event.get():
  30.         if event.type == pygame.QUIT:
  31.             sys.exit()

  32.         if event.type == KEYDOWN:

  33.             # 全屏
  34.             if event.key == K_F11:
  35.                 fullscreen = not  fullscreen
  36.                 if fullscreen:
  37.                     screen = pygame.display.set_mode((1024, 768), FULLSCREEN | HWSURFACE)
  38.                     width, height = 1024, 768
  39.                 else:
  40.                     screen = pygame.display.set_mode(size)
  41.             # 放大、缩小小乌龟(=、-),空格键恢复原始尺寸
  42.             if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
  43.                 # 最大只能放大一倍,缩小50%
  44.                 if event.key == K_EQUALS and ratio < 2:
  45.                     ratio += 0.1
  46.                 if event.key == K_MINUS and ratio > 0.5:
  47.                     ratio -= 0.1
  48.                 if event.key  == K_SPACE:
  49.                     ratio = 1.0

  50.                 turtle = pygame.transform.smoothscale(oturtle, (int(oturtle_rect.width*ratio), int(oturtle_rect.height*ratio)))
  51.         # 用户调整窗口尺寸
  52.         if event.type == VIDEORESIZE:
  53.             size = event.size
  54.             width, height = size
  55.             print(size)
  56.             screen = pygame.display.set_mode(size, RESIZABLE)

  57.     # 图像移动
  58.     position = position.move(speed)

  59.     if position.right > width :
  60.         turtle = turtle_right
  61.         position = turtle_rect = turtle.get_rect()
  62.         position.left = width - turtle_rect.width
  63.         speed = [0, 5]

  64.     if position.bottom > height:
  65.         turtle = turtle_buttom
  66.         position = turtle_rect = turtle.get_rect()
  67.         position.left = width - turtle_rect.width
  68.         position.top = height - turtle_rect.height
  69.         speed = [-5 , 0]

  70.     if position.left < 0:
  71.         turtle = turtle_left
  72.         position = turtle_rect = turtle.get_rect()
  73.         position.top = height - turtle_rect.height
  74.         speed = [0, -5]

  75.     if position.top < 0:
  76.         turtle = turtle_top
  77.         position = turtle_rect = turtle. get_rect()
  78.         speed = [5, 0]

  79.     # 填充背景
  80.     screen.fill(bg)
  81.     # 更新图像
  82.     screen.blit(turtle, position)
  83.     # 更新界面
  84.     pygame.display.flip()
  85.     # 延迟10毫秒
  86.     pygame.time.delay(10)
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-21 17:25:15 | 显示全部楼层    本楼为最佳答案   
1、82 行 是 turtle_buttom 不是 turtle_bottom

2、85、91行 是 turtle_rect 中的 height 属性,不是 turtle_height

参考代码:

  1. import pygame
  2. import sys
  3. from pygame.locals import *

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

  6. size = width, height = 600, 400

  7. bg = (255, 255, 255) #RGB

  8. fullscreen = False

  9. # 指定窗口大小
  10. screen = pygame.display.set_mode(size, RESIZABLE)
  11. # 设置窗口标题
  12. pygame.display.set_caption("初次见面,请大家多多关照!")

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


  15. # 加载图片
  16. oturtle = pygame.image.load("2.jpeg")
  17. turtle = oturtle
  18. # 获得图像的位置矩形
  19. oturtle_rect = oturtle.get_rect()
  20. position = turtle_rect = oturtle_rect

  21. speed = [5, 0]
  22. turtle_right = pygame.transform.rotate(turtle, 90)
  23. turtle_top = pygame.transform.rotate(turtle, 180)
  24. turtle_left = pygame.transform.rotate(turtle, 270)
  25. turtle_buttom = turtle

  26. l_head = turtle
  27. r_head = pygame.transform.flip(turtle, True, False)

  28. while True:
  29.     for event in pygame.event.get():
  30.         if event.type == pygame.QUIT:
  31.             sys.exit()

  32.         if event.type == KEYDOWN:

  33.             # 全屏
  34.             if event.key == K_F11:
  35.                 fullscreen = not  fullscreen
  36.                 if fullscreen:
  37.                     screen = pygame.display.set_mode((1024, 768), FULLSCREEN | HWSURFACE)
  38.                     width, height = 1024, 768
  39.                 else:
  40.                     screen = pygame.display.set_mode(size)
  41.             # 放大、缩小小乌龟(=、-),空格键恢复原始尺寸
  42.             if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
  43.                 # 最大只能放大一倍,缩小50%
  44.                 if event.key == K_EQUALS and ratio < 2:
  45.                     ratio += 0.1
  46.                 if event.key == K_MINUS and ratio > 0.5:
  47.                     ratio -= 0.1
  48.                 if event.key  == K_SPACE:
  49.                     ratio = 1.0

  50.                 turtle = pygame.transform.smoothscale(oturtle, (int(oturtle_rect.width*ratio), int(oturtle_rect.height*ratio)))
  51.         # 用户调整窗口尺寸
  52.         if event.type == VIDEORESIZE:
  53.             size = event.size
  54.             width, height = size
  55.             print(size)
  56.             screen = pygame.display.set_mode(size, RESIZABLE)

  57.     # 图像移动
  58.     position = position.move(speed)

  59.     if position.right > width :
  60.         turtle = turtle_right
  61.         position = turtle_rect = turtle.get_rect()
  62.         position.left = width - turtle_rect.width
  63.         speed = [0, 5]

  64.     if position.bottom > height:
  65.         turtle = turtle_buttom
  66.         position = turtle_rect = turtle.get_rect()
  67.         position.left = width - turtle_rect.width
  68.         position.top = height - turtle_rect.height
  69.         speed = [-5 , 0]

  70.     if position.left < 0:
  71.         turtle = turtle_left
  72.         position = turtle_rect = turtle.get_rect()
  73.         position.top = height - turtle_rect.height
  74.         speed = [0, -5]

  75.     if position.top < 0:
  76.         turtle = turtle_top
  77.         position = turtle_rect = turtle. get_rect()
  78.         speed = [5, 0]

  79.     # 填充背景
  80.     screen.fill(bg)
  81.     # 更新图像
  82.     screen.blit(turtle, position)
  83.     # 更新界面
  84.     pygame.display.flip()
  85.     # 延迟10毫秒
  86.     pygame.time.delay(10)
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-21 20:10:30 | 显示全部楼层
Twilight6 发表于 2022-5-21 17:25
1、82 行 是 turtle_buttom 不是 turtle_bottom

2、85、91行 是 turtle_rect 中的 height 属性,不是 tu ...

感谢    确实是我没看清楚
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-22 00:02:13 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-22 11:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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