hyt8853 发表于 2023-1-5 22:21:07

pygame ,提高游戏的颜值 2那个视频中的

import pygame
import sys
from pygame.locals import *

pygame.init()

# 全局变量
# pos_start = []
# pos_stop = []
# mouse_pos = []
# cap_rect = []
# capture = 0

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

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

turtle = pygame.image.load("turtle.jpg")

select_rect = pygame.Rect(0, 0, 0, 0)
select = 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_stop
      pygame.draw.rect(screen, (0, 0, 0), select_rect, 2)
    if drag:
      if drag == 1:
            cap_rect.center = mouse_pos

      screen.blit(capture, cap_rect)

    pygame.display.flip()
    clock.tick(30)






完全看不到选择框, 而且有变量未全局定义警告,怎么回事呢
页: [1]
查看完整版本: pygame ,提高游戏的颜值 2那个视频中的