Skip to content

Commit

Permalink
chore: fix backdrop, and add image possibility (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
koen1711 authored Jan 8, 2025
1 parent 201afd3 commit 1d896f7
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 39 deletions.
19 changes: 11 additions & 8 deletions play/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
from ..loop import loop as _loop
from ..utils import color_name_to_rgb as _color_name_to_rgb
from ..io.keypress import _pressed_keys
from ..globals import backdrop as __backdrop
from ..globals import globals_list
from ..physics import set_physics_simulation_steps as _set_physics_simulation_steps

_backdrop = __backdrop # Work around for the global variable not being imported


def start_program():
"""
Expand Down Expand Up @@ -57,14 +55,19 @@ async def animate():
await _asyncio.sleep(0)


def set_backdrop(color_or_image_name):
def set_backdrop(color):
"""Set the backdrop color or image for the game.
:param color_or_image_name: The color or image to set as the backdrop.
:param color: The color or image to set as the backdrop.
"""
global _backdrop
_color_name_to_rgb(color_or_image_name)
globals_list.backdrop = _color_name_to_rgb(color)


_backdrop = color_or_image_name
def set_backdrop_image(image):
"""Set the backdrop image for the game.
:param image: The image to set as the backdrop.
"""
globals_list.backdrop = pygame.image.load(image)
globals_list.backdrop_type = "image"


async def timer(seconds=1.0):
Expand Down
14 changes: 11 additions & 3 deletions play/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .sprites_loop import _update_sprites
from ..callback import callback_manager, CallbackType
from ..callback.callback_helpers import run_callback
from ..globals import backdrop, FRAME_RATE, _walls
from ..globals import globals_list
from ..io import screen, PYGAME_DISPLAY, convert_pos
from ..io.keypress import (
key_num_to_name as _pygame_key_to_name,
Expand Down Expand Up @@ -147,7 +147,7 @@ def game_loop():
mouse_state.click_happened_this_frame = False
mouse_state.click_release_happened_this_frame = False

_clock.tick(FRAME_RATE)
_clock.tick(globals_list.FRAME_RATE)

if not _handle_pygame_events():
return False
Expand Down Expand Up @@ -179,7 +179,15 @@ def game_loop():
#############################
_loop.call_soon(simulate_physics)

PYGAME_DISPLAY.fill(_color_name_to_rgb(backdrop))
if globals_list.backdrop_type == "color":
PYGAME_DISPLAY.fill(globals_list.backdrop)
elif globals_list.backdrop_type == "image":
PYGAME_DISPLAY.blit(
globals_list.backdrop,
(0, 0),
)
else:
PYGAME_DISPLAY.fill((255, 255, 255))

_update_sprites()

Expand Down
4 changes: 2 additions & 2 deletions play/core/physics_loop.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""This module contains the function that simulates the physics of the game"""

from ..globals import FRAME_RATE
from ..globals import globals_list
from ..physics import physics_space, _NUM_SIMULATION_STEPS
from .sprites_loop import _update_sprites

Expand All @@ -12,5 +12,5 @@ def simulate_physics():
# more steps means more accurate simulation, but more processing time
for _ in range(_NUM_SIMULATION_STEPS):
# the smaller the simulation step, the more accurate the simulation
physics_space.step(1 / (FRAME_RATE * _NUM_SIMULATION_STEPS))
physics_space.step(1 / (globals_list.FRAME_RATE * _NUM_SIMULATION_STEPS))
_update_sprites(True)
8 changes: 4 additions & 4 deletions play/core/sprites_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import math as _math

from play.globals import sprites_group
from play.globals import globals_list
from .mouse_loop import mouse_state
from ..callback import callback_manager, CallbackType
from ..callback.callback_helpers import run_callback
Expand All @@ -15,9 +15,9 @@

def _update_sprites(skip_user_events=False): # pylint: disable=too-many-branches
# pylint: disable=too-many-nested-blocks
sprites_group.update()
globals_list.sprites_group.update()

for sprite in sprites_group.sprites():
for sprite in globals_list.sprites_group.sprites():

######################################################
# update sprites with results of physics simulation
Expand Down Expand Up @@ -84,4 +84,4 @@ def _update_sprites(skip_user_events=False): # pylint: disable=too-many-branche
[],
)

