Skip to content

Commit

Permalink
typehints
Browse files Browse the repository at this point in the history
  • Loading branch information
JaskRendix committed Jan 18, 2024
1 parent da50ec0 commit 6911994
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 64 deletions.
26 changes: 15 additions & 11 deletions apps/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
logger.setLevel(logging.INFO)


def init_screen(width, height):
def init_screen(width: int, height: int) -> pygame.Surface:
"""Set the screen mode
This function is used to handle window resize events
"""
return pygame.display.set_mode((width, height), pygame.RESIZABLE)


class TiledRenderer(object):
class TiledRenderer:
"""
Super simple way to render a tiled map
"""
Expand All @@ -55,7 +55,7 @@ def __init__(self, filename) -> None:
print(i)
continue

def render_map(self, surface) -> None:
def render_map(self, surface: pygame.Surface) -> None:
"""Render our map to a pygame surface
Feel free to use this as a starting point for your pygame app.
Expand Down Expand Up @@ -84,7 +84,7 @@ def render_map(self, surface) -> None:
elif isinstance(layer, TiledImageLayer):
self.render_image_layer(surface, layer)

def render_tile_layer(self, surface, layer) -> None:
def render_tile_layer(self, surface: pygame.Surface, layer: TiledTileLayer) -> None:
"""Render all TiledTiles in this layer"""
# deref these heavily used references for speed
tw = self.tmx_data.tilewidth
Expand All @@ -95,7 +95,9 @@ def render_tile_layer(self, surface, layer) -> None:
for x, y, image in layer.tiles():
surface_blit(image, (x * tw, y * th))

def render_object_layer(self, surface, layer) -> None:
def render_object_layer(
self, surface: pygame.Surface, layer: TiledObjectGroup
) -> None:
"""Render all TiledObjects contained in this layer"""
# deref these heavily used references for speed
draw_rect = pygame.draw.rect
Expand Down Expand Up @@ -125,12 +127,14 @@ def render_object_layer(self, surface, layer) -> None:
else:
draw_rect(surface, rect_color, (obj.x, obj.y, obj.width, obj.height), 3)

def render_image_layer(self, surface, layer) -> None:
def render_image_layer(
self, surface: pygame.Surface, layer: TiledImageLayer
) -> None:
if layer.image:
surface.blit(layer.image, (0, 0))


class SimpleTest(object):
class SimpleTest:
"""Basic app to display a rendered Tiled map"""

def __init__(self, filename) -> None:
Expand All @@ -144,7 +148,7 @@ def load_map(self, filename) -> None:
"""Create a renderer, load data, and print some debug info"""
self.renderer = TiledRenderer(filename)

def draw(self, surface) -> None:
def draw(self, surface: pygame.Surface) -> None:
"""Draw our map to some surface (probably the display)"""
# first we make a temporary surface that will accommodate the entire
# size of the map.
Expand All @@ -161,7 +165,7 @@ def draw(self, surface) -> None:

# display a bit of use info on the display
f = pygame.font.Font(pygame.font.get_default_font(), 20)
i = f.render("press any key for next map or ESC to quit", 1, (180, 180, 0))
i = f.render("press any key for next map or ESC to quit", True, (180, 180, 0))
surface.blit(i, (0, 0))

def handle_input(self) -> None:
Expand All @@ -187,11 +191,11 @@ def handle_input(self) -> None:
self.exit_status = 0
self.running = False

def run(self):
def run(self) -> bool:
"""This is our app main loop"""
self.dirty = True
self.running = True
self.exit_status = 1
self.exit_status = True

while self.running:
self.handle_input()
Expand Down
26 changes: 15 additions & 11 deletions apps/pygame_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
logger = logging.getLogger(__name__)


def init_screen(width, height):
def init_screen(width: int, height: int) -> pygame.Surface:
"""Set the screen mode
This function is used to handle window resize events
"""
return pygame.display.set_mode((width, height), pygame.RESIZABLE)


class TiledRenderer(object):
class TiledRenderer:
"""
Super simple way to render a tiled map
"""
Expand All @@ -47,7 +47,7 @@ def __init__(self, filename) -> None:
self.pixel_size = tm.width * tm.tilewidth, tm.height * tm.tileheight
self.tmx_data = tm

def render_map(self, surface) -> None:
def render_map(self, surface: pygame.Surface) -> None:
"""Render our map to a pygame surface
Feel free to use this as a starting point for your pygame app.
Expand Down Expand Up @@ -76,7 +76,7 @@ def render_map(self, surface) -> None:
elif isinstance(layer, TiledImageLayer):
self.render_image_layer(surface, layer)

