Skip to content

Commit b1ee25c

Browse files
committed
typehints
1 parent 988c436 commit b1ee25c

File tree

6 files changed

+45
-40
lines changed

6 files changed

+45
-40
lines changed

apps/demo/demo-stitched.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
VIDEORESIZE,
2525
K_r,
2626
)
27-
from pytmx.util_pygame import load_pygame
27+
from pytmx.util_pygame import load_pygame # type: ignore
2828

2929
import pyscroll
3030
from pyscroll.data import MapAggregator, TiledMapData
@@ -52,7 +52,7 @@ def __init__(self) -> None:
5252
self.velocity = [0, 0]
5353
self._position = [0.0, 0.0]
5454
self._old_position = self.position
55-
self.rect = self.image.get_rect()
55+
self.rect: pygame.Rect = self.image.get_rect()
5656
self.feet = pygame.Rect(0, 0, self.rect.width * 0.5, 8)
5757

5858
@property
@@ -67,12 +67,12 @@ def update(self, dt: float) -> None:
6767
self._old_position = self._position[:]
6868
self._position[0] += self.velocity[0] * dt
6969
self._position[1] += self.velocity[1] * dt
70-
self.rect.topleft = self._position
70+
self.rect.topleft = (int(self._position[0]), int(self._position[1]))
7171
self.feet.midbottom = self.rect.midbottom
7272

7373
def move_back(self, dt: float) -> None:
7474
self._position = self._old_position
75-
self.rect.topleft = self._position
75+
self.rect.topleft = (int(self._position[0]), int(self._position[1]))
7676
self.feet.midbottom = self.rect.midbottom
7777

7878

@@ -99,7 +99,7 @@ def __init__(self, screen: pygame.Surface) -> None:
9999
pyscroll_data = TiledMapData(tmx_data)
100100
world_data.add_map(pyscroll_data, offset)
101101

