【问题标题】:pygame draw lifebar with a clipping areapygame 绘制带有剪切区域的 lifebar
【发布时间】:2015-05-18 10:45:25
【问题描述】:

我想用 pygame 绘制一个生命条,使用一个剪切区域(例如,当一半的生命值消失时,将区域限制为一半。) 但即使剪切区域是正确的,我总是能得到完整的图像。

那是我的救生圈课:

class Lifebar():
    def __init__(self,x,y,images,owner):
        self.x=x
        self.y=y
        self.images=images
        self.owner=owner
        self.owner.world.game.addGUI(self)
        self.inter=False

    def getValues(self):
        value1 = 1.0 * self.owner.hitpoints
        value2 = 1.0 * self.owner.maxhitpoints
        return [value1,value2]

    def render(self,surface):
        rendervalues = self.getValues()
        maxwidth = self.images[0].get_width()
        ratio = rendervalues[0] / rendervalues[1]
        actwidth = int(round(maxwidth * ratio))
        surface.blit(self.images[0],(self.x,self.y))
        surface.set_clip(self.x, 0, (self.x+actwidth), 1080)
        surface.blit(self.images[1],(self.x,self.y))
        self.owner.world.game.setclipDefault()
        surface.blit(self.images[2],(self.x,self.y))

我检查了生命值未满,并且剪切区域在 x 方向上受到限制。 (get_clip()) 我不知道我是否误解了 set_clip() 的工作原理,因为我之前只将它用于整个屏幕(部分超出屏幕的对象)

【问题讨论】:

    标签: pygame


    【解决方案1】:

    当您调用.blit() 时,pygame.Surface 对象的.set_clip() 方法设置任何其他表面将被绘制到的区域。这意味着传递的矩形定义目标表面上的新起点以及将要更新的区域的大小。

    剪切图像的特定区域并将其绘制到表面上,您可以将可选矩形作为第三个参数传递给.blit() 方法:

    surface.blit(source_image, #source image (surface) which you want to cut out
                 (destination_x, destination_y), #coordinates on the destination surface
                 (x_coordinate, y_coordinate, width, height)) #"cut out"-rect
    

    我希望这对你有一点帮助:)

    【讨论】:

    • 这是否意味着当生命条开始的剪辑区域开始时我需要使用x=0进行blitting?
    • 最近没有时间做很多python,但我只是尝试了一下,它工作得很好。谢谢。
    猜你喜欢
    • 2011-08-21
    • 2019-09-25
    • 2011-09-10
    • 2010-11-22
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2013-06-18
    • 2018-07-19
    相关资源
    最近更新 更多