Skip to content

Easy game

py_simple.easy_game

easy_game is built on top of pygame to simplify the tricky parts of building games.

EasyGameError

Bases: Exception

Raised when a pygame window/game can't be set up.

Wraps whatever pygame raises internally (bad dimensions, display driver issues, etc.) so py_simple functions can fail with one consistent, easy-to-read exception instead of a random builtin or pygame-specific one.

Parameters:

Name Type Description Default
message str

Human-readable description of what went wrong.

required

basic_game_setup(width, height, title='My Game')

Sets up a pygame window and clock in one call, handling the pygame.init(), display, caption, and clock boilerplate every pygame project needs before the game loop can start.

Parameters:

Name Type Description Default
width int

Width of the game window, in pixels.

required
height int

Height of the game window, in pixels.

required
title str

Text shown in the window's title bar. Defaults to "My Game".

'My Game'

Returns:

Name Type Description
tuple tuple

(screen, clock), where screen is the pygame Surface returned by pygame.display.set_mode() and clock is a pygame.time.Clock instance.

Raises:

Type Description
EasyGameError

If pygame fails to initialize or set up the window (e.g. invalid width/height, display driver issue).

Example
from py_simple import basic_game_setup

screen, clock = basic_game_setup(800, 600, "My Game")
import pygame

pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()

check_if_quit()

Checks the pygame event queue for a quit event (e.g. the window's close button), saving you from writing the for event in pygame.event.get() loop yourself every frame.

Returns:

Name Type Description
bool bool

True if a quit event was found in the queue, False otherwise.

Example
from py_simple import check_if_quit

running = True
while running:
    if check_if_quit():
        running = False
import pygame

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

draw_text(screen, text, position, font_size=36, color=(255, 255, 255))

Draws text on a game screen using pygame's default font, saving you from creating a font, rendering text, and blitting it yourself.

Parameters:

Name Type Description Default
screen Surface

The pygame surface to draw on.

required
text str

The text to display.

required
position tuple

(x, y) coordinates for the text's top-left corner, in pixels.

required
font_size int

Size of the text, in pixels. Defaults to 36.

36
color tuple

RGB tuple for the text color. Defaults to white (255, 255, 255).

(255, 255, 255)

Returns:

Type Description
Rect

pygame.Rect: The rectangle covering the text after it is drawn.

Raises:

Type Description
EasyGameError

If pygame cannot create, render, or draw the text.

Example
from py_simple import basic_game_setup, draw_text

screen, clock = basic_game_setup(800, 600)
draw_text(screen, "Score: 10", (20, 20))
import pygame

screen = pygame.display.set_mode((800, 600))
font = pygame.font.Font(None, 36)
text_surface = font.render("Score: 10", True, (255, 255, 255))
screen.blit(text_surface, (20, 20))

fill_background(screen, color=(0, 0, 0))

Fills the entire game screen with a solid background color, saving you from writing screen clearing boilerplate every frame.

Parameters:

Name Type Description Default
screen Surface

The pygame surface to fill.

required
color tuple

RGB tuple for the background color. Defaults to black (0, 0, 0).

(0, 0, 0)

Returns:

Type Description
None

None

Example
from py_simple import basic_game_setup, fill_background

screen, clock = basic_game_setup(800, 600)
fill_background(screen, (30, 30, 30))
import pygame

screen = pygame.display.set_mode((800, 600))
screen.fill((30, 30, 30))

get_mouse_position()

Gets the current position of the mouse cursor, saving you from remembering the exact pygame call.

Returns:

Name Type Description
tuple tuple

(x, y) coordinates of the mouse cursor, in pixels, relative to the top-left corner of the window.

Example
from py_simple import get_mouse_position

x, y = get_mouse_position()
import pygame

x, y = pygame.mouse.get_pos()

is_key_pressed(key_name)

Checks whether a specific keyboard key is currently held down, saving you from remembering key constant imports and state arrays.

Parameters:

Name Type Description Default
key_name str

The name of the key (e.g., "SPACE", "RETURN", "UP").

required

Returns:

Name Type Description
bool bool

True if the specified key is being pressed, False otherwise.

Raises:

Type Description
EasyGameError

If an invalid key name is provided.

Example
from py_simple import is_key_pressed

if is_key_pressed("SPACE"):
    print("Spacebar held down!")
import pygame

keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
    print("Spacebar held down!")

is_left_mouse_button_clicked()

Checks whether the left mouse button is currently held down.

Returns:

Name Type Description
bool bool

True if the left mouse button is being pressed, False otherwise.

Example
from py_simple import is_left_mouse_button_clicked

if is_left_mouse_button_clicked():
    print("Left click!")
import pygame

if pygame.mouse.get_pressed()[0]:
    print("Left click!")

is_middle_mouse_button_clicked()

Checks whether the middle mouse button (scroll wheel) is currently held down.

Returns:

Name Type Description
bool bool

True if the middle mouse button is being pressed, False otherwise.

Example
from py_simple import is_middle_mouse_button_clicked

if is_middle_mouse_button_clicked():
    print("Middle click!")
import pygame

if pygame.mouse.get_pressed()[1]:
    print("Middle click!")

is_right_mouse_button_clicked()

Checks whether the right mouse button is currently held down.

Returns:

Name Type Description
bool bool

True if the right mouse button is being pressed, False otherwise.

Example
from py_simple import is_right_mouse_button_clicked

if is_right_mouse_button_clicked():
    print("Right click!")
import pygame

if pygame.mouse.get_pressed()[2]:
    print("Right click!")

update_screen()

Updates the pygame display, saving you from remembering the exact pygame display refresh call in every game loop.

Returns:

Type Description
None

None

Raises:

Type Description
EasyGameError

If pygame fails to update the display.

Example
from py_simple import update_screen

update_screen()
import pygame

pygame.display.flip()