-
Ensure you have Python on your machine: Download Python
-
Install Pygame using pip:
pip install pygame
-
Verify the installation by running the following Python commands:
import pygame print("Pygame version:", pygame.__version__)
-
To get started, you will first want to initalize a Pygame window:
# https://www.pygame.org/docs/ import pygame # Initialize the pygame library pygame.init() SCREEN_DIMENSIONS = (750, 750) # Creating the base pygame screen screen = pygame.display.set_mode(SCREEN_DIMENSIONS)
-
You may notice that the window only appears briefly. This is because we have not established a render loop yet:
... # Creating the base pygame screen screen = pygame.display.set_mode(SCREEN_DIMENSIONS) # Creating the clock used to frame rate management clock = pygame.time.Clock() running = True while (running): for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Updates the screen pygame.display.flip() clock.tick(60)
-
We now have a proper screen! But what if we want to change the color? We can do this by utilizing the
fill()
command:# Creating the base pygame screen screen = pygame.display.set_mode(SCREEN_DIMENSIONS) # Creating the clock used to frame rate management clock = pygame.time.Clock() running = True while (running): for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Fills the screen in (r, g, b) format screen.fill((255, 0, 0)) # Updates the screen pygame.display.flip() clock.tick(60)
-
Now, lets try adding an external image using the
pygame.image.load()
andblit()
functions:# Creating the base pygame screen screen = pygame.display.set_mode(SCREEN_DIMENSIONS) # Creating the clock used to frame rate management clock = pygame.time.Clock() logo = pygame.image.load('logo.png') running = True while (running): for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Fills the screen in (r, g, b) format screen.fill((255, 0, 0)) # Displays the HackRPI logo in (x, y) format screen.blit(logo, (60, 60)) # Updates the screen pygame.display.flip() clock.tick(60)
-
Now, it is up to you! There are many more features that Pygame presents and I recommend that you explore them! Pygame Documentation
- Name: Anthony Smith | [email protected]