| 
 | 
 
 
 楼主 |
发表于 2020-4-23 13:51:26
|
显示全部楼层
 
 
 
 
为啥我的剪切不了,我都一句一句的对着看了 
 
- #实现图片的裁剪功能,第一次单击鼠标,选定裁剪区域,第二次单击鼠标后,可拖动所裁剪的区域
 
 - #第三次单击,所拖动区域消失,再次单击,可选定新的裁剪区域,重复操作
 
 - 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")
 
  
- # 0->未选择,1 -> 选择中,2-> 完成选择
 
 - select = 0
 
  
- #初始化一个选择框
 
 - select_rect = pygame.Rect(0,0,0,0)
 
  
- # 0->未拖拽,1 -> 拖拽中,2-> 完成拖拽
 
 - drag = 0
 
  
 
- #获取图像的位置矩形
 
 - position = turtle.get_rect()
 
  
- #移动乌龟到相对中央的位置
 
 - for i in range(len(position)):
 
 -    position[i] = position[i] + 180
 
  
 
- while True:
 
 -     for event in pygame.event.get():
 
 -         if event.type == pygame.QUIT:
 
 -             sys.exit()
 
 -             
 
 -         elif event.type == pygame.MOUSEBUTTONDOWN:
 
 -              if event.button == 1:
 
 -                 #第一次点击,选择范围
 
 -                 if select == 0 and drag == 0:
 
 -                    pos_strat = event.pos  #获取鼠标当前位置
 
 -                    select =1
 
  
-                 #第二次点击,拖拽图像
 
 -                 elif select == 2 and drag == 0:
 
 -                    capture = screen.subsurface(select_rect).copy()  #获取子对象
 
 -                    capture_rect = capture.get_rect()                #获取子对象的位置
 
 -                    drag = 1 
 
 -                 #第三次点击,初始化
 
 -                 elif select == 2 and drag == 2:
 
 -                    select = 0
 
 -                    drag = 0
 
  
-         elif event.type ==  pygame.MOUSEBUTTONUP:
 
 -             #第一次释放,结束选择
 
 -             if event.button == 1:
 
 -                pos_stop = event.pos  #获取鼠标当前位置
 
 -                select =2
 
 -             #第二次释放,结束拖拽
 
 -             if select == 2 and drag == 1:
 
 -                drag == 2
 
  
-     #填充背景色
 
 -     screen.fill(bg)
 
  
-     #更新图片,blit将一个图像画到另一个图像上去,a_cartoon画到screen)
 
 -     screen.blit(turtle,position)
 
  
 
-     #实时绘制选择框
 
 -     if select:   #select != 0
 
 -        mouse_pos = pygame.mouse.get_pos()    #得到鼠标当前的位置
 
 -        if select == 1:
 
 -           pos_stop = mouse_pos  #获取鼠标当前位置
 
 -        select_rect.left,select_rect.top = pos_strat
 
 -        select_rect.width,select_rect.height = pos_stop[0]-pos_strat[0],pos_stop[1]-pos_strat[1]
 
 -        pygame.draw.rect(screen,(0,0,0),select_rect,1)  #画框
 
 -     #拖拽剪裁的图像
 
 -     if drag:
 
 -        if drag == 1:
 
 -           capture_rect.center = mouse_pos     #鼠标的位置实时在图片的中心
 
 -        screen.blit(capture,capture_rect)      #将子图像绘制到屏幕上
 
 -        
 
 -     #填充背景色
 
 -     screen.fill(bg)
 
  
-     #更新图片,blit将一个图像画到另一个图像上去,a_cartoon画到screen)
 
 -     screen.blit(turtle,position)
 
  
-     # 更新界面
 
 -     pygame.display.flip()
 
  
-     #延迟
 
 -     pygame.time.delay(10)
 
 
  复制代码 |   
 
 
 
 |