Skip to content

Commit b331de7

Browse files
committed
demo
1 parent 51798a8 commit b331de7

3 files changed

Lines changed: 227 additions & 0 deletions

File tree

‎apps/demo/procedural.py‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# This little demo is just a sandbox to show off how the ProceduralData adapter
2+
# plugs into BufferedRenderer. No map files needed — you get a checkerboard of
3+
# grass and water, some rocks sprinkled in, and animated tiles you can pan/zoom
4+
# around with. Think of it as a quick visual smoke test rather than a full game.
5+
6+
import pygame
7+
8+
from pyscroll.data import ProceduralData
9+
from pyscroll.orthographic import BufferedRenderer
10+
11+
12+
def main():
13+
pygame.init()
14+
screen = pygame.display.set_mode((800, 600))
15+
clock = pygame.time.Clock()
16+
17+
data = ProceduralData()
18+
renderer = BufferedRenderer(data, (800, 600))
19+
20+
renderer.center(
21+
(
22+
data.map_size[0] * data.tile_size[0] // 2,
23+
data.map_size[1] * data.tile_size[1] // 2,
24+
)
25+
)
26+
27+
offset_x, offset_y = 0, 0
28+
zoom = 1.0
29+
30+
running = True
31+
while running:
32+
for event in pygame.event.get():
33+
if event.type == pygame.QUIT or (
34+
event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE
35+
):
36+
running = False
37+
38+
keys = pygame.key.get_pressed()
39+
if keys[pygame.K_LEFT]:
40+
offset_x -= 10
41+
if keys[pygame.K_RIGHT]:
42+
offset_x += 10
43+
if keys[pygame.K_UP]:
44+
offset_y -= 10
45+
if keys[pygame.K_DOWN]:
46+
offset_y += 10
47+
if keys[pygame.K_PLUS] or keys[pygame.K_EQUALS]: # '+' key
48+
zoom = min(2.0, zoom + 0.05)
49+
if keys[pygame.K_MINUS]:
50+
zoom = max(0.5, zoom - 0.05)
51+
52+
renderer.zoom = zoom
53+
renderer.center(
54+
(
55+
data.map_size[0] * data.tile_size[0] // 2 + offset_x,
56+
data.map_size[1] * data.tile_size[1] // 2 + offset_y,
57+
)
58+
)
59+
60+
data._update_time()
61+
data.process_animation_queue(renderer._tile_view)
62+
63+
screen.fill((0, 0, 0))
64+
renderer.draw(screen, screen.get_rect())
65+
pygame.display.flip()
66+
67+
clock.tick(60)
68+
69+
pygame.quit()
70+
71+
72+
if __name__ == "__main__":
73+
main()

‎pyscroll/data.py‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,3 +613,93 @@ def __repr__(self) -> str:
613613

