鱼C论坛

 找回密码
 立即注册
查看: 1452|回复: 2

pygame

[复制链接]
发表于 2021-10-16 11:06:05 | 显示全部楼层 |阅读模式

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

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

x
求大神们帮忙看看这段代码出啥问题了,第二次点击的时候总是显示错误

# 小游戏 turtle 裁剪

import pygame
import sys
from pygame.locals import *

# 初始化pygame
pygame.init()

size = width, height = 600, 400
bg = (255, 255, 255)  # RGB

# 创建指定大小的窗口 返回一个Surface的对象
screen = pygame.display.set_mode(size)
# 设置窗口标题
pygame.display.set_caption('初次见面,请多多关照')

# 加载图片
turtle = pygame.image.load('new_turtle.jpg')
# 获取图像的位置矩形
position = turtle.get_rect(center=(300, 200))

# select=0 未选择 select=1 正在选择 select=2 选择完成
select = 0
select_rect = pygame.Rect(0, 0, 0, 0)
# drag=0 未拖拽 drag=1 正在拖拽 drag=2 拖拽完成
drag = 0


while True:
    # 每次操作都是一个事件
    for event in pygame.event.get():
        if event.type == pygame.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
                # 第二次释放,拖拽完成
                elif 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()




C:\ProgramData\Anaconda3\python.exe D:/PycharmProjects/pythonProject/pygame/game_turtle/turtle4.py
pygame 2.0.2 (SDL 2.0.16, Python 3.8.8)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "D:/PycharmProjects/pythonProject/pygame/game_turtle/turtle4.py", line 44, in <module>
    capture = screen.subsurface(select_rect).copy()
pygame.error: Out of memory

Process finished with exit code 1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-17 10:08:43 | 显示全部楼层
  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.     screen.fill(bg)
  47.     screen.blit(turtle, position)

  48.     # 实时绘制选择框
  49.     if select:
  50.         mouse_pos = pygame.mouse.get_pos()
  51.         if select == 1:
  52.             pos_stop = mouse_pos

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

  56.     # 拖拽裁剪的图像
  57.     if drag:
  58.         if drag == 1:
  59.             cap_rect.center = mouse_pos
  60.         screen.blit(capture, cap_rect)

  61.     pygame.display.flip()

  62. clock.tick(30)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-17 10:29:17 | 显示全部楼层

感谢,照着你的代码改了以后,可以正常运行了,但是感觉两个代码貌似没太大差别,不知道到底是哪里的问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-14 17:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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