Click here to Skip to main content
15,887,294 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I just installed python and Pygame, so I know it's not an issue with it being outdated. But every time I try to run the code's on the pygame website, it comes up black. When I try to close the screen, a window opens up saying it's not working and in order to fully close it I have to close Python directly.
Is it my computer? Or am I just stupid?

What I have tried:

Python
# Example file showing a basic pygame "game loop"
import pygame

# pygame setup
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
running = True

while running:
    # poll for events
    # pygame.QUIT event means the user clicked X to close your window
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # fill the screen with a color to wipe away anything from last frame
    screen.fill("purple")

    # RENDER YOUR GAME HERE

    # flip() the display to put your work on screen
    pygame.display.flip()

    clock.tick(60)  # limits FPS to 60

pygame.quit()
Posted
Updated 28-Jul-23 5:13am
v2

Probably because you haven't put any code in the section marked:
Python
# RENDER YOUR GAME HERE


With no code there, there is nothing to show, so it sits waiting for the user to get bored and go away ...
 
Share this answer
 
Comments
Andre Oosthuizen 28-Jul-23 10:59am    
:)
You need to understand infinite loops; see The Python Tutorial — Python 3.10.12 documentation[^].
 
Share this answer
 
The 'screen.fill()' method in pygame expects a color in the form of an RGB tuple (Red, Green, Blue) or an integer value representing a color. However, you are passing a string ("purple") instead, which is causing the problem - Introduction to Pygame - Drawing a background[^]

You can also see - How to change screen background color in Pygame?[^].

You can use a RGB Color Picker found at - HTML Color Picker[^]

Your code should thus be -
Python
#OLD CODE - fill the screen with a color to wipe away anything from last frame
    screen.fill("purple")

#NEW CODE - fill the screen with a color to wipe away anything from last frame
    screen.fill((128, 0, 128))#Purple as you can get it...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900