614614
def __len__(self) -> int:
615615
return len(self.maps)
616+
617+
618+
class ProceduralData(PyscrollDataAdapter):
619+
"""
620+
A concrete PyscrollDataAdapter subclass for generating simple, procedural
621+
maps on the fly. Used primarily for testing and demonstration.
622+
623+
Generates a map with three layers: [0: Ground, 1: Detail, 2: Overlay].
624+
"""
625+
626+
_TILE_SIZE: Vector2DInt = (32, 32)
627+
_MAP_WIDTH: int = 40
628+
_MAP_HEIGHT: int = 30
629+
_LAYER_COUNT: int = 3
630+
631+
_GID_GRASS: int = 1
632+
_GID_WATER: int = 2
633+
_GID_ROCK: int = 3
634+
_GID_ALT_WATER: int = 999
635+
636+
def __init__(self) -> None:
637+
super().__init__()
638+
self._surfaces: dict[int, Surface] = {}
639+
self._create_surfaces()
640+
self._animated_tile: dict[tuple[int, int, int], Any] = {}
641+
self._animation_map: dict[int, Any] = {}
642+
self._animation_queue: list[Any] = []
643+
self._tracked_gids: set[int] = set()
644+
645+
@property
646+
def tile_size(self) -> Vector2DInt:
647+
return self._TILE_SIZE
648+
649+
@property
650+
def map_size(self) -> Vector2DInt:
651+
return (self._MAP_WIDTH, self._MAP_HEIGHT)
652+
653+
@property
654+
def visible_tile_layers(self) -> list[int]:
655+
return list(range(self._LAYER_COUNT))
656+
657+
def _create_surfaces(self) -> None:
658+
"""Create and store tile surfaces."""
659+
w, h = self.tile_size
660+
661+
def make_surface(color: str) -> Surface:
662+
surf = Surface((w, h))
663+
surf.fill(pygame.Color(color))
664+
return surf
665+
666+
self._surfaces[self._GID_GRASS] = make_surface("#799a46")
667+
self._surfaces[self._GID_WATER] = make_surface("#4a82a6")
668+
self._surfaces[self._GID_ROCK] = make_surface("#555555")
669+
self._surfaces[self._GID_ALT_WATER] = make_surface("#62a2cc")
670+
671+
def reload_data(self) -> None:
672+
"""No external data to reload."""
673+
pass
674+
675+
def _get_tile_image_by_id(self, id: int) -> Optional[Surface]:
676+
return self._surfaces.get(id)
677+
678+
def _get_tile_gid(self, x: int, y: int, l: int) -> Optional[int]:
679+
if not self.is_on_map(x, y):
680+
return None
681+
682+
if l == 0:
683+
return self._GID_GRASS if (x + y) % 2 == 0 else self._GID_WATER
684+
elif l == 1:
685+
if x % 5 == 0 and y % 5 == 0:
686+
return self._GID_ROCK
687+
return None
688+
689+
def _get_tile_image(self, x: int, y: int, l: int) -> Optional[Surface]:
690+
gid = self._get_tile_gid(x, y, l)
691+
return self._surfaces.get(gid) if gid is not None else None
692+
693+
def get_animations(self) -> list[Any]:
694+
"""
695+
Demonstrates a simple animation: water alternates between two colors.
696+
"""
697+
return [
698+
(
699+
self._GID_WATER,
700+
[
701+
(self._GID_WATER, 500),
702+
(self._GID_ALT_WATER, 500),
703+
],
704+
)
705+
]

‎tests/test_procedural_data.py‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import pygame
2+
import pytest
3+
from pygame.surface import Surface
4+
5+
from pyscroll.data import ProceduralData
6+
7+
8+
@pytest.fixture
9+
def proc():
10+
pygame.init()
11+
return ProceduralData()
12+
13+
14+
def test_tile_size_and_map_size(proc):
15+
assert proc.tile_size == (32, 32)
16+
assert proc.map_size == (40, 30)
17+
assert proc.visible_tile_layers == [0, 1, 2]
18+
19+
20+
def test_pixel_to_tile(proc):
21+
assert proc.pixel_to_tile(64, 96) == (2, 3)
22+
assert proc.pixel_to_tile(0, 0) == (0, 0)
23+
assert proc.pixel_to_tile(31.9, 31.9) == (0, 0)
24+
25+
26+
def test_is_on_map(proc):
27+
assert proc.is_on_map(0, 0)
28+
assert proc.is_on_map(39, 29)
29+
assert not proc.is_on_map(-1, 0)
30+
assert not proc.is_on_map(40, 0)
31+
assert not proc.is_on_map(0, 30)
32+
33+
34+
def test_get_tile_gid_ground(proc):
35+
assert proc._get_tile_gid(0, 0, 0) == proc._GID_GRASS
36+
assert proc._get_tile_gid(1, 0, 0) == proc._GID_WATER
37+
38+
39+
def test_get_tile_gid_detail(proc):
40+
assert proc._get_tile_gid(5, 5, 1) == proc._GID_ROCK
41+
assert proc._get_tile_gid(6, 5, 1) is None
42+
43+
44+
def test_get_tile_gid_overlay(proc):
45+
assert proc._get_tile_gid(0, 0, 2) is None
46+
47+
48+
def test_get_tile_image(proc):
49+
img = proc._get_tile_image(0, 0, 0)
50+
assert isinstance(img, Surface)
51+
assert img.get_size() == proc.tile_size
52+
53+
54+
def test_get_tile_image_by_id(proc):
55+
img = proc._get_tile_image_by_id(proc._GID_GRASS)
56+
assert isinstance(img, Surface)
57+
58+
59+
def test_get_animations(proc):
60+
animations = proc.get_animations()
61+
assert isinstance(animations, list)
62+
gid, frames = animations[0]
63+
assert gid == proc._GID_WATER
64+
assert all(isinstance(f[0], int) and isinstance(f[1], int) for f in frames)

0 commit comments

Comments
 (0)