66 lines
1.5 KiB
Python
66 lines
1.5 KiB
Python
# Code is taken from https://realpython.com/pygame-a-primer/
|
|
|
|
# Import and initialize the pygame library
|
|
import pygame
|
|
|
|
from pygame.locals import (
|
|
K_UP,
|
|
K_DOWN,
|
|
K_LEFT,
|
|
K_RIGHT,
|
|
K_ESCAPE,
|
|
KEYDOWN,
|
|
QUIT,
|
|
)
|
|
|
|
pygame.init()
|
|
|
|
# Define constants for the screen width and height
|
|
SCREEN_WIDTH = 800
|
|
SCREEN_HEIGHT = 600
|
|
|
|
# Create the screen object
|
|
# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
|
|
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
|
|
|
# Set up the drawing window
|
|
|
|
# Variable to keep the main loop running
|
|
running = True
|
|
|
|
# Main loop
|
|
while running:
|
|
# Look at every event in the queue
|
|
for event in pygame.event.get():
|
|
# Did the user hit a key?
|
|
if event.type == KEYDOWN:
|
|
# Was it the Escape key? If so, stop the loop.
|
|
if event.key == K_ESCAPE:
|
|
running = False
|
|
|
|
# Did the user click the window close button? If so, stop the loop.
|
|
elif event.type == QUIT:
|
|
running = False
|
|
|
|
# Fill the background with white
|
|
screen.fill((255, 255, 255))
|
|
|
|
# Create a surface and pass in a tuple containing its length and width
|
|
surf = pygame.Surface((50, 50))
|
|
|
|
# Give the surface a color to separate it from the background
|
|
surf.fill((0, 0, 0))
|
|
rect = surf.get_rect()
|
|
|
|
surf_center = (
|
|
(SCREEN_WIDTH-surf.get_width())/2,
|
|
(SCREEN_HEIGHT-surf.get_height())/2
|
|
)
|
|
|
|
screen.blit(surf, surf_center)
|
|
|
|
# Flip the display
|
|
pygame.display.flip()
|
|
|
|
# Done! Time to quit.
|
|
pygame.quit() |