Pygame: 绘制矩形的奇怪行为

Pygame: strange behaviour of a drawn rectangle

我正在尝试为 Pygame 中的游戏制作生命条 class。我已经这样做了:

class Lifebar():
    def __init__(self, x, y, max_health):
        self.x = x
        self.y = y
        self.health = max_health
        self.max_health = max_health

    def update(self, surface, add_health):
        if self.health > 0:
            self.health += add_health
            pygame.draw.rect(surface, (0, 255, 0), (self.x, self.y, 30 - 30 * (self.max_health - self.health) / self.max_health, 10))


    print(30 - 30 * (self.max_health - self.health) / self.max_health)

它可以工作,但是当我尝试将它的生命值降低到零时,矩形超出了左边的限制一点点。为什么会这样?

这里有一个代码可以自己尝试(如果我对问题的解释不清楚,就 运行):

import pygame
from pygame.locals import *
import sys

WIDTH = 640
HEIGHT = 480

class Lifebar():
    def __init__(self, x, y, max_health):
        self.x = x
        self.y = y
        self.health = max_health
        self.max_health = max_health

    def update(self, surface, add_health):
        if self.health > 0:
            self.health += add_health
            pygame.draw.rect(surface, (0, 255, 0), (self.x, self.y, 30 - 30 * (self.max_health - self.health) / self.max_health, 10))
        print(30 - 30 * (self.max_health - self.health) / self.max_health)

def main():
    pygame.init()

    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption("Prueba")


    clock = pygame.time.Clock()

    lifebar = Lifebar(WIDTH // 2, HEIGHT // 2, 100)

    while True:
        clock.tick(15)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        screen.fill((0,0,255))

        lifebar.update(screen, -1)

        pygame.display.flip()

if __name__ == "__main__":
    main()  

我认为这是因为您的代码绘制了宽度小于 1 像素的矩形,即使 pygame documentation 表示 "The area covered by a Rect does not include the right- and bottom-most edge of pixels",显然这意味着它总是 does 包括最左边和最上面的边缘,这就是给出结果的原因。这可以说是一个错误——在那些情况下它不应该绘制任何东西。

下面是一种解决方法,它可以简单地避免绘制 Rect 宽度小于整个像素的图像。我还简化了一些数学运算,使事情更清晰(更快)。

    def update(self, surface, add_health):
        if self.health > 0:
            self.health += add_health
            width = 30 * self.health/self.max_health
            if width >= 1.0:
                pygame.draw.rect(surface, (0, 255, 0), 
                                 (self.x, self.y, width, 10))
                print(self.health, (self.x, self.y, width, 10))