Skip to content

Commit 7e40739

Browse files
committed
typehints
1 parent 988c436 commit 7e40739

File tree

10 files changed

+122
-98
lines changed

10 files changed

+122
-98
lines changed

.github/workflows/python-app.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ jobs:
3434
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
3535
- name: Test
3636
run: |
37-
python -m unittest tests/pyscroll/test_pyscroll.py
37+
python -m unittest discover -s tests/pyscroll -p "test_*.py"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ map_layer = pyscroll.BufferedRenderer(map_data, map_size)
155155
# just an example for clarity. here's a made up game engine:
156156

157157
def game_engine_draw():
158-
surfaces = list()
158+
surfaces = []
159159
for game_object in my_game_engine:
160160

161161
# pyscroll uses normal pygame surfaces.

apps/demo/demo-stitched.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Very basic! No animations.
77
88
"""
9+
910
from __future__ import annotations
1011

1112
from pathlib import Path
@@ -24,7 +25,7 @@
2425
VIDEORESIZE,
2526
K_r,
2627
)
27-
from pytmx.util_pygame import load_pygame
28+
from pytmx.util_pygame import load_pygame # type: ignore
2829

2930
import pyscroll
3031
from pyscroll.data import MapAggregator, TiledMapData
@@ -52,7 +53,7 @@ def __init__(self) -> None:
5253
self.velocity = [0, 0]
5354
self._position = [0.0, 0.0]
5455
self._old_position = self.position
55-
self.rect = self.image.get_rect()
56+
self.rect: pygame.Rect = self.image.get_rect()
5657
self.feet = pygame.Rect(0, 0, self.rect.width * 0.5, 8)
5758

5859
@property
@@ -67,12 +68,12 @@ def update(self, dt: float) -> None:
6768
self._old_position = self._position[:]
6869
self._position[0] += self.velocity[0] * dt
6970
self._position[1] += self.velocity[1] * dt
70-
self.rect.topleft = self._position
71+
self.rect.topleft = (int(self._position[0]), int(self._position[1]))
7172
self.feet.midbottom = self.rect.midbottom
7273

7374
def move_back(self, dt: float) -> None:
7475
self._position = self._old_position
75-
self.rect.topleft = self._position
76+
self.rect.topleft = (int(self._position[0]), int(self._position[1]))
7677
self.feet.midbottom = self.rect.midbottom
7778

7879

@@ -99,7 +100,7 @@ def __init__(self, screen: pygame.Surface) -> None:
99100
pyscroll_data = TiledMapData(tmx_data)
100101
world_data.add_map(pyscroll_data, offset)
101102

102-
self.map_layer = pyscroll.BufferedRenderer(
103+
self.map_layer = pyscroll.orthographic.BufferedRenderer(
103104
data=world_data,
104105
size=screen.get_size(),
105106
clamp_camera=True,
@@ -110,7 +111,7 @@ def __init__(self, screen: pygame.Surface) -> None:
110111
# put the hero in the center of the map
111112
self.hero = Hero()
112113
self.hero.layer = 0
113-
self.hero.position = (400, 400)
114+
self.hero.position = [400.0, 400.0]
114115

115116
# add our hero to the group
116117
self.group.add(self.hero)

apps/demo/demo.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
See the "Quest" tutorial for a more simple use with
1111
pygame sprites and groups.
1212
"""
13+
1314
import collections
1415
import logging
16+
from typing import Deque
1517

1618
import pygame
1719
from pygame.locals import *
18-
from pytmx.util_pygame import load_pygame
20+
from pytmx.util_pygame import load_pygame # type: ignore
1921

2022
import pyscroll
2123
import pyscroll.data
@@ -31,7 +33,7 @@
3133

3234

3335
# simple wrapper to keep the screen resizeable
34-
def init_screen(width, height):
36+
def init_screen(width: int, height: int) -> pygame.Surface:
3537
return pygame.display.set_mode((width, height), pygame.RESIZABLE)
3638

3739