def render_tile_layer(self, surface, layer) -> None:
def render_tile_layer(self, surface: pygame.Surface, layer: TiledTileLayer) -> None:
"""Render all TiledTiles in this layer"""
# deref these heavily used references for speed
tw = self.tmx_data.tilewidth
Expand All @@ -96,7 +96,9 @@ def render_tile_layer(self, surface, layer) -> None:
sy = x * th2 + y * th2
surface_blit(image, (sx + ox, sy))

def render_object_layer(self, surface, layer) -> None:
def render_object_layer(
self, surface: pygame.Surface, layer: TiledObjectGroup
) -> None:
"""Render all TiledObjects contained in this layer"""
# deref these heavily used references for speed
draw_lines = pygame.draw.lines
Expand All @@ -122,12 +124,14 @@ def render_object_layer(self, surface, layer) -> None:
surface, rect_color, obj.closed, obj.apply_transformations(), 3
)

def render_image_layer(self, surface, layer) -> None:
def render_image_layer(
self, surface: pygame.Surface, layer: TiledImageLayer
) -> None:
if layer.image:
surface.blit(layer.image, (0, 0))


class SimpleTest(object):
class SimpleTest:
"""Basic app to display a rendered Tiled map"""

def __init__(self, filename) -> None:
Expand Down Expand Up @@ -155,7 +159,7 @@ def load_map(self, filename) -> None:
for k, v in self.renderer.tmx_data.get_tile_colliders():
logger.info("%s\t%s", k, list(v))

def draw(self, surface) -> None:
def draw(self, surface: pygame.Surface) -> None:
"""Draw our map to some surface (probably the display)"""
# first we make a temporary surface that will accommodate the entire
# size of the map.
Expand All @@ -172,7 +176,7 @@ def draw(self, surface) -> None:

# display a bit of use info on the display
f = pygame.font.Font(pygame.font.get_default_font(), 20)
i = f.render("press any key for next map or ESC to quit", 1, (180, 180, 0))
i = f.render("press any key for next map or ESC to quit", True, (180, 180, 0))
surface.blit(i, (0, 0))

def handle_input(self) -> None:
Expand All @@ -198,11 +202,11 @@ def handle_input(self) -> None:
self.exit_status = 0
self.running = False

def run(self):
def run(self) -> bool:
"""This is our app main loop"""
self.dirty = True
self.running = True
self.exit_status = 1
self.exit_status = True

while self.running:
self.handle_input()
Expand Down
6 changes: 3 additions & 3 deletions apps/pygame_sdl2_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class GameContext:
renderer: Renderer


class TiledRenderer(object):
class TiledRenderer:
"""
Super simple way to render a tiled map
Expand Down Expand Up @@ -136,10 +136,10 @@ def handle_input(self) -> None:
self.exit_status = 0
self.running = False

def run(self):
def run(self) -> bool:
"""This is our app main loop"""
self.running = True
self.exit_status = 1
self.exit_status = True

while self.running:
self.handle_input()
Expand Down
4 changes: 2 additions & 2 deletions apps/pysdl2_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from pytmx.util_pysdl2 import load_pysdl2


class TiledRenderer(object):
class TiledRenderer:
"""
Super simple way to render a tiled map with pyglet
Expand Down Expand Up @@ -81,7 +81,7 @@ def render_map(self) -> None:
self.render_tile_layer(layer)


class SimpleTest(object):
class SimpleTest:
def __init__(self, filename, window) -> None:
self.running = False
self.dirty = False
Expand Down
Loading

0 comments on commit 6911994

Please sign in to comment.