Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix typehints #182

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions apps/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@
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
"""

def __init__(self, filename) -> None:
def __init__(self, filename: str) -> None:
tm = load_pygame(filename)

# self.size will be the pixel size of the 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,8 +191,13 @@ def handle_input(self) -> None:
self.exit_status = 0
self.running = False

def run(self):
"""This is our app main loop"""
def run(self) -> int:
"""
This is our app main loop

Returns:
Int: 0 means no error, 1 is an error
"""
self.dirty = True
self.running = True
self.exit_status = 1
Expand Down
36 changes: 23 additions & 13 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,24 +124,27 @@ 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:
def __init__(self, filename: str) -> None:
self.renderer = None
self.running = False
self.dirty = False
self.exit_status = 0
self.load_map(filename)

def load_map(self, filename) -> None:
def load_map(self, filename: str) -> None:
"""Create a renderer, load data, and print some debug info"""
self.renderer = TiledRenderer(filename)
assert self.renderer

logger.info("Objects in map:")
for obj in self.renderer.tmx_data.objects:
Expand All @@ -155,7 +160,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 +177,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))
JaskRendix marked this conversation as resolved.
Show resolved Hide resolved
surface.blit(i, (0, 0))

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

def run(self):
"""This is our app main loop"""
def run(self) -> int:
"""
This is our app main loop

Returns:
Int: 0 means no error, 1 is an error
"""
self.dirty = True
self.running = True
self.exit_status = 1
Expand Down
11 changes: 8 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,8 +136,13 @@ def handle_input(self) -> None:
self.exit_status = 0
self.running = False

def run(self):
"""This is our app main loop"""
def run(self) -> int:
"""
This is our app main loop

Returns:
Int: 0 means no error, 1 is an error
"""
self.running = True
self.exit_status = 1

Expand Down
15 changes: 8 additions & 7 deletions apps/pyglet_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
logger.setLevel(logging.INFO)

import pyglet

from pyglet.sprite import Sprite

from pytmx import *
Expand All @@ -32,7 +31,7 @@ class TiledRenderer:
no shape drawing yet
"""

def __init__(self, filename) -> None:
def __init__(self, filename: str) -> None:
tm = load_pyglet(filename)
self.size = tm.width * tm.tilewidth, tm.height * tm.tileheight
self.tmx_data = tm
Expand Down Expand Up @@ -100,20 +99,21 @@ def generate_sprites(self) -> None:
sprite = Sprite(layer.image, x, y, batch=self.batch)
self.sprites.append(sprite)

def draw(self):
def draw(self) -> None:
self.batch.draw()


class SimpleTest:
def __init__(self, filename) -> None:
def __init__(self, filename: str) -> None:
self.renderer = None
self.running = False
self.dirty = False
self.exit_status = 0
self.load_map(filename)

def load_map(self, filename) -> None:
def load_map(self, filename: str) -> None:
self.renderer = TiledRenderer(filename)
assert self.renderer

logger.info("Objects in map:")
for obj in self.renderer.tmx_data.objects:
Expand All @@ -126,6 +126,7 @@ def load_map(self, filename) -> None:
logger.info("%s\t%s", k, v)

def draw(self) -> None:
assert self.renderer
self.renderer.draw()


Expand All @@ -142,7 +143,7 @@ def all_filenames():


class TestWindow(pyglet.window.Window):
def __init__(self, width, height, vsync):
def __init__(self, width: int, height: int, vsync: bool):
super().__init__(width=width, height=height, vsync=vsync)
self.fps_display = pyglet.window.FPSDisplay(self, color=(50, 255, 50, 255))
self.filenames = all_filenames()
Expand All @@ -168,5 +169,5 @@ def on_key_press(self, symbol, mod):

if __name__ == "__main__":
window = TestWindow(600, 600, vsync=False)
pyglet.clock.schedule_interval(window.draw, 1/120)
pyglet.clock.schedule_interval(window.draw, 1 / 120)
pyglet.app.run(None)
15 changes: 10 additions & 5 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 All @@ -104,8 +104,13 @@ def draw(self) -> None:
self.map_renderer.render_map()
self.sdl_renderer.present()

def run(self, window):
"""Starts an event loop without actually processing any event."""
def run(self, window) -> int:
"""
Starts an event loop without actually processing any event.

Returns:
Int: 0 means no error, 1 is an error
"""
import ctypes

event = events.SDL_Event()
Expand All @@ -126,7 +131,7 @@ def run(self, window):
return self.exit_status


def all_filenames():
def all_filenames() -> list[str]:
import glob
import os.path

Expand Down
Loading