鱼C论坛

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

[已解决]84讲,50行,NameError: name 'location_mouse_end' is not defined

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

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

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

x
目前只是初步想实现裁剪功能,Rect的用法对吗,如果对吗

  1. #实现图片的裁剪功能,第一次单击鼠标,选定裁剪区域,第二次单击鼠标后,可拖动所裁剪的区域
  2. #第三次单击,所拖动区域消失,再次单击,可选定新的裁剪区域,重复操作
  3. import pygame
  4. import sys   #退出程序时要用
  5. from pygame.locals import *


  6. #初始化pygame,他是一个包
  7. pygame.init()

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

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

  13. #设置窗口的标题
  14. pygame.display.set_caption("来剪我呀")

  15. #加载图片
  16. turtle=pygame.image.load("turtle.png")

  17. #获取图像的位置矩形
  18. position = turtle.get_rect()

  19. #移动乌龟到相对中央的位置
  20. for i in range(len(position)):
  21.    position[i] = position[i] + 180

  22. code = []
  23. while True:
  24.     for event in pygame.event.get():
  25.         if event.type == pygame.QUIT:
  26.             sys.exit()
  27.             
  28.         if event.type == pygame.MOUSEBUTTONUP:
  29.             #获取鼠标松开时的位置
  30.             location_mouse_end = pygame.mouse.get_pos()
  31.             
  32.         if event.type == pygame.MOUSEBUTTONDOWN:
  33.             #获取鼠标按下时的位置
  34.             location_mouse_begin = pygame.mouse.get_pos()
  35.             print(location_mouse_begin)
  36.             #判断鼠标是否第一次按下,限定裁剪范围
  37.             #绘制矩形,函数表示原地移动Rect对象
  38.             if event.button == 1:    #鼠标左键按下
  39.                code.append(1)
  40.                cut_image = pygame.rect(turtle,"black",\
  41.                                         location_mouse_begin,location_mouse_end,width = 2)

  42.                   

  43.     #填充背景色
  44.     screen.fill(bg)

  45.     #更新图片,blit将一个图像画到另一个图像上去,a_cartoon画到screen)
  46.     screen.blit(turtle,position)

  47.     # 更新界面
  48.     pygame.display.flip()

  49.     #延迟
  50.     pygame.time.delay(10)
复制代码
最佳答案
2020-4-22 14:04:41
本帖最后由 txxcat 于 2020-4-22 14:05 编辑
猪猪虾 发表于 2020-4-22 09:39
还是不对,我的这个逻辑就不太对,我想实现的是鼠标按下,获得第一个坐标,然后拖动鼠标,鼠标松开,获得 ...


单从语法上来说,pygame.Rect(left, top, width, height),R要大写,参数也不大对,正确写法应该是:
  1. cut_image = pygame.Rect(location_mouse_begin,(location_mouse_end[0]-location_mouse_begin[0],location_mouse_end[1]-location_mouse_begin[1]))
复制代码

但是这个方法在这里适用不适用就难说了。
关于捕捉鼠标按下和松开的逻辑,我改了一段你看看能不能参考一下:
  1. location_mouse_begin=None
  2. location_mouse_end=None
  3. while True:
  4.     for event in pygame.event.get():
  5.         if event.type == pygame.QUIT:
  6.             sys.exit()
  7.          
  8.             
  9.         if event.type == pygame.MOUSEBUTTONDOWN:
  10.             #获取鼠标按下时的位置
  11.             location_mouse_begin = pygame.mouse.get_pos()
  12.             
  13.         if event.type == pygame.MOUSEBUTTONUP:
  14.             #获取鼠标松开时的位置
  15.             location_mouse_end = pygame.mouse.get_pos()
  16.             
  17.         if location_mouse_begin and location_mouse_end:
  18.             print(location_mouse_begin,location_mouse_end)
  19.             #判断鼠标是否第一次按下,限定裁剪范围
  20.             #绘制矩形,函数表示原地移动Rect对象
  21. #            if event.button == 1:    #鼠标左键按下
  22. #                code.append(1)
  23. #               cut_image = pygame.Rect(turtle,"black",\
  24. #                                        location_mouse_begin,location_mouse_end,width = 2)
  25.                 cut_image = pygame.Rect(location_mouse_begin,(location_mouse_end[0]-location_mouse_begin[0],location_mouse_end[1]-location_mouse_begin[1]))
  26.                 location_mouse_begin=None
  27.                 location_mouse_end=None
复制代码

对于第几次按下鼠标,你可以在里面加上计数的代码,具体功能的实现,恐怕还需要你自己想想办法了。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-22 09:02:37 | 显示全部楼层
  1.         if event.type == pygame.MOUSEBUTTONUP:
  2.             #获取鼠标松开时的位置
  3.             location_mouse_end = pygame.mouse.get_pos()
复制代码

你是不是需要把这段移到这个判断里面去:
  1. if event.type == pygame.MOUSEBUTTONDOWN:
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-22 09:39:42 | 显示全部楼层
txxcat 发表于 2020-4-22 09:02
你是不是需要把这段移到这个判断里面去:

还是不对,我的这个逻辑就不太对,我想实现的是鼠标按下,获得第一个坐标,然后拖动鼠标,鼠标松开,获得第二个坐标,将两个坐标传递给Rect,,获取矩形区域

我的rect的用法对吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-22 14:04:41 | 显示全部楼层    本楼为最佳答案   
本帖最后由 txxcat 于 2020-4-22 14:05 编辑
猪猪虾 发表于 2020-4-22 09:39
还是不对,我的这个逻辑就不太对,我想实现的是鼠标按下,获得第一个坐标,然后拖动鼠标,鼠标松开,获得 ...


单从语法上来说,pygame.Rect(left, top, width, height),R要大写,参数也不大对,正确写法应该是:
  1. cut_image = pygame.Rect(location_mouse_begin,(location_mouse_end[0]-location_mouse_begin[0],location_mouse_end[1]-location_mouse_begin[1]))
复制代码

但是这个方法在这里适用不适用就难说了。
关于捕捉鼠标按下和松开的逻辑,我改了一段你看看能不能参考一下:
  1. location_mouse_begin=None
  2. location_mouse_end=None
  3. while True:
  4.     for event in pygame.event.get():
  5.         if event.type == pygame.QUIT:
  6.             sys.exit()
  7.          
  8.             
  9.         if event.type == pygame.MOUSEBUTTONDOWN:
  10.             #获取鼠标按下时的位置
  11.             location_mouse_begin = pygame.mouse.get_pos()
  12.             
  13.         if event.type == pygame.MOUSEBUTTONUP:
  14.             #获取鼠标松开时的位置
  15.             location_mouse_end = pygame.mouse.get_pos()
  16.             
  17.         if location_mouse_begin and location_mouse_end:
  18.             print(location_mouse_begin,location_mouse_end)
  19.             #判断鼠标是否第一次按下,限定裁剪范围
  20.             #绘制矩形,函数表示原地移动Rect对象
  21. #            if event.button == 1:    #鼠标左键按下
  22. #                code.append(1)
  23. #               cut_image = pygame.Rect(turtle,"black",\
  24. #                                        location_mouse_begin,location_mouse_end,width = 2)
  25.                 cut_image = pygame.Rect(location_mouse_begin,(location_mouse_end[0]-location_mouse_begin[0],location_mouse_end[1]-location_mouse_begin[1]))
  26.                 location_mouse_begin=None
  27.                 location_mouse_end=None
复制代码

对于第几次按下鼠标,你可以在里面加上计数的代码,具体功能的实现,恐怕还需要你自己想想办法了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-17 18:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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