|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import pygame
import sys
from pygame.locals import *
pygame.init()
size = width,height = 640,480
bg=(255,255,255)
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("裁剪图片")
turtle = pygame.image.load("turtle.png")
select = 0
select_rect = pygame.Rect(0,0,0,0)
drag = 0
position = turtle.get_rect()
position.center = width // 2,height // 2
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == MOUSEBUTTONDOWN:#事件类型为
if event.button == 1:
#第一次点击,选择范围
if select == 0 and drag == 0:
pos_start = event.pos
select = 1
#第二次点击,拖拽图像
elif select == 2 and drag == 0:
capture = screen.subsurface(select_rect).copy()
cap_rect = capture.get_rect()
drag = 1
#第三次点击,初始化
elif select == 2 and drag == 2:
select = 0
drag = 0
elif event.type == MOUSEBUTTONUP:
if event.button == 1:
#第一次释放,结束选择
if select == 1 and drag == 0:
pos_stop = event.pos
select = 2
#第二次释放,结束拖拽
if select == 2 and drag == 1:
drag = 2
screen.fill(bg)
screen.blit(turtle,position)
#实时绘制选择框
if select:
mouse_pos = pygame.mouse.get_pos()#获取光标的位置
if select == 1:
pos_stop = mouse_pos
select_rect.left,select_rect.top = pos_start
select_rect.width, select_rect.height = pos_stop[0] - pos_start[0],pos_stop[1] - pos_start[1]
pygame.draw.rect(screen,(0,0,0),select_rect,1)
#拖拽裁剪的图像
if drag:
if drag == 1:
cap_rect.center = mouse_pos
screen.blit(capture,cap_rect)
pygame.display.flip()
clock.tick(30)
你想咋理解,我觉得就字面意思啊
拖拽从左上到右下,
pos_start 是矩形左上角坐标 -- 鼠标第一次点击的坐标
pos_stop 是拖拽完成后,鼠标释放时的坐标
右下角坐标x - 左上角坐标x就是矩形的宽
右下角坐标y - 左上角坐标y就是矩形的高
|
|