@@ -42,7 +44,7 @@ class ScrollTest:
4244
4345
"""
4446

45-
def __init__(self, filename) -> None:
47+
def __init__(self, filename: str) -> None:
4648

4749
# load data from pytmx
4850
tmx_data = load_pygame(filename)
@@ -60,7 +62,7 @@ def __init__(self, filename) -> None:
6062
t = ["scroll demo. press escape to quit", "arrow keys move"]
6163

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

6567
# set our initial viewpoint in the center of the map
6668
self.center = [
@@ -71,12 +73,12 @@ def __init__(self, filename) -> None:
7173
# the camera vector is used to handle camera movement
7274
self.camera_acc = [0, 0, 0]
7375
self.camera_vel = [0, 0, 0]
74-
self.last_update_time = 0
76+
self.last_update_time = 0.0
7577

7678
# true when running
7779
self.running = False
7880

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

8183
# tell the map_layer (BufferedRenderer) to draw to the surface
8284
# the draw function requires a rect to draw to.
@@ -85,7 +87,7 @@ def draw(self, surface) -> None:
8587
# blit our text over the map
8688
self.draw_text(surface)
8789

88-
def draw_text(self, surface) -> None:
90+
def draw_text(self, surface: pygame.Surface) -> None:
8991
y = 0
9092
for text in self.text_overlay:
9193
surface.blit(text, (0, y))
@@ -116,27 +118,27 @@ def handle_input(self) -> None:
116118
# but is much easier to use.
117119
pressed = pygame.key.get_pressed()
118120
if pressed[K_UP]:
119-
self.camera_acc[1] = -SCROLL_SPEED * self.last_update_time
121+
self.camera_acc[1] = int(-SCROLL_SPEED * self.last_update_time)
120122
elif pressed[K_DOWN]:
121-
self.camera_acc[1] = SCROLL_SPEED * self.last_update_time
123+
self.camera_acc[1] = int(SCROLL_SPEED * self.last_update_time)
122124
else:
123125
self.camera_acc[1] = 0
124126

125127
if pressed[K_LEFT]:
126-
self.camera_acc[0] = -SCROLL_SPEED * self.last_update_time
128+
self.camera_acc[0] = int(-SCROLL_SPEED * self.last_update_time)
127129
elif pressed[K_RIGHT]:
128-
self.camera_acc[0] = SCROLL_SPEED * self.last_update_time
130+
self.camera_acc[0] = int(SCROLL_SPEED * self.last_update_time)
129131
else:
130132
self.camera_acc[0] = 0
131133

132-
def update(self, td) -> None:
134+
def update(self, td: float) -> None:
133135
self.last_update_time = td
134136

135137
friction = pow(0.0001, self.last_update_time)
136138

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

141143
self.camera_vel[0] *= friction
142144
self.camera_vel[1] *= friction
@@ -165,13 +167,13 @@ def update(self, td) -> None:
165167

166168
# set the center somewhere else
167169
# in a game, you would set center to a playable character
168-
self.map_layer.center(self.center)
170+
self.map_layer.center((self.center[0], self.center[1]))
169171

170172
def run(self) -> None:
171173
clock = pygame.time.Clock()
172174
self.running = True
173175
fps = 60.0
174-
fps_log = collections.deque(maxlen=20)
176+
fps_log: Deque[float] = collections.deque(maxlen=20)
175177

176178
try:
177179
while self.running:

apps/demo/translate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
incomplete
55
"""
6+
67
import pygame
78

89

@@ -18,7 +19,7 @@ def run(self) -> None:
1819
r = self._map_layer.translate_point(spr.rect.topleft)
1920
pygame.draw.circle(surface, (20, 20, 20), r, 3)
2021

21-
spr_list = list()
22+
spr_list = []
2223
for spr in self.sprites():
2324
spr_list.append(spr.rect)
2425

apps/tutorial/quest.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
99
pip install pytmx
1010
"""
11+
1112
from __future__ import annotations
1213

1314
from pathlib import Path
@@ -26,7 +27,7 @@
2627
VIDEORESIZE,
2728
K_r,
2829
)
29-
from pytmx.util_pygame import load_pygame
30+
from pytmx.util_pygame import load_pygame # type: ignore
3031

3132
import pyscroll
3233
import pyscroll.data
@@ -76,7 +77,7 @@ def __init__(self) -> None:
7677
self.velocity = [0, 0]
7778
self._position = [0.0, 0.0]
7879
self._old_position = self.position
79-
self.rect = self.image.get_rect()
80+
self.rect: pygame.Rect = self.image.get_rect()
8081
self.feet = pygame.Rect(0, 0, self.rect.width * 0.5, 8)
8182

8283
@property
@@ -91,7 +92,7 @@ def update(self, dt: float) -> None:
9192
self._old_position = self._position[:]
9293
self._position[0] += self.velocity[0] * dt
9394
self._position[1] += self.velocity[1] * dt
94-
self.rect.topleft = self._position
95+
self.rect.topleft = (int(self._position[0]), int(self._position[1]))
9596
self.feet.midbottom = self.rect.midbottom
9697

9798
def move_back(self, dt: float) -> None:
@@ -100,7 +101,7 @@ def move_back(self, dt: float) -> None:
100101
101102
"""
102103
self._position = self._old_position
103-
self.rect.topleft = self._position
104+
self.rect.topleft = (int(self._position[0]), int(self._position[1]))
104105
self.feet.midbottom = self.rect.midbottom
105106

106107

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

133134
# create new renderer (camera)
134-
self.map_layer = pyscroll.BufferedRenderer(
135+
self.map_layer = pyscroll.orthographic.BufferedRenderer(
135136
data=pyscroll.data.TiledMapData(tmx_data),
136137
size=screen.get_size(),
137138
clamp_camera=False,
@@ -147,7 +148,8 @@ def __init__(self, screen: pygame.Surface) -> None:
147148

148149
# put the hero in the center of the map
149150
self.hero = Hero()
150-
self.hero.position = self.map_layer.map_rect.center
151+
_center = self.map_layer.map_rect.center
152+
self.hero.position = [float(i) for i in _center]
151153

152154
# add our hero to the group
153155
self.group.add(self.hero)

0 commit comments

Comments
 (0)