|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
目前只是初步想实现裁剪功能,Rect的用法对吗,如果对吗
- #实现图片的裁剪功能,第一次单击鼠标,选定裁剪区域,第二次单击鼠标后,可拖动所裁剪的区域
- #第三次单击,所拖动区域消失,再次单击,可选定新的裁剪区域,重复操作
- import pygame
- import sys #退出程序时要用
- from pygame.locals import *
- #初始化pygame,他是一个包
- pygame.init()
- size= width,height = 600,600
- speed = [5,0] #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[i] = position[i] + 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 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
复制代码
对于第几次按下鼠标,你可以在里面加上计数的代码,具体功能的实现,恐怕还需要你自己想想办法了。
|
|