鱼C论坛

 找回密码
 立即注册
查看: 1124|回复: 18

[已解决]82讲,谁有剪切乌龟,能拖拽的源代码啊

[复制链接]
发表于 2020-4-23 13:34:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
分享一下
最佳答案
2020-4-23 13:36:14
好的好的
  1. import pygame
  2. import pygame.transform
  3. import sys
  4. from pygame.locals import *
  5. pygame.init()
  6. size = width,height = 400,350
  7. bg = (255,255,255)
  8. clock = pygame.time.Clock()
  9. screen = pygame.display.set_mode(size)
  10. pygame.display.set_caption('cutting turtle')
  11. turtle = pygame.image.load('turtle.png')
  12. select = 0
  13. select_rect = pygame.Rect(0,0,0,0)
  14. drag = 0
  15. position = turtle.get_rect()
  16. position.center = width // 2,height // 2
  17. while True:
  18.     for event in pygame.event.get():
  19.         if event.type == QUIT:
  20.             sys.exit()
  21.         elif event.type == MOUSEBUTTONDOWN:
  22.             if event.button == 1:
  23.                 if select == 0 and drag == 0:
  24.                     pos_start = event.pos
  25.                     select = 1
  26.                 elif select == 2 and drag == 0:
  27.                     capture = screen.subsurface(select_rect).copy()
  28.                     cap_rect = capture.get_rect()
  29.                     drag = 1
  30.                 elif select == 2 and drag == 2:
  31.                     select = 0
  32.                     drag = 0
  33.         elif event.type == MOUSEBUTTONUP:
  34.             if event.button == 1:
  35.                 if select == 1 and drag == 0:
  36.                     pos_stop = event.pos
  37.                     select = 2
  38.                 if select == 2 and drag == 1:
  39.                     drag = 2
  40.     # 设置背景
  41.     screen.fill(bg)
  42.     # 更新图片
  43.     screen.blit(turtle,position)
  44.     if select:
  45.         mouse_pos = pygame.mouse.get_pos()
  46.         if select == 1:
  47.                 pos_stop = mouse_pos
  48.         select_rect.left,select_rect.top = pos_start
  49.         select_rect.width,select_rect.height = pos_stop[0] - pos_start[0],pos_stop[1] - pos_start[1]
  50.         pygame.draw.rect(screen,(0,0,0),select_rect,1)
  51.     if drag:
  52.         if drag == 1:
  53.             cap_rect.center = mouse_pos
  54.         screen.blit(capture,cap_rect)
  55.     # 更新背景
  56.     pygame.display.flip()
  57.     # 延时10毫秒
  58.     #pygame.time.delay(10)
  59.     # 设置帧数
  60.     clock.tick(30)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-23 13:36:14 | 显示全部楼层    本楼为最佳答案   
