【问题标题】:Subsurface rect outside of surface area when using get_clip() in Pygame?在 Pygame 中使用 get_clip() 时,表面区域之外的次表面矩形?
【发布时间】: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


【解决方案1】:

错误是使用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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    相关资源
    最近更新 更多