鱼C论坛

 找回密码
 立即注册
查看: 845|回复: 2

[已解决]72行,NameError: name 'ocartoon_rect' is not defined

[复制链接]
发表于 2020-4-20 22:33:53 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 猪猪虾 于 2020-4-20 23:00 编辑

  1. import pygame
  2. import sys   #退出程序时要用
  3. from pygame.locals import *

  4. #初始化pygame,他是一个包
  5. pygame.init()
  6. resortion = pygame.display.list_modes()  #获取当前屏幕允许的分辨率

  7. size= width,height = 600,400
  8. speed = [-2,1]     #x每次往左走2,Y向下偏移1格
  9. bg=(255,255,255)   #背景填充为白色

  10. #创建指定大小的窗口,RESIZABLE窗口尺寸可修改
  11. screen=pygame.display.set_mode(size,RESIZABLE)

  12. #设置窗口的标题
  13. pygame.display.set_caption("初次见面,请多指教")

  14. #加载图片
  15. ocartoon=pygame.image.load("turtle.png")
  16. cartoon=ocartoon
  17. #获取图像的位置矩形
  18. position = cartoon.get_rect()

  19. l_head=cartoon
  20. r_head=pygame.transform.flip(cartoon,True,False)

  21. #设置放大缩小的倍数
  22. ratio = 1.0  #每点一次,增加或者减小1倍

  23. fullscreen = False
  24. while True:
  25.     for event in pygame.event.get():
  26.         if event.type == pygame.QUIT:
  27.             sys.exit()

  28.         if event.type== KEYDOWN:
  29.             if event.key == K_LEFT:
  30.                 cartoon=l_head
  31.                 speed=[-1,0]
  32.             if event.key == K_RIGHT:
  33.                 speed=[1,0]
  34.                 cartoon=r_head
  35.             if event.key == K_UP:
  36.                 speed=[0,-1]
  37.             if event.key == K_DOWN:
  38.                 speed=[0,1]

  39.             #全屏,
  40.            
  41.             if event.key == K_F11:
  42.                 fullscreen = not fullscreen
  43.                 print(resortion[0])
  44.                 if fullscreen:
  45.                     #设置屏幕大小
  46.                     screen = pygame.display.set_mode(resortion[0],FULLSCREEN | HWSURFACE)
  47.                     width,height= resortion[0][0],resortion[0][1]
  48.                     
  49.                 else:
  50.                     screen = pygame.display.set_mode(size)
  51.             #放大缩小尺寸,当鼠标按下+-空格
  52.             if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
  53.                 #设置最大只能放大2被
  54.                 if event.key == K_PLUS and ratio<3:
  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.                 cartoon=pygame.transform.smoothscale(ocartoon,(int(ocartoon_rect.width * ratio),int(ocartoon_rect.height * ratio)))
  62.                 l_head=cartoon
  63.                 r_head=pygame.transform.flip(cartoon,True,False)


  64.         #用户调整窗口尺寸
  65.         if event.type == VIDEORESIZE:
  66.             size = event.size
  67.             width,height= size[0],size[1]
  68.             print(size)
  69.             #创建指定大小的窗口,RESIZABLE窗口尺寸可修改
  70.             screen=pygame.display.set_mode(size,RESIZABLE)

  71.                            
  72.     #移动图像
  73.     position = position.move(speed)

  74.     if position.left<0 or position.right > width:
  75.         #反转图像,True表示水平翻转,False表示不垂直翻转
  76.         a_cartoon=pygame.transform.flip(cartoon,True,False)
  77.         #反方向移动
  78.         speed[0] = -speed[0]

  79.     if position.top<0 or position.bottom > height:
  80.         speed[1] = -speed[1]
  81.         
  82.     #填充背景色
  83.     screen.fill(bg)

  84.     #更新图片,blit将一个图像画到另一个图像上去,a_cartoon画到screen)
  85.     screen.blit(cartoon,position)

  86.     # 更新界面
  87.     pygame.display.flip()

  88.     #延迟
  89.     pygame.time.delay(10)
  90.    