好的好的
  1. import pygame
  2. import pygame.transform
  3. import sys
  4. from pygame.locals import *
  5. pygame.init()
  6. size = width,height = 400,350
  7. bg = (255,255,255)
  8. clock = pygame.time.Clock()
  9. screen = pygame.display.set_mode(size)
  10. pygame.display.set_caption('cutting turtle')
  11. turtle = pygame.image.load('turtle.png')
  12. select = 0
  13. select_rect = pygame.Rect(0,0,0,0)
  14. drag = 0
  15. position = turtle.get_rect()
  16. position.center = width // 2,height // 2
  17. while True:
  18.     for event in pygame.event.get():
  19.         if event.type == QUIT:
  20.             sys.exit()
  21.         elif event.type == MOUSEBUTTONDOWN:
  22.             if event.button == 1:
  23.                 if select == 0 and drag == 0:
  24.                     pos_start = event.pos
  25.                     select = 1
  26.                 elif select == 2 and drag == 0:
  27.                     capture = screen.subsurface(select_rect).copy()
  28.                     cap_rect = capture.get_rect()
  29.                     drag = 1
  30.                 elif select == 2 and drag == 2:
  31.                     select = 0
  32.                     drag = 0
  33.         elif event.type == MOUSEBUTTONUP:
  34.             if event.button == 1:
  35.                 if select == 1 and drag == 0:
  36.                     pos_stop = event.pos
  37.                     select = 2
  38.                 if select == 2 and drag == 1:
  39.                     drag = 2
  40.     # 设置背景
  41.     screen.fill(bg)
  42.     # 更新图片
  43.     screen.blit(turtle,position)
  44.     if select:
  45.         mouse_pos = pygame.mouse.get_pos()
  46.         if select == 1:
  47.                 pos_stop = mouse_pos
  48.         select_rect.left,select_rect.top = pos_start
  49.         select_rect.width,select_rect.height = pos_stop[0] - pos_start[0],pos_stop[1] - pos_start[1]
  50.         pygame.draw.rect(screen,(0,0,0),select_rect,1)
  51.     if drag:
  52.         if drag == 1:
  53.             cap_rect.center = mouse_pos
  54.         screen.blit(capture,cap_rect)
  55.     # 更新背景
  56.     pygame.display.flip()
  57.     # 延时10毫秒
  58.     #pygame.time.delay(10)
  59.     # 设置帧数
  60.     clock.tick(30)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-23 13:36:28 | 显示全部楼层
找着课程自己撸……(我一直都这么搞得)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-23 13:41:27 | 显示全部楼层
weiter 发表于 2020-4-23 13:36
找着课程自己撸……(我一直都这么搞得)

感谢建议
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-23 13:41:55 | 显示全部楼层
电邮多少,给你发
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-23 13:44:13 | 显示全部楼层

clock = pygame.time.Clock()
请教一下,这句是干啥
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-23 13:46:34 | 显示全部楼层

