|

楼主 |
发表于 2018-5-11 09:10:07
|
显示全部楼层
- import pygame
- import sys
- class Cut:
- mouse_button_left = 1
- mouse_button_mid = 2
- mouse_button_right = 3
- mouse_button_up = 4
- mouse_button_down = 5
- select = 0
- draw = 0
- select_rect = pygame.Rect(0, 0, 0, 0)
- def color_img(self):
- #设置背景颜色
- bgcolor = (255, 255, 255)
- self.screen.fill(bgcolor)
- #填充背景图片
- self.img_bg = pygame.image.load('pig.jpg')
- self.screen.blit(self.img_bg, (int(self.width * 0.4), int(self.height * 0.4)))
-
-
- def __init__(self, width = 400, height = 500):
- #初始化pygame
- pygame.init()
- self.width =width
- self.height = height
-
- #初始化窗口大小和标题
- self.screen = pygame.display.set_mode((width, height))
- self.title = pygame.display.set_caption('Freedom Cut T_T')
- self.color_img()
- #更新界面
- pygame.display.update()
- def cut(self):
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit()
-
- if event.type == pygame.MOUSEBUTTONDOWN:
- if event.button == self.mouse_button_left:
- #第一次选择,选择范围
- if self.select == 0 and self.draw == 0:
- self.select = 1
- self.pos_start = pygame.mouse.get_pos()
- #开始拖动,拖动中
- if self.select == 2 and self.draw == 0:
- self.draw = 1
- self.capture = self.screen.subsurface(self.select_rect).copy()
- self.cap_rect = self.capture.get_rect()
- if self.select == 2 and self.draw == 2:
- self.select = 1
- self.draw = 0
- self.pos_start = pygame.mouse.get_pos()
-
- #mouse右键取消选中
- if event.button == self.mouse_button_right:
- self.select = 0
- self.draw = 0
-
- if event.type == pygame.MOUSEBUTTONUP:
- if event.button == self.mouse_button_left:
- #第一次释放,选择完成
- if self.select == 1 and self.draw == 0:
- self.select = 2
- self.pos_stop = pygame.mouse.get_pos()
- #第二次释放,拖动完成
- if self.select == 2 and self.draw == 1:
- self.draw = 2
- if self.select == 0 and self.draw == 0:
- pass
- if event.button == self.mouse_button_right:
- pass
- self.color_img()
- #实时绘图
- if self.select:
- mouse_pos = pygame.mouse.get_pos()
- self.select_rect.left, self.select_rect.top = self.pos_start
- if self.select == 1:
- self.select_rect.width, self.select_rect.height = mouse_pos
- else:
- self.select_rect.width, self.select_rect.height = self.pos_stop
- self.select_rect.width, self.select_rect.height = \
- self.select_rect.width - self.pos_start[0], self.select_rect.height - self.pos_start[1]
- pygame.draw.rect(self.screen, (0, 255, 0), self.select_rect, 1)
- #拖动裁剪的图像
- if self.draw:
- mouse_pos = pygame.mouse.get_pos()
- if self.draw == 1:
- self.cap_rect.center = mouse_pos
- self.screen.blit(self.capture, self.cap_rect)
-
- #更新界面
- pygame.display.update()
- if __name__ == '__main__':
- """width = int(input('请输入window的width:'))
- height = int(input('请输入window的height:'))"""
- cut = Cut()
- cut.cut()
复制代码 |
|