Skip to content

Commit

Permalink
typehints
Browse files Browse the repository at this point in the history
  • Loading branch information
JaskRendix committed Dec 1, 2024
1 parent 988c436 commit 6108677
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 109 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ map_layer = pyscroll.BufferedRenderer(map_data, map_size)
# just an example for clarity. here's a made up game engine:

def game_engine_draw():
surfaces = list()
surfaces = []
for game_object in my_game_engine:

# pyscroll uses normal pygame surfaces.
Expand Down
13 changes: 7 additions & 6 deletions apps/demo/demo-stitched.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Very basic! No animations.
"""

from __future__ import annotations

from pathlib import Path
Expand All @@ -24,7 +25,7 @@
VIDEORESIZE,
K_r,
)
from pytmx.util_pygame import load_pygame
from pytmx.util_pygame import load_pygame # type: ignore

import pyscroll
from pyscroll.data import MapAggregator, TiledMapData
Expand Down Expand Up @@ -52,7 +53,7 @@ def __init__(self) -> None:
self.velocity = [0, 0]
self._position = [0.0, 0.0]
self._old_position = self.position
self.rect = self.image.get_rect()
self.rect: pygame.Rect = self.image.get_rect()
self.feet = pygame.Rect(0, 0, self.rect.width * 0.5, 8)

@property
Expand All @@ -67,12 +68,12 @@ def update(self, dt: float) -> None:
self._old_position = self._position[:]
self._position[0] += self.velocity[0] * dt
self._position[1] += self.velocity[1] * dt
self.rect.topleft = self._position
self.rect.topleft = (int(self._position[0]), int(self._position[1]))
self.feet.midbottom = self.rect.midbottom

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


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

self.map_layer = pyscroll.BufferedRenderer(
self.map_layer = pyscroll.orthographic.BufferedRenderer(
data=world_data,
size=screen.get_size(),
clamp_camera=True,
Expand All @@ -110,7 +111,7 @@ def __init__(self, screen: pygame.Surface) -> None:
# put the hero in the center of the map
self.hero = Hero()
self.hero.layer = 0
self.hero.position = (400, 400)
self.hero.position = [400.0, 400.0]

# add our hero to the group
self.group.add(self.hero)
Expand Down
34 changes: 18 additions & 16 deletions apps/demo/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
See the "Quest" tutorial for a more simple use with
pygame sprites and groups.
"""

import collections
import logging
from typing import Deque

import pygame
from pygame.locals import *
from pytmx.util_pygame import load_pygame
from pytmx.util_pygame import load_pygame # type: ignore

import pyscroll
import pyscroll.data
Expand All @@ -31,7 +33,7 @@


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


Expand All @@ -42,7 +44,7 @@ class ScrollTest:
"""

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

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

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

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

# true when running
self.running = False

def draw(self, surface) -> None:
def draw(self, surface: pygame.Surface) -> None:

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

def draw_text(self, surface) -> None:
def draw_text(self, surface: pygame.Surface) -> None:
y = 0
for text in self.text_overlay:
surface.blit(text, (0, y))
Expand Down Expand Up @@ -116,27 +118,27 @@ def handle_input(self) -> None:
# but is much easier to use.
pressed = pygame.key.get_pressed()
if pressed[K_UP]:
self.camera_acc[1] = -SCROLL_SPEED * self.last_update_time
self.camera_acc[1] = int(-SCROLL_SPEED * self.last_update_time)
elif pressed[K_DOWN]:
self.camera_acc[1] = SCROLL_SPEED * self.last_update_time
self.camera_acc[1] = int(SCROLL_SPEED * self.last_update_time)
else:
self.camera_acc[1] = 0

if pressed[K_LEFT]:
self.camera_acc[0] = -SCROLL_SPEED * self.last_update_time
self.camera_acc[0] = int(-SCROLL_SPEED * self.last_update_time)
elif pressed[K_RIGHT]:
self.camera_acc[0] = SCROLL_SPEED * self.last_update_time
self.camera_acc[0] = int(SCROLL_SPEED * self.last_update_time)
else:
self.camera_acc[0] = 0

def update(self, td) -> None:
def update(self, td: float) -> None:
self.last_update_time = td

friction = pow(0.0001, self.last_update_time)

# update the camera vector
self.camera_vel[0] += self.camera_acc[0] * td
self.camera_vel[1] += self.camera_acc[1] * td
self.camera_vel[0] += int(self.camera_acc[0] * td)
self.camera_vel[1] += int(self.camera_acc[1] * td)

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

# set the center somewhere else
# in a game, you would set center to a playable character
self.map_layer.center(self.center)
self.map_layer.center((self.center[0], self.center[1]))

def run(self) -> None:
clock = pygame.time.Clock()
self.running = True
fps = 60.0
fps_log = collections.deque(maxlen=20)
fps_log: Deque[float] = collections.deque(maxlen=20)

try:
while self.running:
Expand Down
3 changes: 2 additions & 1 deletion apps/demo/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
incomplete
"""

import pygame


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

spr_list = list()
spr_list = []
for spr in self.sprites():
spr_list.append(spr.rect)

Expand Down
14 changes: 8 additions & 6 deletions apps/tutorial/quest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
pip install pytmx
"""

from __future__ import annotations

from pathlib import Path
Expand All @@ -26,7 +27,7 @@
VIDEORESIZE,
K_r,
)
from pytmx.util_pygame import load_pygame
from pytmx.util_pygame import load_pygame # type: ignore

import pyscroll
import pyscroll.data
Expand Down Expand Up @@ -76,7 +77,7 @@ def __init__(self) -> None:
self.velocity = [0, 0]
self._position = [0.0, 0.0]
self._old_position = self.position
self.rect = self.image.get_rect()
self.rect: pygame.Rect = self.image.get_rect()
self.feet = pygame.Rect(0, 0, self.rect.width * 0.5, 8)

@property
Expand All @@ -91,7 +92,7 @@ def update(self, dt: float) -> None:
self._old_position = self._position[:]
self._position[0] += self.velocity[0] * dt
self._position[1] += self.velocity[1] * dt
self.rect.topleft = self._position
self.rect.topleft = (int(self._position[0]), int(self._position[1]))
self.feet.midbottom = self.rect.midbottom

def move_back(self, dt: float) -> None:
Expand All @@ -100,7 +101,7 @@ def move_back(self, dt: float) -> None:
"""
self._position = self._old_position
self.rect.topleft = self._position
self.rect.topleft = (int(self._position[0]), int(self._position[1]))
self.feet.midbottom = self.rect.midbottom


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

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

# put the hero in the center of the map
self.hero = Hero()
self.hero.position = self.map_layer.map_rect.center
_center = self.map_layer.map_rect.center
self.hero.position = [float(i) for i in _center]

# add our hero to the group
self.group.add(self.hero)
Expand Down
Loading

0 comments on commit 6108677

Please sign in to comment.