猪猪虾 发表于 2020-4-23 13:34:05

82讲,谁有剪切乌龟,能拖拽的源代码啊

分享一下

wuqramy 发表于 2020-4-23 13:36:14

好的好的
import pygame
import pygame.transform
import sys
from pygame.locals import *
pygame.init()
size = width,height = 400,350
bg = (255,255,255)
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption('cutting turtle')
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 - pos_start,pos_stop - pos_start
      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()
    # 延时10毫秒
    #pygame.time.delay(10)
    # 设置帧数
    clock.tick(30)

weiter 发表于 2020-4-23 13:36:28

找着课程自己撸……(我一直都这么搞得)

猪猪虾 发表于 2020-4-23 13:41:27

weiter 发表于 2020-4-23 13:36
找着课程自己撸……(我一直都这么搞得)

感谢建议

老八秘制 发表于 2020-4-23 13:41:55

电邮多少,给你发

猪猪虾 发表于 2020-4-23 13:44:13

wuqramy 发表于 2020-4-23 13:36
好的好的

clock = pygame.time.Clock()
请教一下,这句是干啥

weiter 发表于 2020-4-23 13:46:34

猪猪虾 发表于 2020-4-23 13:41
感谢建议

唯一的问题是我没有b站账号,经常看不清甲鱼老师撸的什么……(有账号的直接照抄就好,或者截图,自动扣字)

猪猪虾 发表于 2020-4-23 13:48:55

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

我无意间发现B站上面有小甲鱼课程的盗版,有点侵权了,账号叫“”IT的搬运工“,很清晰,论坛上也可以直接看

猪猪虾 发表于 2020-4-23 13:49:45

老八秘制 发表于 2020-4-23 13:41
电邮多少,给你发

好了好了,你换头像了
谢谢谢谢

永恒的蓝色梦想 发表于 2020-4-23 13:50:10

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

这都众所周知的事了……

永恒的蓝色梦想 发表于 2020-4-23 13:50:45

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

注册一个不就好了,又不要钱{:10_248:}

老八秘制 发表于 2020-4-23 13:51:23

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

那个是13年的视频,那时候小甲鱼还没进B站呢,而且那个视频弹幕大神比较多,对学习也有帮助

猪猪虾 发表于 2020-4-23 13:51:26

老八秘制 发表于 2020-4-23 13:41
电邮多少,给你发

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

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


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

size= width,height = 600,600
speed =    #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 = position + 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-pos_strat,pos_stop-pos_strat
       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)

wuqramy 发表于 2020-4-23 13:52:47

猪猪虾 发表于 2020-4-23 13:44
clock = pygame.time.Clock()
请教一下,这句是干啥

设置帧数响应器

老八秘制 发表于 2020-4-23 13:53:15

猪猪虾 发表于 2020-4-23 13:51
为啥我的剪切不了,我都一句一句的对着看了

电邮多少,我给你发源码

猪猪虾 发表于 2020-4-23 13:53:17

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

这样啊,
帮我瞅一眼我下面发的代码呗,比对了一遍又一遍,要了源代码比对,还是看不出来哪有问题,
没有任何错误,但是剪切不了

猪猪虾 发表于 2020-4-23 13:54:48

永恒的蓝色梦想 发表于 2020-4-23 13:50
这都众所周知的事了……

我以为人家侵权了,些许的尴尬

老八秘制 发表于 2020-4-23 13:54:55

猪猪虾 发表于 2020-4-23 13:53
这样啊,
帮我瞅一眼我下面发的代码呗,比对了一遍又一遍,要了源代码比对,还是看不出来哪有问题,
没 ...

import pygame
import sys
from pygame.locals import *

pygame.init()

size = width, height = 800, 600
bg = (255, 255, 255)

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

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()
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 - pos_start, pos_stop - pos_start
      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)

也有可能是电脑的问题,本来pygame对内存消耗就比较大

weiter 发表于 2020-4-23 13:57:38

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

知道,这个很早就有了,估计小甲鱼也知道
页: [1]
查看完整版本: 82讲,谁有剪切乌龟,能拖拽的源代码啊