【发布时间】:2017-04-11 17:56:20
【问题描述】:
我目前正在开发一个 pygame,试图为我的角色设置动画,以便当玩家移动他时,程序将在四个精灵图像子表面之间循环。我已经为此设置了一个类:
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 x 50 可改变颜色的方形精灵)时,它工作得很好,但是当我尝试实现我的(部分完成的)实际字符表时,我回来了
ValueError: subsurface rectangle outside surface area
我不确定是纸张大小(虚拟纸张为 832 x 52 像素,字符表为 1008 x 79 像素)还是什么,我似乎找不到任何解决此问题的文章。 (我在快速搜索中找到的最接近的是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)) -
效果很好!我不敢相信我没听懂——谢谢!
标签: python pygame pygame-surface