复制代码
最佳答案
2020-4-21 02:40:33
程序里两个问题:
一个就是'NameError: name 'ocartoon_rect' is not defined',变量名'ocartoon_rect'没有被定义,很明显这个变量名是错的,分析代码改成:ocartoon.get_rect()就可以运行了。
调试中发现不能放大,发现外面条件是if event.key == K_EQUALS,里面却是if event.key == K_PLUS,把K_PLUS改成 K_EQUALS后运行正常。
下面是修改后的完整代码:

  1. import pygame
  2. import sys   #退出程序时要用
  3. from pygame.locals import *

  4. #初始化pygame,他是一个包
  5. pygame.init()
  6. resortion = pygame.display.list_modes()  #获取当前屏幕允许的分辨率

  7. size= width,height = 600,400
  8. speed = [-2,1]     #x每次往左走2,Y向下偏移1格
  9. bg=(255,255,255)   #背景填充为白色

  10. #创建指定大小的窗口,RESIZABLE窗口尺寸可修改
  11. screen=pygame.display.set_mode(size,RESIZABLE)

  12. #设置窗口的标题
  13. pygame.display.set_caption("初次见面,请多指教")

  14. #加载图片
  15. ocartoon=pygame.image.load("turtle.png")
  16. cartoon=ocartoon
  17. #获取图像的位置矩形
  18. position = cartoon.get_rect()

  19. l_head=cartoon
  20. r_head=pygame.transform.flip(cartoon,True,False)

  21. #设置放大缩小的倍数
  22. ratio = 1.0  #每点一次,增加或者减小1倍

  23. fullscreen = False
  24. while True:
  25.     for event in pygame.event.get():
  26.         if event.type == pygame.QUIT:
  27.             sys.exit()

  28.         if event.type== KEYDOWN:
  29.             if event.key == K_LEFT:
  30.                 cartoon=l_head
  31.                 speed=[-1,0]
  32.             if event.key == K_RIGHT:
  33.                 speed=[1,0]
  34.                 cartoon=r_head
  35.             if event.key == K_UP:
  36.                 speed=[0,-1]
  37.             if event.key == K_DOWN:
  38.                 speed=[0,1]

  39.             #全屏,
  40.            
  41.             if event.key == K_F11:
  42.                 fullscreen = not fullscreen
  43.                 print(resortion[0])
  44.                 if fullscreen:
  45.                     #设置屏幕大小
  46.                     screen = pygame.display.set_mode(resortion[0],FULLSCREEN | HWSURFACE)
  47.                     width,height= resortion[0][0],resortion[0][1]
  48.                     
  49.                 else:
  50.                     screen = pygame.display.set_mode(size)
  51.             #放大缩小尺寸,当鼠标按下+-空格
  52.             if event.key == K_EQUALS  or event.key == K_MINUS or event.key == K_SPACE:
  53.                 #设置最大只能放大2被
  54.                 if event.key == K_EQUALS and ratio<3:
  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.                 cartoon=pygame.transform.smoothscale(ocartoon,(int(ocartoon.get_rect().width * ratio),int(ocartoon.get_rect().height * ratio)))
  61.                 l_head=cartoon
  62.                 r_head=pygame.transform.flip(cartoon,True,False)


  63.         #用户调整窗口尺寸
  64.         if event.type == VIDEORESIZE:
  65.             size = event.size
  66.             width,height= size[0],size[1]
  67.             print(size)
  68.             #创建指定大小的窗口,RESIZABLE窗口尺寸可修改
  69.             screen=pygame.display.set_mode(size,RESIZABLE)

  70.                            
  71.     #移动图像
  72.     position = position.move(speed)

  73.     if position.left<0 or position.right > width:
  74.         #反转图像,True表示水平翻转,False表示不垂直翻转
  75.         a_cartoon=pygame.transform.flip(cartoon,True,False)
  76.         #反方向移动
  77.         speed[0] = -speed[0]

  78.     if position.top<0 or position.bottom > height:
  79.         speed[1] = -speed[1]
  80.         
  81.     #填充背景色
  82.     screen.fill(bg)

  83.     #更新图片,blit将一个图像画到另一个图像上去,a_cartoon画到screen)
  84.     screen.blit(cartoon,position)

  85.     # 更新界面
  86.     pygame.display.flip()

  87.     #延迟
  88.     pygame.time.delay(10)
  89.    
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-20 23:02:50 | 显示全部楼层
  1. https://zhidao.baidu.com/question/1899516828114237340.html
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-21 02:40:33 | 显示全部楼层    本楼为最佳答案   
程序里两个问题:
一个就是'NameError: name 'ocartoon_rect' is not defined',变量名'ocartoon_rect'没有被定义,很明显这个变量名是错的,分析代码改成:ocartoon.get_rect()就可以运行了。
调试中发现不能放大,发现外面条件是if event.key == K_EQUALS,里面却是if event.key == K_PLUS,把K_PLUS改成 K_EQUALS后运行正常。
下面是修改后的完整代码:

  1. import pygame
  2. import sys   #退出程序时要用
  3. from pygame.locals import *

  4. #初始化pygame,他是一个包
  5. pygame.init()
  6. resortion = pygame.display.list_modes()  #获取当前屏幕允许的分辨率

  7. size= width,height = 600,400
  8. speed = [-2,1]     #x每次往左走2,Y向下偏移1格
  9. bg=(255,255,255)   #背景填充为白色

  10. #创建指定大小的窗口,RESIZABLE窗口尺寸可修改
  11. screen=pygame.display.set_mode(size,RESIZABLE)

  12. #设置窗口的标题
  13. pygame.display.set_caption("初次见面,请多指教")

  14. #加载图片
  15. ocartoon=pygame.image.load("turtle.png")
  16. cartoon=ocartoon
  17. #获取图像的位置矩形
  18. position = cartoon.get_rect()

  19. l_head=cartoon
  20. r_head=pygame.transform.flip(cartoon,True,False)

  21. #设置放大缩小的倍数
  22. ratio = 1.0  #每点一次,增加或者减小1倍

  23. fullscreen = False
  24. while True:
  25.     for event in pygame.event.get():
  26.         if event.type == pygame.QUIT:
  27.             sys.exit()

  28.         if event.type== KEYDOWN:
  29.             if event.key == K_LEFT:
  30.                 cartoon=l_head
  31.                 speed=[-1,0]
  32.             if event.key == K_RIGHT:
  33.                 speed=[1,0]
  34.                 cartoon=r_head
  35.             if event.key == K_UP:
  36.                 speed=[0,-1]
  37.             if event.key == K_DOWN:
  38.                 speed=[0,1]

  39.             #全屏,
  40.            
  41.             if event.key == K_F11:
  42.                 fullscreen = not fullscreen
  43.                 print(resortion[0])
  44.                 if fullscreen:
  45.                     #设置屏幕大小
  46.                     screen = pygame.display.set_mode(resortion[0],FULLSCREEN | HWSURFACE)
  47.                     width,height= resortion[0][0],resortion[0][1]
  48.                     
  49.                 else:
  50.                     screen = pygame.display.set_mode(size)
  51.             #放大缩小尺寸,当鼠标按下+-空格
  52.             if event.key == K_EQUALS  or event.key == K_MINUS or event.key == K_SPACE:
  53.                 #设置最大只能放大2被
  54.                 if event.key == K_EQUALS and ratio<3:
  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.                 cartoon=pygame.transform.smoothscale(ocartoon,(int(ocartoon.get_rect().width * ratio),int(ocartoon.get_rect().height * ratio)))
  61.                 l_head=cartoon
  62.                 r_head=pygame.transform.flip(cartoon,True,False)


  63.         #用户调整窗口尺寸
  64.         if event.type == VIDEORESIZE:
  65.             size = event.size
  66.             width,height= size[0],size[1]
  67.             print(size)
  68.             #创建指定大小的窗口,RESIZABLE窗口尺寸可修改
  69.             screen=pygame.display.set_mode(size,RESIZABLE)

  70.                            
  71.     #移动图像
  72.     position = position.move(speed)

  73.     if position.left<0 or position.right > width:
  74.         #反转图像,True表示水平翻转,False表示不垂直翻转
  75.         a_cartoon=pygame.transform.flip(cartoon,True,False)
  76.         #反方向移动
  77.         speed[0] = -speed[0]

  78.     if position.top<0 or position.bottom > height:
  79.         speed[1] = -speed[1]
  80.         
  81.     #填充背景色
  82.     screen.fill(bg)

  83.     #更新图片,blit将一个图像画到另一个图像上去,a_cartoon画到screen)
  84.     screen.blit(cartoon,position)

  85.     # 更新界面
  86.     pygame.display.flip()

  87.     #延迟
  88.     pygame.time.delay(10)
  89.    
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-18 04:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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