2017-04-10 27 views
1

我目前正在研究一個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

任何想法?

+0

嘗試使用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

+0

它工作得很好!我不敢相信我沒有聽到 - 謝謝你! –

回答

0

的錯誤是使用self.image_rect_h*x作爲pygame.Rect的第一個參數(在x座標),將其更改爲self.image_rect_w*x固定它。

self.sheet.set_clip(pygame.Rect(self.image_rect_w*x, 0, self.image_rect_w, self.image_rect_h)) 

我不知道爲什麼會出現錯誤,因爲pygame.Surface.set_clip.get_clip應僅限於表面區域。你應該張貼完整回溯(錯誤消息)。

我實際上認爲如果圖像加載和剪切出現問題,讓程序崩潰會更好,以便更容易找到錯誤,否則無論如何你都會得到不正確的精靈。所以你可以使用pygame.Surface.subsurface而不是set_clipget_clip

rect = (self.image_rect_w*x, 0, self.image_rect_w, self.image_rect_h) 
sprite = self.sheet.subsurface(rect)