鱼C论坛

 找回密码
 立即注册
查看: 2698|回复: 1

pygame课的裁剪问题

[复制链接]
发表于 2022-8-31 19:21:09 | 显示全部楼层 |阅读模式

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

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

x
import pygame
import sys
from pygame.locals import *

pygame.init()

size = width,height = 800,600
screen = pygame.display.set_mode(size)
bg = (255,255,255)

turtle = pygame.image.load('turtle.png')
pygame.display.set_caption("小裁缝")

#获取图形当前位置
position = turtle.get_rect()
#修改图形位置,使其居中
position.center = width // 2, height // 2

clock = pygame.time.Clock()

#选择边框
select = 0
#拖拽边框
drag = 0
#先初始化一个矩形
rectangle = pygame.Rect(0,0,0,0)

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


        if event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                if select == 0 and drag == 0:
                    mouse_rpos = event.pos #得到开始点击鼠标时的坐标
                    select = 1
                elif select == 2 and drag == 0:
                    c_rec = screen.subsurface(rectangle).copy()
                    c_rec_pos = c_rec.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: #当鼠标点击时,开始画图
        rect_pos = pygame.mouse.get_pos()
        if select == 1:#第一次点击时
           pos_stop  = rect_pos
        rectangle.left,rectangle.top = mouse_rpos
        rectangle.width,rectangle.height = pos_stop[0] - mouse_rpos[0],pos_stop[1] - mouse_rpos[1]
        pygame.draw.rect(screen, (0, 0, 0), rectangle, 1)


    if drag:
        if drag == 1:#正在拖拽
            c_rec_pos.center= rect_pos
        screen.blit(c_rec,c_rec_pos)


    pygame.display.flip()
    clock.tick(30)
为什么我的图形裁剪完之后松开鼠标,图形仍跟随鼠标
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-8-31 19:36:12 | 显示全部楼层
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[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()
   
    clock.tick(30)
源码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-28 16:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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