|
楼主 |
发表于 2020-3-27 15:59:28
|
显示全部楼层
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode([1280,720])
keep_going = True
timer = pygame.time.Clock()
bg_color = (255,255,255)
pygame.display.set_caption("Pygame")
image = pygame.image.load("z.jpg")
chop_image = pygame.transform.chop(image,(207,200,50,50))
position = chop_image.get_rect()
width = screen.get_rect().width
height = screen.get_rect().height
position.center = width // 2, height // 2
select = 0
select_rect = pygame.Rect(0,0,0,0)
drag = 0
while keep_going:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
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.buttton == 1:
# 第一次释放,结束选择
if select == 1 and drag == 0:
pos_stop = event.pos
select = 2
# 第二次释放,结束拖拽
elif select == 2 and drag == 1:
drag = 2
screen.fill(bg_color)
screen.blit(image,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()
pygame.quit()
|
|