Skip to content

Instantly share code, notes, and snippets.

@ShawnHymel
Created September 21, 2022 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShawnHymel/eac8211f2a364e865f2c26a4a5e8f6d1 to your computer and use it in GitHub Desktop.
Save ShawnHymel/eac8211f2a364e865f2c26a4a5e8f6d1 to your computer and use it in GitHub Desktop.
PyGame Display Test
"""
PyGame Display Test
Run to see if you can draw things in Pygame. Press 'esc` to exit. Run with
root privileges to draw on external monitor (e.g. from SSH).
"""
import pygame
# Initialize pygame
pygame.init()
# assigning values to X and Y variable
WIDTH = 480
HEIGHT = 480
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 128)
# Create display surface
display_surface = pygame.display.set_mode((WIDTH, HEIGHT))
# Set the pygame window name
pygame.display.set_caption('Show Text')
# Create a rectangle with some text
font = pygame.font.Font('freesansbold.ttf', 32)
text = font.render('PyGame Draw Test', True, RED, BLUE)
textRect = text.get_rect()
textRect.center = (WIDTH // 2, HEIGHT // 2)
# Disable mouse
pygame.event.set_blocked(pygame.MOUSEMOTION)
pygame.mouse.set_visible(False)
# Main game loop
running = True
while running:
# Iterate over all received game events in the queue
for event in pygame.event.get():
# Check for GUI or keystroke exits
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
# Fill surface with solid color
display_surface.fill(WHITE)
# Draw text on text rectangle
display_surface.blit(text, textRect)
# Draws the surface object to the screen.
pygame.display.update()
# Exit
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment