0

我正在学习用 pygame 制作游戏,这我不是很熟悉,而且一切都很顺利。最近,我遇到了一个让我感到困惑的错误......

这是给我错误的特定代码...

    def draw(self, win):
    self.move()
    if self.visible:
        if self.walkCount + 1 >= 33:  # Since we have 11 images for each animtion our upper bound is 33.
            # We will show each image for 3 frames. 3 x 11 = 33.
            self.walkCount = 0

        if self.vel > 0:  # If we are moving to the right we will display our walkRight images
            win.blit(self.walkRight[self.walkCount // 3], (self.x, self.y))
            self.walkCount += 1

        else:  # Otherwise we will display the walkLeft images
            win.blit(self.walkLeft[self.walkCount // 3], (self.x, self.y))
            self.walkCount += 1

        pygame.draw.rect(win, (255, 0, 0), (self.hitbox[0], self.hitbox[1], -20, 50, 10))
        pygame.draw.rect(win, (0, 255, 0), (self.hitbox[0], self.hitbox[1], -20, 50, 10))
        self.hitbox = (self.x + 17, self.y + 2, 31, 57)

当我执行我的代码时,我得到这个错误......

pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "/Users/user/PycharmProjects/Marvel2/Pepe.py", line 215, in <module>
    redrawGameWindow()
  File "/Users/user/PycharmProjects/Marvel2/Pepe.py", line 133, in redrawGameWindow
    goblin.draw(win)
  File "/Users/user/PycharmProjects/Marvel2/Pepe.py", line 95, in draw
    pygame.draw.rect(win, (255, 0, 0), (self.hitbox[0], self.hitbox[1], -20, 50, 10))
TypeError: Rect argument is invalid

Process finished with exit code 1

谁能帮我?

谢谢!

4

1 回答 1

0

的第三个参数pygame.draw.rect通过其左上角位置及其宽度和高度来描述矩形。因此,第三个参数必须是一个大小为 4 的元组(x, y, widht, height)
在您的示例中,元组大小为 5:

pygame.draw.rect(win, (255, 0, 0), (self.hitbox[0], self.hitbox[1], -20, 50, 10))

由于负宽度和高度没有任何意义,我建议矩形的大小应该是 50x10:

pygame.draw.rect(win, (255, 0, 0), (self.hitbox[0], self.hitbox[1], 50, 10))
于 2020-04-12T17:07:32.477 回答