Skip to content

Commit

Permalink
typehints
Browse files Browse the repository at this point in the history
  • Loading branch information
JaskRendix committed Jan 19, 2024
1 parent 5c4b21d commit 39410ed
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion apps/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ 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 Down
5 changes: 3 additions & 2 deletions apps/pygame_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,17 @@ def render_image_layer(
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 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)
11 changes: 8 additions & 3 deletions apps/pysdl2_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 9 additions & 8 deletions pytmx/pytmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import zlib
from base64 import b64decode
from collections import defaultdict, namedtuple
from collections.abc import Generator, Iterable, Sequence
from collections.abc import Iterable, Sequence
from copy import deepcopy
from itertools import chain, product
from math import cos, radians, sin
Expand Down Expand Up @@ -1386,7 +1386,7 @@ def iter_data(self) -> Iterable[tuple[int, int, int]]:
for x, gid in enumerate(row):
yield x, y, gid

def tiles(self) -> Generator[tuple[int, int, Any], Any, None]:
def tiles(self):
"""Yields X, Y, Image tuples for each tile in the layer.
Yields:
Expand Down Expand Up @@ -1614,8 +1614,8 @@ class TiledImageLayer(TiledElement):
def __init__(self, parent: TiledMap, node: ElementTree.Element) -> None:
TiledElement.__init__(self)
self.parent = parent
self.source = None
self.trans = None
self.source: Optional[str] = None
self.trans: Optional[str] = None
self.gid = 0

# defaults from the specification
Expand All @@ -1637,15 +1637,16 @@ def image(self):
return self.parent.images[self.gid]
return None

def parse_xml(self, node: ElementTree.Element):
def parse_xml(self, node: ElementTree.Element) -> TiledImageLayer:
"""Parse an Image Layer from ElementTree xml node."""
self._set_properties(node)
self.name = node.get("name", None)
self.opacity = node.get("opacity", self.opacity)
self.opacity = int(node.get("opacity", self.opacity))
self.visible = bool(node.get("visible", self.visible))
image_node = node.find("image")
self.source = image_node.get("source", None)
self.trans = image_node.get("trans", None)
if image_node:
self.source = image_node.get("source", None)
self.trans = image_node.get("trans", None)
return self


Expand Down
4 changes: 3 additions & 1 deletion pytmx/util_pyglet.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
logger.error("cannot import pyglet (is it installed?)")
raise

from typing import Any

import pytmx


Expand Down Expand Up @@ -68,7 +70,7 @@ def load_image(rect=None, flags=None):
return load_image


def load_pyglet(filename, *args, **kwargs):
def load_pyglet(filename: str, *args: Any, **kwargs: Any) -> pytmx.TiledMap:
kwargs["image_loader"] = pyglet_image_loader
kwargs["invert_y"] = True
return pytmx.TiledMap(filename, *args, **kwargs)

0 comments on commit 39410ed

Please sign in to comment.