sprites_group.draw(PYGAME_DISPLAY)
globals_list.sprites_group.draw(PYGAME_DISPLAY)
20 changes: 13 additions & 7 deletions play/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

import pygame

all_sprites = []
sprites_group = pygame.sprite.Group()

_walls = []
class Globals: # pylint: disable=too-few-public-methods
all_sprites = []
sprites_group = pygame.sprite.Group()

backdrop = (255, 255, 255)
walls = []

FRAME_RATE = 60
WIDTH = 800
HEIGHT = 600
backdrop_type = "color" # color or image
backdrop = (255, 255, 255)

FRAME_RATE = 60
WIDTH = 800
HEIGHT = 600


globals_list = Globals()
20 changes: 10 additions & 10 deletions play/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
from pygame._sdl2.video import Window # pylint: disable=no-name-in-module
from pygame.locals import *

from ..globals import _walls, WIDTH, HEIGHT
from ..globals import globals_list
from ..physics import physics_space

PYGAME_DISPLAY = None


class Screen:
def __init__(self, width=WIDTH, height=HEIGHT):
def __init__(self, width=globals_list.WIDTH, height=globals_list.HEIGHT):
global PYGAME_DISPLAY

self._width = width
Expand Down Expand Up @@ -149,33 +149,33 @@ def _create_wall(a, b):

def create_walls():
"""Create walls around the screen."""
_walls.append(
globals_list.walls.append(
_create_wall([screen.left, screen.top], [screen.right, screen.top])
) # top
_walls.append(
globals_list.walls.append(
_create_wall([screen.left, screen.bottom], [screen.right, screen.bottom])
) # bottom
_walls.append(
globals_list.walls.append(
_create_wall([screen.left, screen.bottom], [screen.left, screen.top])
) # left
_walls.append(
globals_list.walls.append(
_create_wall([screen.right, screen.bottom], [screen.right, screen.top])
) # right


def remove_walls():
"""Remove the walls from the physics space."""
for wall in _walls:
for wall in globals_list.walls:
physics_space.remove(wall)
_walls.clear()
globals_list.walls.clear()


def remove_wall(index):
"""Remove a wall from the physics space.
:param index: The index of the wall to remove. 0: top, 1: bottom, 2: left, 3: right.
"""
physics_space.remove(_walls[index])
_walls.pop(index)
physics_space.remove(globals_list.walls[index])
globals_list.walls.pop(index)


create_walls()
Expand Down
8 changes: 4 additions & 4 deletions play/objects/sprite.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pygame

from ..callback import callback_manager, CallbackType
from ..globals import sprites_group, _walls
from ..globals import globals_list
from ..physics import physics_space, Physics as _Physics
from ..utils import _clamp
from ..io import screen
Expand Down Expand Up @@ -55,7 +55,7 @@ def __init__(self, image=None):
self.rect = None

super().__init__()
sprites_group.add(self)
globals_list.sprites_group.add(self)

def __setattr__(self, name, value):
# ignore if it's in the ignored list or if the variable doesn't change
Expand All @@ -68,7 +68,7 @@ def __setattr__(self, name, value):
def is_touching_wall(self) -> bool:
"""Check if the sprite is touching the edge of the screen.
:return: Whether the sprite is touching the edge of the screen."""
for wall in _walls:
for wall in globals_list.walls:
if self.physics._pymunk_shape.shapes_collide(wall).points:
return True
return False
Expand Down Expand Up @@ -358,7 +358,7 @@ def remove(self):
"""Remove the sprite from the screen."""
if self.physics:
self.physics._remove()
sprites_group.remove(self)
globals_list.sprites_group.remove(self)

@property
def width(self):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# This call to setup() does all the work
setup(
name="corderius-play",
version="2.3.2",
version="2.4.0",
description="The easiest way to make games and media projects in Python.",
long_description=README,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 1d896f7

Please sign in to comment.