[英]subsurface ValueError: 'subsurface rectangle outside surface area'
我正在嘗試獲取精靈表的圖像並將其添加到 animation 字典中。
看來我很愚蠢或不明白地下是如何工作的,因為我真的不明白為什么會出現這個錯誤:
ValueError:表面區域外的次表面矩形
這是我的簡單代碼:
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)
ValueError 打印x '1' 到 '17'。 所以它在創建一個地下后崩潰,對嗎?
print(animations)
顯示{'idle': [<Surface(80x80x32 SW)>,...]
我的字典中有 18 個表面。
首先,怎么可能有一個在表面區域之外創建的矩形,其次,當它說不可能時,為什么字典中有 18 個表面? 我很困惑。
問題是您對原始圖像和地下圖像使用相同的變量img
。 第一次,一切都還在工作,因為存儲在img
變量中的表面仍然是原始表面。 然而,第二個,這個表面被第一個地下取代。 這個表面沒有原始表面那么大,導致矩形在表面區域之外。
解決這個問題的方法是創建一個新變量來存儲地下而不是img
。 這可以是您想要的任何其他名稱,但我會為 new_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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.