唯一的问题是我没有b站账号,经常看不清甲鱼老师撸的什么……(有账号的直接照抄就好,或者截图,自动扣字)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-23 13:48:55 | 显示全部楼层
weiter 发表于 2020-4-23 13:46
唯一的问题是我没有b站账号,经常看不清甲鱼老师撸的什么……(有账号的直接照抄就好,或者截图,自动扣 ...

我无意间发现B站上面有小甲鱼课程的盗版,有点侵权了,账号叫“”IT的搬运工“,很清晰,论坛上也可以直接看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-23 13:49:45 | 显示全部楼层

好了好了,你换头像了
谢谢谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-23 13:50:10 | 显示全部楼层
猪猪虾 发表于 2020-4-23 13:48
我无意间发现B站上面有小甲鱼课程的盗版,有点侵权了,账号叫“”IT的搬运工“,很清晰,论坛上也可以直接 ...

这都众所周知的事了……
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-23 13:50:45 | 显示全部楼层
weiter 发表于 2020-4-23 13:46
唯一的问题是我没有b站账号,经常看不清甲鱼老师撸的什么……(有账号的直接照抄就好,或者截图,自动扣 ...

注册一个不就好了,又不要钱
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-23 13:51:23 | 显示全部楼层
猪猪虾 发表于 2020-4-23 13:48
我无意间发现B站上面有小甲鱼课程的盗版,有点侵权了,账号叫“”IT的搬运工“,很清晰,论坛上也可以直接 ...

那个是13年的视频,那时候小甲鱼还没进B站呢,而且那个视频弹幕大神比较多,对学习也有帮助
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-23 13:51:26 | 显示全部楼层

为啥我的剪切不了,我都一句一句的对着看了

  1. #实现图片的裁剪功能,第一次单击鼠标,选定裁剪区域,第二次单击鼠标后,可拖动所裁剪的区域
  2. #第三次单击,所拖动区域消失,再次单击,可选定新的裁剪区域,重复操作
  3. import pygame
  4. import sys   #退出程序时要用
  5. from pygame.locals import *


  6. #初始化pygame,他是一个包
  7. pygame.init()

  8. size= width,height = 600,600
  9. speed = [5,0]     #x每次往左走2,Y向下偏移1格
  10. bg=(255,255,255)   #背景填充为白色

  11. #创建指定大小的窗口,RESIZABLE窗口尺寸可修改
  12. screen=pygame.display.set_mode(size,RESIZABLE)

  13. #设置窗口的标题
  14. pygame.display.set_caption("来剪我呀")

  15. #加载图片
  16. turtle=pygame.image.load("turtle.png")

  17. # 0->未选择,1 -> 选择中,2-> 完成选择
  18. select = 0

  19. #初始化一个选择框
  20. select_rect = pygame.Rect(0,0,0,0)

  21. # 0->未拖拽,1 -> 拖拽中,2-> 完成拖拽
  22. drag = 0


  23. #获取图像的位置矩形
  24. position = turtle.get_rect()

  25. #移动乌龟到相对中央的位置
  26. for i in range(len(position)):
  27.    position[i] = position[i] + 180


  28. while True:
  29.     for event in pygame.event.get():
  30.         if event.type == pygame.QUIT:
  31.             sys.exit()
  32.             
  33.         elif event.type == pygame.MOUSEBUTTONDOWN:
  34.              if event.button == 1:
  35.                 #第一次点击,选择范围
  36.                 if select == 0 and drag == 0:
  37.                    pos_strat = event.pos  #获取鼠标当前位置
  38.                    select =1

  39.                 #第二次点击,拖拽图像
  40.                 elif select == 2 and drag == 0:
  41.                    capture = screen.subsurface(select_rect).copy()  #获取子对象
  42.                    capture_rect = capture.get_rect()                #获取子对象的位置
  43.                    drag = 1
  44.                 #第三次点击,初始化
  45.                 elif select == 2 and drag == 2:
  46.                    select = 0
  47.                    drag = 0

  48.         elif event.type ==  pygame.MOUSEBUTTONUP:
  49.             #第一次释放,结束选择
  50.             if event.button == 1:
  51.                pos_stop = event.pos  #获取鼠标当前位置
  52.                select =2
  53.             #第二次释放,结束拖拽
  54.             if select == 2 and drag == 1:
  55.                drag == 2

  56.     #填充背景色
  57.     screen.fill(bg)

  58.     #更新图片,blit将一个图像画到另一个图像上去,a_cartoon画到screen)
  59.     screen.blit(turtle,position)


  60.     #实时绘制选择框
  61.     if select:   #select != 0
  62.        mouse_pos = pygame.mouse.get_pos()    #得到鼠标当前的位置
  63.        if select == 1:
  64.           pos_stop = mouse_pos  #获取鼠标当前位置
  65.        select_rect.left,select_rect.top = pos_strat
  66.        select_rect.width,select_rect.height = pos_stop[0]-pos_strat[0],pos_stop[1]-pos_strat[1]
  67.        pygame.draw.rect(screen,(0,0,0),select_rect,1)  #画框
  68.     #拖拽剪裁的图像
  69.     if drag:
  70.        if drag == 1:
  71.           capture_rect.center = mouse_pos     #鼠标的位置实时在图片的中心
  72.        screen.blit(capture,capture_rect)      #将子图像绘制到屏幕上
  73.       
  74.     #填充背景色
  75.     screen.fill(bg)

  76.     #更新图片,blit将一个图像画到另一个图像上去,a_cartoon画到screen)
  77.     screen.blit(turtle,position)

  78.     # 更新界面
  79.     pygame.display.flip()

  80.     #延迟
  81.     pygame.time.delay(10)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-23 13:52:47 | 显示全部楼层
猪猪虾 发表于 2020-4-23 13:44
clock = pygame.time.Clock()
请教一下,这句是干啥

设置帧数响应器
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-23 13:53:15 | 显示全部楼层
猪猪虾 发表于 2020-4-23 13:51
为啥我的剪切不了,我都一句一句的对着看了

电邮多少,我给你发源码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-23 13:53:17 | 显示全部楼层
老八秘制 发表于 2020-4-23 13:51
那个是13年的视频,那时候小甲鱼还没进B站呢,而且那个视频弹幕大神比较多,对学习也有帮助

这样啊,
帮我瞅一眼我下面发的代码呗,比对了一遍又一遍,要了源代码比对,还是看不出来哪有问题,
没有任何错误,但是剪切不了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-23 13:54:48 | 显示全部楼层
永恒的蓝色梦想 发表于 2020-4-23 13:50
这都众所周知的事了……

我以为人家侵权了,些许的尴尬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-23 13:54:55 | 显示全部楼层
猪猪虾 发表于 2020-4-23 13:53
这样啊,
帮我瞅一眼我下面发的代码呗,比对了一遍又一遍,要了源代码比对,还是看不出来哪有问题,
没 ...
  1. import pygame
  2. import sys
  3. from pygame.locals import *

  4. pygame.init()

  5. size = width, height = 800, 600
  6. bg = (255, 255, 255)

  7. clock = pygame.time.Clock()
  8. screen = pygame.display.set_mode(size)
  9. pygame.display.set_caption("FishC Demo")

  10. turtle = pygame.image.load("turtle.png")

  11. # 0 -> 未选择,1 -> 选择中,2 -> 完成选择
  12. select = 0
  13. select_rect = pygame.Rect(0, 0, 0, 0)
  14. # 0 -> 未拖拽,1 -> 拖拽中,2 -> 完成拖拽
  15. drag = 0

  16. position = turtle.get_rect()
  17. position.center = width // 2, height // 2

  18. while True:
  19.     for event in pygame.event.get():
  20.         if event.type == QUIT:
  21.             sys.exit()

  22.         elif event.type == MOUSEBUTTONDOWN:
  23.             if event.button == 1:
  24.                 # 第一次点击,选择范围
  25.                 if select == 0 and drag == 0:
  26.                     pos_start = event.pos
  27.                     select = 1
  28.                 # 第二次点击,推拽图像
  29.                 elif select == 2 and drag == 0:
  30.                     capture = screen.subsurface(select_rect).copy()
  31.                     cap_rect = capture.get_rect()
  32.                     drag = 1
  33.                 # 第三次点击,初始化
  34.                 elif select == 2 and drag == 2:
  35.                     select = 0
  36.                     drag = 0

  37.         elif event.type == MOUSEBUTTONUP:
  38.             if event.button == 1:
  39.                 # 第一次释放,结束选择
  40.                 if select == 1 and drag == 0:
  41.                     pos_stop = event.pos
  42.                     select = 2
  43.                 # 第二次释放,结束拖拽
  44.                 if select == 2 and drag == 1:
  45.                     drag = 2
  46.                
  47.     screen.fill(bg)
  48.     screen.blit(turtle, position)
  49.    
  50.     # 实时绘制选择框
  51.     if select:
  52.         mouse_pos = pygame.mouse.get_pos()
  53.         if select == 1:
  54.             pos_stop = mouse_pos

  55.         select_rect.left, select_rect.top = pos_start
  56.         select_rect.width, select_rect.height = pos_stop[0] - pos_start[0], pos_stop[1] - pos_start[1]
  57.         pygame.draw.rect(screen, (0, 0, 0), select_rect,1)

  58.     # 拖拽裁剪的图像
  59.     if drag:
  60.         if drag == 1:
  61.             cap_rect.center = mouse_pos
  62.         screen.blit(capture, cap_rect)
  63.            
  64.     pygame.display.flip()
  65.    
  66.     clock.tick(30)
复制代码

也有可能是电脑的问题,本来pygame对内存消耗就比较大
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-23 13:57:38 | 显示全部楼层
猪猪虾 发表于 2020-4-23 13:48
我无意间发现B站上面有小甲鱼课程的盗版,有点侵权了,账号叫“”IT的搬运工“,很清晰,论坛上也可以直接 ...

知道,这个很早就有了,估计小甲鱼也知道
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-16 08:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表