102-
self.map_layer = pyscroll.BufferedRenderer(
102+
self.map_layer = pyscroll.orthographic.BufferedRenderer(
103103
data=world_data,
104104
size=screen.get_size(),
105105
clamp_camera=True,
@@ -110,7 +110,7 @@ def __init__(self, screen: pygame.Surface) -> None:
110110
# put the hero in the center of the map
111111
self.hero = Hero()
112112
self.hero.layer = 0
113-
self.hero.position = (400, 400)
113+
self.hero.position = [400.0, 400.0]
114114

115115
# add our hero to the group
116116
self.group.add(self.hero)

apps/demo/demo.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
"""
1313
import collections
1414
import logging
15+
from typing import Deque
1516

1617
import pygame
1718
from pygame.locals import *
18-
from pytmx.util_pygame import load_pygame
19+
from pytmx.util_pygame import load_pygame # type: ignore
1920

2021
import pyscroll
2122
import pyscroll.data
@@ -31,7 +32,7 @@
3132

3233

3334
# simple wrapper to keep the screen resizeable
34-
def init_screen(width, height):
35+
def init_screen(width: int, height: int) -> pygame.Surface:
3536
return pygame.display.set_mode((width, height), pygame.RESIZABLE)
3637

3738

@@ -42,7 +43,7 @@ class ScrollTest:
4243
4344
"""
4445

45-
def __init__(self, filename) -> None:
46+
def __init__(self, filename: str) -> None:
4647

4748
# load data from pytmx
4849
tmx_data = load_pygame(filename)
@@ -60,7 +61,7 @@ def __init__(self, filename) -> None:
6061
t = ["scroll demo. press escape to quit", "arrow keys move"]
6162

6263
# save the rendered text
63-
self.text_overlay = [f.render(i, 1, (180, 180, 0)) for i in t]
64+
self.text_overlay = [f.render(i, True, (180, 180, 0)) for i in t]
6465

6566
# set our initial viewpoint in the center of the map
6667
self.center = [
@@ -71,12 +72,12 @@ def __init__(self, filename) -> None:
7172
# the camera vector is used to handle camera movement
7273
self.camera_acc = [0, 0, 0]
7374
self.camera_vel = [0, 0, 0]
74-
self.last_update_time = 0
75+
self.last_update_time = 0.0
7576

7677
# true when running
7778
self.running = False
7879

79-
def draw(self, surface) -> None:
80+
def draw(self, surface: pygame.Surface) -> None:
8081

8182
# tell the map_layer (BufferedRenderer) to draw to the surface
8283
# the draw function requires a rect to draw to.
@@ -85,7 +86,7 @@ def draw(self, surface) -> None:
8586
# blit our text over the map
8687
self.draw_text(surface)
8788

88-
def draw_text(self, surface) -> None:
89+
def draw_text(self, surface: pygame.Surface) -> None:
8990
y = 0
9091
for text in self.text_overlay:
9192
surface.blit(text, (0, y))
@@ -116,27 +117,27 @@ def handle_input(self) -> None:
116117
# but is much easier to use.
117118
pressed = pygame.key.get_pressed()
118119
if pressed[K_UP]:
119-
self.camera_acc[1] = -SCROLL_SPEED * self.last_update_time
120+
self.camera_acc[1] = int(-SCROLL_SPEED * self.last_update_time)
120121
elif pressed[K_DOWN]:
121-
self.camera_acc[1] = SCROLL_SPEED * self.last_update_time
122+
self.camera_acc[1] = int(SCROLL_SPEED * self.last_update_time)
122123
else:
123124
self.camera_acc[1] = 0
124125

125126
if pressed[K_LEFT]:
126-
self.camera_acc[0] = -SCROLL_SPEED * self.last_update_time
127+
self.camera_acc[0] = int(-SCROLL_SPEED * self.last_update_time)
127128
elif pressed[K_RIGHT]:
128-
self.camera_acc[0] = SCROLL_SPEED * self.last_update_time
129+
self.camera_acc[0] = int(SCROLL_SPEED * self.last_update_time)
129130
else:
130131
self.camera_acc[0] = 0
131132

132-
def update(self, td) -> None:
133+
def update(self, td: float) -> None:
133134
self.last_update_time = td
134135

135136
friction = pow(0.0001, self.last_update_time)
136137

137138
# update the camera vector
138-
self.camera_vel[0] += self.camera_acc[0] * td
139-
self.camera_vel[1] += self.camera_acc[1] * td
139+
self.camera_vel[0] += int(self.camera_acc[0] * td)
140+
self.camera_vel[1] += int(self.camera_acc[1] * td)
140141

141142
self.camera_vel[0] *= friction
142143
self.camera_vel[1] *= friction
@@ -165,13 +166,13 @@ def update(self, td) -> None:
165166

166167
# set the center somewhere else
167168
# in a game, you would set center to a playable character
168-
self.map_layer.center(self.center)
169+
self.map_layer.center((self.center[0], self.center[1]))
169170

170171
def run(self) -> None:
171172
clock = pygame.time.Clock()
172173
self.running = True
173174
fps = 60.0
174-
fps_log = collections.deque(maxlen=20)
175+
fps_log: Deque[float] = collections.deque(maxlen=20)
175176

176177
try:
177178
while self.running:

apps/tutorial/quest.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
VIDEORESIZE,
2727
K_r,
2828
)
29-
from pytmx.util_pygame import load_pygame
29+
from pytmx.util_pygame import load_pygame # type: ignore
3030

3131
import pyscroll
3232
import pyscroll.data
@@ -76,7 +76,7 @@ def __init__(self) -> None:
7676
self.velocity = [0, 0]
7777
self._position = [0.0, 0.0]
7878
self._old_position = self.position
79-
self.rect = self.image.get_rect()
79+
self.rect: pygame.Rect = self.image.get_rect()
8080
self.feet = pygame.Rect(0, 0, self.rect.width * 0.5, 8)
8181

8282
@property
@@ -91,7 +91,7 @@ def update(self, dt: float) -> None:
9191
self._old_position = self._position[:]
9292
self._position[0] += self.velocity[0] * dt
9393
self._position[1] += self.velocity[1] * dt
94-
self.rect.topleft = self._position
94+
self.rect.topleft = (int(self._position[0]), int(self._position[1]))
9595
self.feet.midbottom = self.rect.midbottom
9696

9797
def move_back(self, dt: float) -> None:
@@ -100,7 +100,7 @@ def move_back(self, dt: float) -> None:
100100
101101
"""
102102
self._position = self._old_position
103-
self.rect.topleft = self._position
103+
self.rect.topleft = (int(self._position[0]), int(self._position[1]))
104104
self.feet.midbottom = self.rect.midbottom
105105

106106

@@ -131,7 +131,7 @@ def __init__(self, screen: pygame.Surface) -> None:
131131
self.walls.append(pygame.Rect(obj.x, obj.y, obj.width, obj.height))
132132

133133
# create new renderer (camera)
134-
self.map_layer = pyscroll.BufferedRenderer(
134+
self.map_layer = pyscroll.orthographic.BufferedRenderer(
135135
data=pyscroll.data.TiledMapData(tmx_data),
136136
size=screen.get_size(),
137137
clamp_camera=False,
@@ -147,7 +147,8 @@ def __init__(self, screen: pygame.Surface) -> None:
147147

148148
# put the hero in the center of the map
149149
self.hero = Hero()
150-
self.hero.position = self.map_layer.map_rect.center
150+
_center = self.map_layer.map_rect.center
151+
self.hero.position = [float(i) for i in _center]
151152

152153
# add our hero to the group
153154
self.group.add(self.hero)

pyscroll/data.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import annotations
88

99
import time
10+
from collections.abc import Iterable
1011
from heapq import heappop, heappush
1112
from itertools import product
1213

@@ -43,8 +44,10 @@ class PyscrollDataAdapter:
4344
# or properties. they are listed here as class
4445
# instances, but use as properties is fine, too.
4546

46-
tile_size = None # (int, int): size of each tile in pixels
47-
map_size = None # (int, int): size of map in tiles
47+
# (int, int): size of each tile in pixels
48+
tile_size: tuple[int, int] = (0, 0)
49+
# (int, int): size of map in tiles
50+
map_size: tuple[int, int] = (0, 0)
4851
visible_tile_layers = None # list of visible layer integers
4952

5053
def __init__(self) -> None:
@@ -289,7 +292,7 @@ class TiledMapData(PyscrollDataAdapter):
289292
290293
"""
291294

292-
def __init__(self, tmx) -> None:
295+
def __init__(self, tmx: pytmx.TiledMap) -> None:
293296
super(TiledMapData, self).__init__()
294297
self.tmx = tmx
295298
self.reload_animations()
@@ -320,19 +323,19 @@ def convert_surfaces(self, parent: Surface, alpha: bool = False) -> None:
320323
self.tmx.images = images
321324

322325
@property
323-
def tile_size(self):
326+
def tile_size(self) -> tuple[int, int]:
324327
return self.tmx.tilewidth, self.tmx.tileheight
325328

326329
@property
327-
def map_size(self):
330+
def map_size(self) -> tuple[int, int]:
328331
return self.tmx.width, self.tmx.height
329332

330333
@property
331334
def visible_tile_layers(self):
332335
return self.tmx.visible_tile_layers
333336

334337
@property
335-
def visible_object_layers(self):
338+
def visible_object_layers(self) -> Iterable[pytmx.TiledObjectGroup]:
336339
return (
337340
layer
338341
for layer in self.tmx.visible_layers
@@ -349,10 +352,10 @@ def _get_tile_image_by_id(self, id) -> Surface:
349352
return self.tmx.images[id]
350353

351354
def get_tile_images_by_rect(self, rect: RectLike):
352-
def rev(seq, start, stop):
355+
def rev(seq:list[int], start:int, stop: int) -> enumerate[int]:
353356
if start < 0:
354357
start = 0
355-
return enumerate(seq[start : stop + 1], start)
358+
return enumerate(seq[start:stop + 1], start)
356359

357360
x1, y1, x2, y2 = rect_to_bb(rect)
358361
images = self.tmx.images

pyscroll/group.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING
3+
from typing import TYPE_CHECKING, Any
44

55
import pygame
66

@@ -17,7 +17,7 @@ class PyscrollGroup(pygame.sprite.LayeredUpdates):
1717
1818
"""
1919

20-
def __init__(self, map_layer: BufferedRenderer, *args, **kwargs) -> None:
20+
def __init__(self, map_layer: BufferedRenderer, *args: Any, **kwargs: Any) -> None:
2121
pygame.sprite.LayeredUpdates.__init__(self, *args, **kwargs)
2222
self._map_layer = map_layer
2323

pyscroll/orthographic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def __init__(
4040
clamp_camera: bool = True,
4141
colorkey=None,
4242
alpha: bool = False,
43-
time_source: Callable = time.time,
43+
time_source: Callable[[], float] = time.time,
4444
scaling_function: Callable = pygame.transform.scale,
4545
tall_sprites: int = 0,
4646
sprite_damage_height: int = 0,
@@ -73,7 +73,7 @@ def __init__(
7373
self.scaling_function = scaling_function
7474
self.tall_sprites = tall_sprites
7575
self.sprite_damage_height = sprite_damage_height
76-
self.map_rect = None
76+
self.map_rect = Rect(0, 0, 0, 0)
7777

7878
# internal private defaults
7979
if colorkey and alpha:

0 commit comments

Comments
 (0)