3
votes

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

2
Could you link to the source you are copying from? - ScottMcC
Have you cloned the repo and are trying to run it locally? - ScottMcC
player = pygame.image.load("/Users/eomtae/Desktop/Python tutorial 1/rabbitone-master/resources/images/dude.png") This is my code.. Is it right? - T Eom

2 Answers

8
votes

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()
1
votes

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