0

I am learning to make games with pygame, which I am not very familiar with, and everything has been going great. Recently, I got an error which confuses me...

Here is the specific code which gives me the error...

    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)

When I execute my code, I get this error...

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

Can anyone help me?

Thanks!

  • Shouldn't you give only 4 arguments to the 3rd parameter like this `pygame.draw.rect(win,color,[x,y,width,height])`? – Ch3steR Apr 12 '20 at 17:07

1 Answers1

0

The 3th argument of pygame.draw.rect describes the rectangle by its top left position and its width and and height. Thus the 3rd argument has to be a tuple with size 4 (x, y, widht, height).
In your example the tuple size is 5:

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

Since a negative width respectively height doen't make any sense, I suggest the size of the rectangle is ought to be 50x10:

pygame.draw.rect(win, (255, 0, 0), (self.hitbox[0], self.hitbox[1], 50, 10))
Rabbid76
  • 202,892
  • 27
  • 131
  • 174