0
votes

I am trying to draw a rectangle on the screen. My code is below:

pygame.draw.rect(screen,(255,0,0),
                    (self.width/2-20,self.height/2-20,40,40),
                    width=0)

Pygame keeps returning the error message:

TypeError: rect() takes no keyword arguments

Can anyone help me with this?

3
I tried both. But it returned the same error message. - Yhqjlyr
Instead of width=0 put only 0 i.e. pygame.draw.rect(screen,(255,0,0), [self.width/2-20,self.height/2-20,40,40], 0). See pygame.org/docs/ref/draw.html#pygame.draw.rect for how to use this function - ask

3 Answers

0
votes

If you look at the pygame documentation, pygame.draw.rect needs which screen to draw to, the color, a Rect object, and the width. You should have pygame.draw.rect(screen, (255, 0, 0), pygame.Rect(self.width/2-20, self.height/2-20, 40, 40), 0) instead. (If you take a look at the comment by Dave Burton on this function, you'll additionally see that you shouldn't put width=0.)

0
votes

I had a similar issue. It ended up being my pygame version even though I thought I installed the latest. I installed it using conda install -c cogsci pygame. This is wrong, since that version has not been updated in at least four years. Instead, install it using pip: pip install pygame.

-1
votes

Are you sure you have all the ingredients? You need an init function an update function also for example. Try something like this:

import pygame

pygame.init()

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

while True:

    screen = pygame.display.set_mode((640, 480))
    screen.fill(BLACK)


    pygame.draw.rect(screen, WHITE, [100, 100, 400, 300])
    pygame.display.update()