T Eom
T Eom

Reputation: 95

Pygame ValueError: invalid rectstyle object

I downloaded a pygame example from here called rabbitone, and followed the corresponding youtube video.

So I have studied the code and tried it:

import pygame

pygame.init()


width, height = 640, 480
screen = pygame.display.set_mode((width, height))

player = pygame.image.load("resources/images/dude.png")


while True:
    screen.fill(0,0,0)
    pygame.display.flip()
    screen.blit(player, (100,100))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit(0)

In the video tutorial I'm following, the code works. Why do I get this error?

Traceback (most recent call last):

File "", line 2, in

ValueError: invalid rectstyle object

Upvotes: 4

Views: 12350

Answers (2)

Mace
Mace

Reputation: 21

I had the same issue but it was in a different situation. I put surface.fill(0,0,0) To fix this, I put surface.fill((0,0,0)) With a pair of extra brackets aroud the colour

Upvotes: 2

skrx
skrx

Reputation: 20438

You're passing three separate integers to the pygame.Surface.fill method, but you have to pass a color tuple (or list or pygame.Color object) as the first argument : screen.fill((0, 0, 0)).

You also need to blit the player between the fill and the flip call, otherwise you'll only see a black screen.


Unrelated to the problem, but you should usually convert your surfaces to improve the performance and add a pygame.time.Clock to limit the frame rate.

import pygame


pygame.init()

width, height = 640, 480
screen = pygame.display.set_mode((width, height))
# Add a clock to limit the frame rate.
clock = pygame.time.Clock()

# Convert the image to improve the performance (convert or convert_alpha).
player = pygame.image.load("resources/images/dude.png").convert_alpha()

running = True
while running:
    # Handle events.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Insert the game logic here.

    # Then draw everything, flip the display and call clock tick.
    screen.fill((0, 0, 0))
    screen.blit(player, (100, 100))
    pygame.display.flip()
    clock.tick(60)  # Limit the frame rate to 60 FPS.

pygame.quit()

Upvotes: 8

Related Questions