鱼C论坛

 找回密码
 立即注册
查看: 1980|回复: 1

[已解决]Pygame的问题

[复制链接]
发表于 2019-12-1 07:19:41 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 lixiangyv 于 2019-12-1 07:30 编辑

问题就是《零基础入门学习Python》的第81讲:提高游戏的颜值,
那个放大缩小的那个例子,应该怎么改呀?这节课的课后题也没有。

源代码:
  1. import pygame
  2. from pygame.locals import *
  3. import sys

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

  6. size = width, height = 600, 400
  7. fsize = fwidth, fheight = pygame.display.list_modes()[0]
  8. speed = [-2, 1]
  9. bg = (255, 255, 255)
  10. fullscreen = False

  11. # 创建窗口
  12. screen = pygame.display.set_mode(size, RESIZABLE)
  13. pygame.display.set_caption("turtle")

  14. # 加载图片
  15. oturtle = pygame.image.load("images.png")
  16. turtle = oturtle
  17. # 获取图像位置矩形
  18. oturtle_rect = oturtle.get_rect()
  19. position = turtle_rect = oturtle_rect

  20. # 设置放大缩小的比例
  21. ratio = 1.0

  22. lhead = turtle
  23. rhead = pygame.transform.flip(turtle, True, False)

  24. clock = pygame.time.Clock()

  25. while True:
  26.     for event in pygame.event.get():
  27.         if event.type == pygame.QUIT:
  28.             sys.exit()
  29.         if event.type == KEYDOWN:
  30.             if event.key == K_LEFT:
  31.                 turtle = lhead
  32.                 speed = [-3, 0]
  33.             if event.key == K_RIGHT:
  34.                 turtle = rhead
  35.                 speed = [3, 0]
  36.             if event.key == K_UP:
  37.                 speed = [0, -3]
  38.             if event.key == K_DOWN:
  39.                 speed = [0, 3]

  40.             # 全屏(F11)
  41.             if event.key == K_F11:
  42.                 fullscreen = not fullscreen
  43.                 if fullscreen :
  44.                     screen = pygame.display.set_mode(
  45.                         fsize,
  46.                         FULLSCREEN | HWSURFACE)
  47.                     width, height = fwidth, fheight
  48.                 else:
  49.                     screen = pygame.display.set_mode(size)
  50.                     width, height = 600, 400

  51.             # 放大、缩小小乌龟
  52.             if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
  53.                 # 最大只能放大一倍,缩小50%
  54.                 if event.key == K_EQUALS and ratio < 2:
  55.                     ratio += 0.1
  56.                 if event.key == K_MINUS and ratio > 0.5:
  57.                     ratio -= 0.1
  58.                 if event.key == K_SPACE:
  59.                     ratio = 1.0

  60.                 turtle = pygame.transform.smoothscale(oturtle,
  61.                                              (int(oturtle_rect.width * ratio),
  62.                                              int(oturtle_rect.height * ratio)))
  63.                 lhead = turtle
  64.                 rhead = pygame.transform.flip(turtle, True, False)
  65.         # 用和调整矿口尺寸
  66.         if event.type == VIDEORESIZE:
  67.             size = event.size
  68.             width, height = size
  69.             screen = pygame.display.set_mode(size, RESIZABLE)
  70.             
  71.     # 移动图像
  72.     position = position.move(speed)
  73.     if position.left < 0:
  74.         turtle = pygame.transform.flip(turtle, True, False)
  75.         position.left = 0
  76.         speed[0] = -speed[0]
  77.     if position.right > width:
  78.         position.right = width
  79.         turtle = pygame.transform.flip(turtle, True, False)
  80.         speed[0] = -speed[0]

  81.     if position.top < 0:
  82.         position.top = 0
  83.         speed[1] = -speed[1]
  84.     if position.bottom > height:
  85.         position.bottom = height
  86.         speed[1] = -speed[1]

  87.     screen.fill(bg)
  88.     screen.blit(turtle, position)
  89.     pygame.display.flip()
  90.     clock.tick(150)
