我目前正在研究一個pygame,試圖爲我的角色製作動畫,這樣當玩家移動他時,程序將循環通過四個精靈圖像子表面。我已經建立了這個類:在Pygame中使用get_clip()時,地表以外的表面區域?
import pygame
from pygame.locaks import *
class Prota:
def __init__(self, sheet):
self.sheet = pygame.image.load(sheet).convert_alpha()
self.image_rect = self.sheet.get_rect()
self.image_rect_h = (self.image_rect.height) #num of rows
self.image_rect_w = (self.image_rect.width/16) #num of columns
self.image_reel = self.fetch()
self.image_cue = 0 #index: 0 - 3 (Right), 4 - 7 (Left), 8 - 11 (Front), 12 - 15 (Back)
self.clock = pygame.time.Clock()
def draw(self, screen):
self.clock.tick(60)
screen.blit(self.image_reel[self.image_cue], (400, 300))
def fetch(self):
sprites = []
for x in range(0, 15):
self.sheet.set_clip(pygame.Rect(self.image_rect_h*x, 0, self.image_rect_w, self.image_rect_h))
sprite = self.sheet.subsurface(self.sheet.get_clip())
sprites.append(sprite)
return sprites
當我用一個虛擬的精靈表(只是一個簡單的50×50平方精靈改變顏色),但它的工作完美,當我試圖實現我的(部分完成)實際字符表,我回到
ValueError: subsurface rectangle outside surface area
我不知道這是否是紙張的尺寸(虛置片材爲832 X 52px,和字符表是1008 X 79px),或者是什麼,我可以似乎沒有找到任何解決這個問題的文章。 (我能找到一個快速搜索最近的是How to rotate images in pygame
任何想法?
嘗試使用self.image_rect_w作爲第一個參數。 'self.sheet.set_clip(pygame.Rect(self.image_rect_w * x,0,self.image_rect_w,self.image_rect_h))' – skrx
它工作得很好!我不敢相信我沒有聽到 - 謝謝你! –