鱼C论坛

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

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

[复制链接]
发表于 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 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-10-1 15:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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