|
发表于 2020-4-22 14:04:41
|
显示全部楼层
本楼为最佳答案
本帖最后由 txxcat 于 2020-4-22 14:05 编辑
单从语法上来说,pygame.Rect(left, top, width, height),R要大写,参数也不大对,正确写法应该是:
- cut_image = pygame.Rect(location_mouse_begin,(location_mouse_end[0]-location_mouse_begin[0],location_mouse_end[1]-location_mouse_begin[1]))
复制代码
但是这个方法在这里适用不适用就难说了。
关于捕捉鼠标按下和松开的逻辑,我改了一段你看看能不能参考一下:
- location_mouse_begin=None
- location_mouse_end=None
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit()
-
-
- if event.type == pygame.MOUSEBUTTONDOWN:
- #获取鼠标按下时的位置
- location_mouse_begin = pygame.mouse.get_pos()
-
- if event.type == pygame.MOUSEBUTTONUP:
- #获取鼠标松开时的位置
- location_mouse_end = pygame.mouse.get_pos()
-
- if location_mouse_begin and location_mouse_end:
- print(location_mouse_begin,location_mouse_end)
- #判断鼠标是否第一次按下,限定裁剪范围
- #绘制矩形,函数表示原地移动Rect对象
- # if event.button == 1: #鼠标左键按下
- # code.append(1)
- # cut_image = pygame.Rect(turtle,"black",\
- # location_mouse_begin,location_mouse_end,width = 2)
- cut_image = pygame.Rect(location_mouse_begin,(location_mouse_end[0]-location_mouse_begin[0],location_mouse_end[1]-location_mouse_begin[1]))
- location_mouse_begin=None
- location_mouse_end=None
复制代码
对于第几次按下鼠标,你可以在里面加上计数的代码,具体功能的实现,恐怕还需要你自己想想办法了。 |
|