小甲鱼Pygame82讲:提高游戏的颜值(总结笔记)

rect ( Surface, color, Rect, width = 0)

第一个参数为 Surface,确定是要绘制在哪个 Surface 对象上。

第二个参数 color 指定边框的颜色;

第三个参数 Rect 指定矩形的范围;

第四个参数 width 指定边框的大小,默认值为0,表示填充矩形,如果设为1,就表示边框为1像素的大小。

capture = screen.subsurface(select_rect).copy()

无法用 tranform 模块的 chop() 方法来裁剪,可以使用 subsurface() 方法,就相当于创建 surface 的一个子图像,然后将它 copy 出来。

简单粗暴贴代码:

import pygame
import sys
from pygame.locals import *
 
pygame.init()
 
size = width, height = 400, 400
bg = (255, 255, 255)
 
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Python Demo")
 
turtle = pygame.image.load("turtle2.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)#(0,0,0)代表黑线,像素为1,注意此处有BUG,只能向右下角裁剪
 
    # 拖拽裁剪的图像
    if drag:
        if drag == 1:
            cap_rect.center = mouse_pos
        screen.blit(capture, cap_rect)
           
    pygame.display.flip()
    
    clock.tick(30)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值