复制代码

问题:
放大的时候,小龟会跑到外面去。
放大的时候,小龟会向左边转

这该怎么改呀
最佳答案
2019-12-13 20:08:22
自己对着这个代码看看你在设置乌龟的头那出问题了
  1. import pygame
  2. import sys #退出程序时使用
  3. from pygame.locals import *

  4. #初始化Pygame
  5. pygame.init()#初始化Pygame,其中pygame实际是一个包,一个包里包含着各种功能不同的模块

  6. size = width,height = 600,400#其中使用元组指定窗口的大小
  7. speed = [-2,1]
  8. bg = (255,255,255)#RGB颜色值

  9. fullscreen = False
  10. #创建指定大小的窗口
  11. screen = pygame.display.set_mode(size)#其中pygame.display.set_mode方法会返回一个Surface的对象
  12. #设置窗口标题
  13. pygame.display.set_caption("初次见面,请大家多多关照!")

  14. #加载图片
  15. oturtle = pygame.image.load("turtle2.jpg")#表示原始的画布对象
  16. turtle = oturtle

  17. #设置放大缩小的比率
  18. ratio = 1.0

  19. #获取图像的位置矩形
  20. oturtle_rect = oturtle.get_rect()#其中每一个Surface对象都有一个rect矩形对象,用于表示矩形区域的位置和大小信息
  21. position = turtle_rect = oturtle_rect

  22. l_head = turtle#设置向左走时的头部朝向
  23. r_head = pygame.transform.flip(turtle,True,False)#设置向右走时的头部朝向,其中第一个参数为翻转对象,第二个为是否水平翻转,第三个参数为是否垂直翻转。

  24. while True:#设置死循环,让动画一直执行
  25.     for event in pygame.event.get():
  26.         print(event)
  27.         if event.type == pygame.QUIT:#当执行pygame的退出事件
  28.             sys.exit()
  29.         if event.type == KEYUP:#此时使用键盘事项
  30.             if event.key == K_LEFT:
  31.                 turtle = l_head
  32.                 speed = [-1,0]               
  33.             if event.key == K_RIGHT:
  34.                 turtle = r_head
  35.                 speed = [1,0]               
  36.             if event.key == K_UP:
  37.                 speed =[0,-1]
  38.             if event.key == K_DOWN:
  39.                 speed = [0,1]
  40.                
  41.             #全屏(F11)
  42.             if event.key == K_F11:
  43.                 fullscreen = not fullscreen#需要取反这样才可以从全屏和非全屏间切换
  44.                 if fullscreen:
  45.                     screen = pygame.display.set_mode((1920,1080),FULLSCREEN|HWSURFACE,RESIZABLE)
  46.                     width,height =1920,1080
  47.                 else:
  48.                     screen = pygame.display.set_mode(size,RESIZABLE)
  49.                     width,height = 600,400#此处需要重新给width和height重新赋值,要不然就还是1024和768

  50.             #放大、缩小乌龟(+,-),空格键恢复原始尺寸
  51.             if event.key == K_KP_PLUS or event.key == K_MINUS or event.key ==K_SPACE:
  52.             #其中小甲鱼用K_EQUALS来代替了,因为默认按下-表示减号,按下=表示等号,除非使用shift但是使用功能shift后获取的键值不对,K_KP_PLUS表示小键盘的+号
  53.                 #最大只能放大一倍,缩小50%
  54.                 if event.key == K_KP_PLUS and ratio < 2:
  55.                     ratio += 0.1
  56.                 if event.key == K_MINUS and ratio > 0.5:
  57.                     ratio -= 0.1
  58.                 if event.key == K_SPACE:
  59.                     ratio = 1.0
  60.                     
  61.                 turtle = pygame.transform.smoothscale(oturtle,\
  62.                 (int(oturtle_rect.width*ratio),int(oturtle_rect.height*ratio)))#此处使用oturtle对象进行转换,而且转换后的宽和高必须是整形,要以元组形式展示,然后必须赋值给turtle        
  63.                 #但是上方代码在点击左键或右键后就又重新恢复原来大小了。这是应为原来的l_head和r_head在赋值是给的初始的大小,所以在更改后得重新再次赋值。
  64.                 l_head = turtle#此时当变大后它无论在哪边,头都会往左边走,因为原始的oturtle的头就是向左的,而且放大后图片超出边界,加入一个方向位置的判断。
  65.                 r_head = pygame.transform.flip(turtle,True,False)
  66.                
  67.     #用户调整窗口尺寸
  68.         if event.type == VIDEORESIZE:
  69.             size = event.size
  70.             width,height =size
  71.             print(size)
  72.             screen = pygame.display.set_mode(size,RESIZABLE)#重新画这个屏幕
  73.                
  74.     #移动图像
  75.     position = position.move(speed)
  76.    
  77.     if position.left < 0 or position.right > width:#用于检测是否达到左右的边界,其中左上角(0,0),右下角为(600,400)
  78.         #翻转图像
  79.         turtle = pygame.transform.flip(turtle,True,False)#其中pygame.transform.flip()用来翻转对象,第一个参数为翻转对象,第二个为是否水平翻转,第三个参数为是否垂直翻转。
  80.         #反方向移动
  81.         speed[0] = -speed[0]
  82.         
  83.     if position.top < 0 or position.bottom > height:#用于检测是否达到上下的边界
  84.         speed[1] = -speed[1]#其中speed为一个列表,可以实时更改
  85.    
  86.     #填充背景
  87.     screen.fill(bg)
  88.     #更新图像
  89.     screen.blit(turtle,position)#screen.blit()方法就是把图像画到另一个图像上去,position用来表示覆盖到另一个画布上的位置
  90.     #更新界面
  91.     pygame.display.flip()#由于pygame采用的是双缓冲模式,所以要调用display.flip()将缓冲好的画面一次性刷新到我们的显示器。
  92.     #其中双缓冲就在在内存中建立一个与屏幕绘制区一模一样的对象   
  93.     #延迟10毫秒显示
  94.     pygame.time.delay(10)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-12-13 20:08:22 | 显示全部楼层    本楼为最佳答案   
