猪猪虾 发表于 2020-4-22 08:53:49

84讲,50行,NameError: name 'location_mouse_end' is not defined

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

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


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

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

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

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

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

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

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

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

                  

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

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

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

    #延迟
    pygame.time.delay(10)

txxcat 发表于 2020-4-22 09:02:37

      if event.type == pygame.MOUSEBUTTONUP:
            #获取鼠标松开时的位置
            location_mouse_end = pygame.mouse.get_pos()

你是不是需要把这段移到这个判断里面去:
if event.type == pygame.MOUSEBUTTONDOWN:

猪猪虾 发表于 2020-4-22 09:39:42

txxcat 发表于 2020-4-22 09:02
你是不是需要把这段移到这个判断里面去:

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

我的rect的用法对吗

txxcat 发表于 2020-4-22 14:04:41

本帖最后由 txxcat 于 2020-4-22 14:05 编辑

猪猪虾 发表于 2020-4-22 09:39
还是不对,我的这个逻辑就不太对,我想实现的是鼠标按下,获得第一个坐标,然后拖动鼠标,鼠标松开,获得 ...

单从语法上来说,pygame.Rect(left, top, width, height),R要大写,参数也不大对,正确写法应该是:
cut_image = pygame.Rect(location_mouse_begin,(location_mouse_end-location_mouse_begin,location_mouse_end-location_mouse_begin))
但是这个方法在这里适用不适用就难说了。
关于捕捉鼠标按下和松开的逻辑,我改了一段你看看能不能参考一下:
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-location_mouse_begin,location_mouse_end-location_mouse_begin))
                location_mouse_begin=None
                location_mouse_end=None
对于第几次按下鼠标,你可以在里面加上计数的代码,具体功能的实现,恐怕还需要你自己想想办法了。
页: [1]
查看完整版本: 84讲,50行,NameError: name 'location_mouse_end' is not defined