在 Pygame 中使用 get_clip() 时,表面区域外的次表面矩形? [英] Subsurface rect outside of surface area when using get_clip() in Pygame?
问题描述
我目前正在开发一个 pygame,试图为我的角色设置动画,以便当玩家移动他时,程序将在四个精灵图像次表面循环.我已经为此设置了一个类:
I am currently working on a pygame, trying to animate my character so that as the player moves him the program will cycle through four sprite image subsurfaces. I already setup a class for this:
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 改变颜色的方形精灵)时,它工作得很好,但是当我尝试实现我的(部分完整的)实际角色表时,我又回来了
And it worked perfectly when I used a dummy sprite sheet (just a simple 50 x 50 square sprite that changes colors), but when I tried to implement my (partially complete) actually character sheet, I got back
ValueError: subsurface rectangle outside surface area
我不确定是因为纸张的大小(虚拟纸张是 832 x 52 像素,字符表是 1008 x 79 像素)还是什么,我似乎找不到任何解决此问题的文章.(我在快速搜索中找到的最接近的是 如何在pygame中旋转图像
I am not sure if it's the size of sheets (the dummy sheet was 832 x 52px, and the character sheet is 1008 x 79px), or what, and I can't seem to find any article that addresses this issue. (The closest I could find in a quick search was How to rotate images in pygame
有什么想法吗?
推荐答案
错误是使用 self.image_rect_h*x
作为 pygame.Rect 的第一个参数(x 坐标).将其更改为 self.image_rect_w*x
修复它.
The mistake was to use self.image_rect_h*x
as the first argument (the x-coord) of the pygame.Rect. Changing it to self.image_rect_w*x
fixed it.
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
应该被限制在表面区域.您应该发布完整的回溯(错误消息).
I'm not sure why the error occurs, because pygame.Surface.set_clip
and .get_clip
should be restricted to the surface area. You should post the full traceback (error message).
我实际上认为如果图像加载和剪切有问题,让程序崩溃会更好,这样更容易找到错误,否则无论如何你都会得到不正确的精灵.所以你可以使用 pygame.Surface.subsurface
而不是 set_clip
和 get_clip
.
I actually think it would be better to let the program crash if something is wrong with the image loading and cutting, so that it's easier to find the error, otherwise you would get incorrect sprites anyway. So you could just use pygame.Surface.subsurface
instead of set_clip
andget_clip
.
rect = (self.image_rect_w*x, 0, self.image_rect_w, self.image_rect_h)
sprite = self.sheet.subsurface(rect)
这篇关于在 Pygame 中使用 get_clip() 时,表面区域外的次表面矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!