自己对着这个代码看看你在设置乌龟的头那出问题了
  1. import pygame
  2. import sys #退出程序时使用
  3. from pygame.locals import *

  4. #初始化Pygame
  5. pygame.init()#初始化Pygame,其中pygame实际是一个包,一个包里包含着各种功能不同的模块

  6. size = width,height = 600,400#其中使用元组指定窗口的大小
  7. speed = [-2,1]
  8. bg = (255,255,255)#RGB颜色值

  9. fullscreen = False
  10. #创建指定大小的窗口
  11. screen = pygame.display.set_mode(size)#其中pygame.display.set_mode方法会返回一个Surface的对象
  12. #设置窗口标题
  13. pygame.display.set_caption("初次见面,请大家多多关照!")

  14. #加载图片
  15. oturtle = pygame.image.load("turtle2.jpg")#表示原始的画布对象
  16. turtle = oturtle

  17. #设置放大缩小的比率
  18. ratio = 1.0

  19. #获取图像的位置矩形
  20. oturtle_rect = oturtle.get_rect()#其中每一个Surface对象都有一个rect矩形对象,用于表示矩形区域的位置和大小信息
  21. position = turtle_rect = oturtle_rect

  22. l_head = turtle#设置向左走时的头部朝向
  23. r_head = pygame.transform.flip(turtle,True,False)#设置向右走时的头部朝向,其中第一个参数为翻转对象,第二个为是否水平翻转,第三个参数为是否垂直翻转。

  24. while True:#设置死循环,让动画一直执行
  25.     for event in pygame.event.get():
  26.         print(event)
  27.         if event.type == pygame.QUIT:#当执行pygame的退出事件
  28.             sys.exit()
  29.         if event.type == KEYUP:#此时使用键盘事项
  30.             if event.key == K_LEFT:
  31.                 turtle = l_head
  32.                 speed = [-1,0]               
  33.             if event.key == K_RIGHT:
  34.                 turtle = r_head
  35.                 speed = [1,0]               
  36.             if event.key == K_UP:
  37.                 speed =[0,-1]
  38.             if event.key == K_DOWN:
  39.                 speed = [0,1]
  40.                
  41.             #全屏(F11)
  42.             if event.key == K_F11:
  43.                 fullscreen = not fullscreen#需要取反这样才可以从全屏和非全屏间切换
  44.                 if fullscreen:
  45.                     screen = pygame.display.set_mode((1920,1080),FULLSCREEN|HWSURFACE,RESIZABLE)
  46.                     width,height =1920,1080
  47.                 else:
  48.                     screen = pygame.display.set_mode(size,RESIZABLE)
  49.                     width,height = 600,400#此处需要重新给width和height重新赋值,要不然就还是1024和768

  50.             #放大、缩小乌龟(+,-),空格键恢复原始尺寸
  51.             if event.key == K_KP_PLUS or event.key == K_MINUS or event.key ==K_SPACE:
  52.             #其中小甲鱼用K_EQUALS来代替了,因为默认按下-表示减号,按下=表示等号,除非使用shift但是使用功能shift后获取的键值不对,K_KP_PLUS表示小键盘的+号
  53.                 #最大只能放大一倍,缩小50%
  54.                 if event.key == K_KP_PLUS and ratio < 2:
  55.                     ratio += 0.1
  56.                 if event.key == K_MINUS and ratio > 0.5:
  57.                     ratio -= 0.1
  58.                 if event.key == K_SPACE:
  59.                     ratio = 1.0
  60.                     
  61.                 turtle = pygame.transform.smoothscale(oturtle,\
  62.                 (int(oturtle_rect.width*ratio),int(oturtle_rect.height*ratio)))#此处使用oturtle对象进行转换,而且转换后的宽和高必须是整形,要以元组形式展示,然后必须赋值给turtle        
  63.                 #但是上方代码在点击左键或右键后就又重新恢复原来大小了。这是应为原来的l_head和r_head在赋值是给的初始的大小,所以在更改后得重新再次赋值。
  64.                 l_head = turtle#此时当变大后它无论在哪边,头都会往左边走,因为原始的oturtle的头就是向左的,而且放大后图片超出边界,加入一个方向位置的判断。
  65.                 r_head = pygame.transform.flip(turtle,True,False)
  66.                
  67.     #用户调整窗口尺寸
  68.         if event.type == VIDEORESIZE:
  69.             size = event.size
  70.             width,height =size
  71.             print(size)
  72.             screen = pygame.display.set_mode(size,RESIZABLE)#重新画这个屏幕
  73.                
  74.     #移动图像
  75.     position = position.move(speed)
  76.    
  77.     if position.left < 0 or position.right > width:#用于检测是否达到左右的边界,其中左上角(0,0),右下角为(600,400)
  78.         #翻转图像
  79.         turtle = pygame.transform.flip(turtle,True,False)#其中pygame.transform.flip()用来翻转对象,第一个参数为翻转对象,第二个为是否水平翻转,第三个参数为是否垂直翻转。
  80.         #反方向移动
  81.         speed[0] = -speed[0]
  82.         
  83.     if position.top < 0 or position.bottom > height:#用于检测是否达到上下的边界
  84.         speed[1] = -speed[1]#其中speed为一个列表,可以实时更改
  85.    
  86.     #填充背景
  87.     screen.fill(bg)
  88.     #更新图像
  89.     screen.blit(turtle,position)#screen.blit()方法就是把图像画到另一个图像上去,position用来表示覆盖到另一个画布上的位置
  90.     #更新界面
  91.     pygame.display.flip()#由于pygame采用的是双缓冲模式,所以要调用display.flip()将缓冲好的画面一次性刷新到我们的显示器。
  92.     #其中双缓冲就在在内存中建立一个与屏幕绘制区一模一样的对象   
  93.     #延迟10毫秒显示
  94.     pygame.time.delay(10)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-9 09:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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