Dovy
Dovy

Reputation: 23

TypeError: argument 1 must be pygame.Surface, not pygame.Rect

pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((width_display,height_display))
pygame.display.set_caption("Ballzy")


ballImg = pygame.draw.circle(screen,(255,0,0),(250,250),36)

def ball(x,y):
    screen.blit(ballImg,(x,y))

I got an error which said

TypeError: argument 1 must be pygame.Surface, not pygame.Rect

inside the ball function. This would work if the ball was an actual image but I need it to be a shape drawn in pygame.

Upvotes: 2

Views: 2595

Answers (1)

scrbbL
scrbbL

Reputation: 456

The Pygame documentation says this about pygame.draw.circle:

pygame.draw.circle()
    draw a circle around a point
    circle(Surface, color, pos, radius, width=0) -> Rect

This means that this function returns a pygame rectangle. Pygame rectangles simply store a x and y coordinate and a width and height. This means that ballImg is a rectangle, not an image. It does not store any image data but rather just an area.

Your function ball blits ballImg to the screen.

def ball(x,y):
    screen.blit(ballImg,(x,y))

The pygame documentation says this about blit:

blit()
    draw one image onto another
    blit(source, dest, area=None, special_flags = 0) -> Rect

Source should be a surface and dest should be a position where the image is blit on to.

In your code, however, you try and blit a rectangle to the screen, which does not work. In fact, when you call pygame.draw.circle(), you have already drawn the circle to the screen. You do not need to blit any images to the screen at all.

If instead of blitting ballImg to the screen in ball(), you simply draw a circle at the x and y coordinates as so, your problem should be fixed:

def ball(x,y):
    pygame.draw.circle(screen, (255,0,0), (x, y), 36)

Upvotes: 3

Related Questions