【发布时间】: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