【问题标题】:Subsurface ValueError: 'subsurface rectangle outside surface area'地下 ValueError:\'表面积外的地下矩形\'
【发布时间】:2022-12-15 02:49:36
【问题描述】:

我正在尝试获取 spritesheet 的图像并将它们添加到动画词典中。

看来我很愚蠢或者不了解次表面是如何工作的,因为我真的不明白为什么会出现此错误:

ValueError: subsurface rectangle outside surface area

这是我的简化代码:

import pygame as pg
pg.init()

animations = {"animation": []}
sprite_frame_number = 18

img = pg.Surface((1440, 80))  # that would be the sprite sheet
size = [int(img.get_width() / sprite_frame_number), img.get_height()]  # so in this case size = [80,80]

for x in range(sprite_frame_number):
    frame_location = [size[0] * x, 0]  # so starting with 0, x moves with each iteration 80 pxl to the right
    img_rect = pg.Rect(frame_location, size)
    
    try:  # i used this to see when it starts to crash
        img = img.subsurface(img_rect)
    except ValueError:
        print(x)        
    
    animations["animation"].append(img)
print(animations)

x '1' 到 '17' 的 ValueError 打印。所以它在创建一个地下后崩溃了,对吧?

print(animations){'idle': [<Surface(80x80x32 SW)>,...] 一起显示我的字典中有 18 个表面。

首先怎么可能有一个创建的矩形位于表面区域之外,其次为什么在 dict 说不可能的时候有 18 个表面? 我很困惑。

【问题讨论】:

  • 它仅在第二次创建第二个地下后才崩溃,因为索引从 0 开始。
  • 是的,它创建了第一个索引为 0 的,然后错误命中并打印 1 到 17
  • 我现在明白为什么 dict 充满了 18 个表面。它从顶部获取 img,因为地下不起作用
  • 或者是吗?打印的表面是 80x80...我真的很困惑

标签: python python-3.x pygame


【解决方案1】:

问题是您对原始图像和地下使用了相同的变量img。第一次,一切仍然有效,因为存储在img变量中的表面仍然是原始表面。然而,第二个,这个表面被第一个地下取代。这个表面不像原来的表面那么大,导致矩形在表面区域之外。

解决这个问题的方法是创建一个新变量来存储地下而不是img。这可以是您想要的任何其他名称,但我会选择new_img

import pygame as pg
pg.init()

animations = {"animation": []}
sprite_frame_number = 18

img = pg.Surface((1440, 80))  # that would be the sprite sheet
size = [int(img.get_width() / sprite_frame_number), img.get_height()]  # so in this case size = [80,80]

for x in range(sprite_frame_number):
    frame_location = [size[0] * x, 0]  # so starting with 0, x moves with each iteration 80 pxl to the right
    img_rect = pg.Rect(frame_location, size)
    
    new_img = img.subsurface(img_rect)  # not the same variable as img
    animations["animation"].append(new_img)
    
print(animations)

【讨论】:

  • 哦,你是绝对正确的!太感谢了!我怎么看不到那个 aiaiai
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-23
  • 2019-07-12
  • 2021-03-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多