15972156903 发表于 2020-3-24 02:58:14

小甲鱼视频第p83 提高游戏的颜值有困惑


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("Danjuan Demo")

turtle = pygame.image.load(r'D:\\image\dj123.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_start - pos_stop,pos_start - pos_stop
      pygame.draw.rect(screen,(0,255,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)




运行代码后,显示出图片,我准备拖拽的时候,会出现一个选择框,然后报错,如下:
Traceback (most recent call last):
File "C:\Users\user\Desktop\python-yf\__pycache__\03.20.03.py", line 40, in <module>
    capture = screen.subsurface(select_rect).copy()
pygame.error: Out of memory

我代码跟小甲鱼对照了一下,应该没问题。请问为什么会报错。。。


2。还有个问题是select_rect.width,select_rect.height = pos_start - pos_stop,pos_start - pos_stop ,这个里面的0和1 是什么意思,也没琢磨清楚,请大家不吝赐教 ,谢谢!!

bigbird0419 发表于 2020-3-24 03:49:47

第二个问题:位置坐标都是x和y,形如的列表,0表示x的值,1表示y的值

一个账号 发表于 2020-3-24 09:30:17

https://fishc.com.cn/thread-115275-1-1.html

kylin121380 发表于 2020-3-24 20:21:53

改成
select_rect.width,select_rect.height = pos_stop - pos_start,pos_stop - pos_start

15972156903 发表于 2020-3-25 02:41:13

bigbird0419 发表于 2020-3-24 03:49
第二个问题:位置坐标都是x和y,形如的列表,0表示x的值,1表示y的值

感谢大佬。。。

15972156903 发表于 2020-3-25 02:41:55

kylin121380 发表于 2020-3-24 20:21
改成
select_rect.width,select_rect.height = pos_stop - pos_start,pos_stop - pos_start

谢谢大佬。。

15972156903 发表于 2020-3-25 02:46:48

一个账号 发表于 2020-3-24 09:30
https://fishc.com.cn/thread-115275-1-1.html

感谢!
页: [1]
查看完整版本: 小甲鱼视频第p83 提高游戏的颜值有困惑