pgzero-plus 1.0.0__tar.gz → 1.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. {pgzero_plus-1.0.0/src/pgzero_plus.egg-info → pgzero_plus-1.0.1}/PKG-INFO +1 -1
  2. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/pyproject.toml +1 -1
  3. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/tilemaps.py +277 -38
  4. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1/src/pgzero_plus.egg-info}/PKG-INFO +1 -1
  5. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_tilemaps.py +269 -0
  6. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/COPYING +0 -0
  7. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/README.md +0 -0
  8. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/setup.cfg +0 -0
  9. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/__init__.py +0 -0
  10. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/__main__.py +0 -0
  11. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/_install_check.py +0 -0
  12. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/actor.py +0 -0
  13. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/animation.py +0 -0
  14. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/builtins.py +0 -0
  15. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/clock.py +0 -0
  16. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/constants.py +0 -0
  17. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/data/icon.png +0 -0
  18. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/data/joypad.png +0 -0
  19. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/game.py +0 -0
  20. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/keyboard.py +0 -0
  21. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/loaders.py +0 -0
  22. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/music.py +0 -0
  23. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/ptext.py +0 -0
  24. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/rect.py +0 -0
  25. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/runner.py +0 -0
  26. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/screen.py +0 -0
  27. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/soundfmt.py +0 -0
  28. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/spellcheck.py +0 -0
  29. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/sprites.py +0 -0
  30. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/storage.py +0 -0
  31. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero/tone.py +0 -0
  32. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero_plus.egg-info/SOURCES.txt +0 -0
  33. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero_plus.egg-info/dependency_links.txt +0 -0
  34. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero_plus.egg-info/entry_points.txt +0 -0
  35. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero_plus.egg-info/requires.txt +0 -0
  36. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzero_plus.egg-info/top_level.txt +0 -0
  37. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/src/pgzrun.py +1 -1
  38. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_actor.py +0 -0
  39. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_actor_features.py +0 -0
  40. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_animation.py +0 -0
  41. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_builtins.py +0 -0
  42. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_event_dispatch.py +0 -0
  43. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_game_state.py +0 -0
  44. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_install_check.py +0 -0
  45. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_keyboard.py +0 -0
  46. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_loader_case.py +0 -0
  47. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_music.py +0 -0
  48. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_rect_actor.py +0 -0
  49. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_runner.py +0 -0
  50. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_screen.py +0 -0
  51. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_sound_formats.py +0 -0
  52. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_spellcheck.py +0 -0
  53. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_sprites.py +0 -0
  54. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_storage.py +0 -0
  55. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_tone.py +0 -0
  56. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_transform_anchor.py +0 -0
  57. {pgzero_plus-1.0.0 → pgzero_plus-1.0.1}/test/test_webp_loader.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pgzero-plus
3
- Version: 1.0.0
3
+ Version: 1.0.1
4
4
  Summary: An enhanced fork of Pygame Zero with additional features for teaching and developing 2D games
5
5
  Author-email: Michael D'Argenio <mjdargen@gmail.com>
6
6
  License-Expression: LGPL-3.0-or-later
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "pgzero-plus"
3
- version = "1.0.0"
3
+ version = "1.0.1"
4
4
  description = "An enhanced fork of Pygame Zero with additional features for teaching and developing 2D games"
5
5
  readme = "README.md"
6
6
  license = "LGPL-3.0-or-later"
@@ -8,7 +8,7 @@ import pygame
8
8
  import xml.etree.ElementTree as ET
9
9
 
10
10
  from . import loaders
11
- from .actor import Actor
11
+ from .actor import Actor, _set_angle, _set_flip, _set_opacity, _set_scale
12
12
 
13
13
  FLIP_H = 0x80000000
14
14
  FLIP_V = 0x40000000
@@ -18,6 +18,68 @@ GID_MASK = 0x0FFFFFFF
18
18
  _NO_PROPERTIES = types.MappingProxyType({})
19
19
 
20
20
 
21
+ # A square tile's image can face 8 ways. Tiled saves which one with three
22
+ # flips, applied the way Actor applies them: flip_d first, then flip_h and
23
+ # flip_v. Turning the map a quarter turn makes each tile face another of the
24
+ # 8 ways, so the map's angle and the tile's own flips combine into one set of
25
+ # flips, and turned tiles are drawn and collide exactly like flipped ones.
26
+
27
+
28
+ def _turn(x, y, angle):
29
+ """Turn the point (x, y) about (0, 0) by angle degrees, counterclockwise
30
+ on the screen like Actor.angle. angle is a multiple of 90, so swapping
31
+ and negating gives the exact answer, with no rounding from sin and cos."""
32
+ for _ in range(int(angle) // 90 % 4):
33
+ x, y = y, -x
34
+ return x, y
35
+
36
+
37
+ def _flip_matrix(flip_h, flip_v, flip_d):
38
+ """Where the flips move a point (x, y) of a tile's image, as (a, b, c, d)
39
+ meaning it goes to (a*x + b*y, c*x + d*y)."""
40
+ a, b, c, d = 1, 0, 0, 1
41
+ if flip_d: # swap x and y
42
+ a, b, c, d = c, d, a, b
43
+ if flip_h:
44
+ a, b = -a, -b
45
+ if flip_v:
46
+ c, d = -c, -d
47
+ return a, b, c, d
48
+
49
+
50
+ _FLIPS_FOR_MATRIX = {
51
+ _flip_matrix(flip_h, flip_v, flip_d): (flip_h, flip_v, flip_d)
52
+ for flip_h in (False, True)
53
+ for flip_v in (False, True)
54
+ for flip_d in (False, True)
55
+ }
56
+
57
+
58
+ def _combine_flips(angle, flip_h, flip_v, flip_d):
59
+ """The flips that show a tile with these flips on a map turned by angle."""
60
+ a, b, c, d = _flip_matrix(flip_h, flip_v, flip_d)
61
+ # Turn where the flips sent the x and y directions.
62
+ a, c = _turn(a, c, angle)
63
+ b, d = _turn(b, d, angle)
64
+ return _FLIPS_FOR_MATRIX[(a, b, c, d)]
65
+
66
+
67
+ def _set_tile_flip(tile, current_surface):
68
+ """Like Actor's flip step, but every tile of the same kind, size and
69
+ flips shares one flipped image, so turning a big map doesn't make a copy
70
+ of the image for every tile."""
71
+ if not (tile._flip_h or tile._flip_v or tile._flip_d):
72
+ return current_surface
73
+ if current_surface is not tile._orig_surf:
74
+ # Changed by an earlier step, like opacity, so it can't be shared.
75
+ return _set_flip(tile, current_surface)
76
+ key = tile._surface_key + (tile._flip_h, tile._flip_v, tile._flip_d)
77
+ surfaces = tile._tileset.setdefault("surfaces", {})
78
+ if key not in surfaces:
79
+ surfaces[key] = _set_flip(tile, current_surface)
80
+ return surfaces[key]
81
+
82
+
21
83
  def _get_tile_surface(tileset, tile_col, tile_row, width, height):
22
84
  """Return the tile's image at the given size.
23
85
 
@@ -37,6 +99,23 @@ def _get_tile_surface(tileset, tile_col, tile_row, width, height):
37
99
  return surfaces[key]
38
100
 
39
101
 
102
+ def _scaled_size(length, scale):
103
+ """The length of a tile in whole pixels after scaling.
104
+
105
+ The map's grid and its tile images both use this, so that tiles meet
106
+ exactly. With a scale like 0.7, an exact grid of 11.2 pixels and images
107
+ of 11 would leave a gap of one pixel every few tiles.
108
+ """
109
+ return max(1, round(length * scale))
110
+
111
+
112
+ def _check_scale(scale):
113
+ if isinstance(scale, bool) or not isinstance(scale, numbers.Real):
114
+ raise TypeError(f"scale should be a number, like 2 or 1.5, not {scale!r}.")
115
+ if scale <= 0:
116
+ raise ValueError(f"scale should be bigger than 0, like 2 or 0.5, not {scale!r}.")
117
+
118
+
40
119
  def _check_col_row(col, row):
41
120
  """Return col and row as ints, or explain what they should be."""
42
121
  for name, value in (("col", col), ("row", row)):
@@ -101,6 +180,9 @@ class Tile(Actor):
101
180
  tile_gid = _read_only("tile_gid", _IMAGE_ADVICE)
102
181
  tileset_firstgid = _read_only("tileset_firstgid", _IMAGE_ADVICE)
103
182
 
183
+ # Flipped tiles share their flipped images (see _set_tile_flip).
184
+ function_order = [_set_opacity, _set_scale, _set_tile_flip, _set_angle]
185
+
104
186
  def __init__(
105
187
  self,
106
188
  tileset,
@@ -116,6 +198,7 @@ class Tile(Actor):
116
198
  flip_v=False,
117
199
  flip_d=False,
118
200
  layer=None,
201
+ map_angle=0,
119
202
  ):
120
203
  self._tileset = tileset
121
204
  self._layer = layer
@@ -123,17 +206,19 @@ class Tile(Actor):
123
206
  self._map_tile_width = map_tile_width
124
207
  self._map_tile_height = map_tile_height
125
208
  self._map_scale = max(0.0, scale)
209
+ # The flips set in Tiled, which flip_h, flip_v and flip_d show. The
210
+ # tile is drawn with these combined with the map's angle.
211
+ self._tiled_flip_h = bool(flip_h)
212
+ self._tiled_flip_v = bool(flip_v)
213
+ self._tiled_flip_d = bool(flip_d)
214
+ self._map_angle = map_angle
126
215
 
127
216
  super().__init__(self._tile_surface(tile_col, tile_row))
128
217
 
129
- # Setting a flip redoes the tile's position and image, so only set the
130
- # flips that are used; most tiles in a map have none.
131
- if flip_h:
132
- self.flip_h = flip_h
133
- if flip_v:
134
- self.flip_v = flip_v
135
- if flip_d:
136
- self.flip_d = flip_d
218
+ # Changing the flips redoes the tile's position and image, so skip it
219
+ # for the many tiles that have none.
220
+ if flip_h or flip_v or flip_d or map_angle:
221
+ self._orient()
137
222
 
138
223
  self._place(map_col, map_row)
139
224
 
@@ -153,6 +238,52 @@ class Tile(Actor):
153
238
  self._map_scale = max(0.0, scale)
154
239
  self._set_image(self._tile_surface(self.tile_col, self.tile_row))
155
240
 
241
+ # flip_h, flip_v and flip_d are the flips set in Tiled (or by the game),
242
+ # even after the map has been turned. Actor's own _flip_h, _flip_v and
243
+ # _flip_d hold those combined with the map's angle, which is how the tile
244
+ # is drawn.
245
+
246
+ @property
247
+ def flip_h(self):
248
+ return self._tiled_flip_h
249
+
250
+ @flip_h.setter
251
+ def flip_h(self, value):
252
+ self._tiled_flip_h = bool(value)
253
+ self._orient()
254
+
255
+ @property
256
+ def flip_v(self):
257
+ return self._tiled_flip_v
258
+
259
+ @flip_v.setter
260
+ def flip_v(self, value):
261
+ self._tiled_flip_v = bool(value)
262
+ self._orient()
263
+
264
+ @property
265
+ def flip_d(self):
266
+ return self._tiled_flip_d
267
+
268
+ @flip_d.setter
269
+ def flip_d(self, value):
270
+ self._tiled_flip_d = bool(value)
271
+ self._orient()
272
+
273
+ def _orient(self):
274
+ """Draw the tile with its flips combined with the map's angle."""
275
+ self._flip_h, self._flip_v, self._flip_d = _combine_flips(
276
+ self._map_angle, self._tiled_flip_h, self._tiled_flip_v, self._tiled_flip_d
277
+ )
278
+ self._update_pos()
279
+ self._update_transform(_set_tile_flip)
280
+
281
+ def _set_map_angle(self, angle):
282
+ """Face the way the tiles of a map turned by angle face."""
283
+ if angle != self._map_angle:
284
+ self._map_angle = angle
285
+ self._orient()
286
+
156
287
  @property
157
288
  def properties(self):
158
289
  """The custom properties given to this kind of tile in Tiled."""
@@ -186,13 +317,14 @@ class Tile(Actor):
186
317
  self._tile_id = tile_id
187
318
  self._tile_gid = self._tileset_firstgid + tile_id
188
319
 
189
- return _get_tile_surface(
190
- self._tileset,
320
+ # Which shared image this is, for _get_tile_surface and _set_tile_flip.
321
+ self._surface_key = (
191
322
  tile_col,
192
323
  tile_row,
193
- max(1, round(self._map_tile_width * self._map_scale)),
194
- max(1, round(self._map_tile_height * self._map_scale)),
324
+ _scaled_size(self._map_tile_width, self._map_scale),
325
+ _scaled_size(self._map_tile_height, self._map_scale),
195
326
  )
327
+ return _get_tile_surface(self._tileset, *self._surface_key)
196
328
 
197
329
  def _place(self, map_col, map_row):
198
330
  """Record the tile's square and put it there on the screen."""
@@ -201,15 +333,11 @@ class Tile(Actor):
201
333
 
202
334
  if self._layer is not None:
203
335
  # Use the map's grid, even if this one tile has been scaled.
204
- tile_map = self._layer._map
205
- origin_x, origin_y = tile_map.x, tile_map.y
206
- width, height = tile_map.tile_width, tile_map.tile_height
336
+ self.topleft = self._layer._map._square_topleft(map_col, map_row)
207
337
  else:
208
- origin_x = origin_y = 0
209
- width = self._map_tile_width * self.scale
210
- height = self._map_tile_height * self.scale
211
-
212
- self.topleft = (origin_x + width * map_col, origin_y + height * map_row)
338
+ width = _scaled_size(self._map_tile_width, self.scale)
339
+ height = _scaled_size(self._map_tile_height, self.scale)
340
+ self.topleft = (width * map_col, height * map_row)
213
341
 
214
342
  def copy(self, map_col=None, map_row=None):
215
343
  """Return a new tile like this one that isn't in any layer."""
@@ -232,6 +360,7 @@ class Tile(Actor):
232
360
  flip_h=self.flip_h,
233
361
  flip_v=self.flip_v,
234
362
  flip_d=self.flip_d,
363
+ map_angle=self._map_angle,
235
364
  )
236
365
 
237
366
 
@@ -375,6 +504,7 @@ class TileLayer(Sequence):
375
504
  old_tile._layer = None
376
505
 
377
506
  tile._layer = self
507
+ tile._set_map_angle(self._map._angle)
378
508
  tile._place(col, row)
379
509
  self._tiles[(col, row)] = tile
380
510
 
@@ -400,7 +530,7 @@ class TileMap(Mapping):
400
530
  The map can be read like a dictionary, but not changed like one.
401
531
  """
402
532
 
403
- def __init__(self, name, map_tile_width, map_tile_height, scale=1, cols=0, rows=0, tilesets=None):
533
+ def __init__(self, name, map_tile_width, map_tile_height, scale=1.0, cols=0, rows=0, tilesets=None):
404
534
  self._name = name
405
535
  self._layers = {} # name: TileLayer, in drawing order
406
536
  self._tilesets = tilesets if tilesets is not None else {}
@@ -409,8 +539,12 @@ class TileMap(Mapping):
409
539
  self._rows = rows
410
540
  self._map_tile_width = map_tile_width
411
541
  self._map_tile_height = map_tile_height
542
+ # Where the map's top left corner, the corner of square (0, 0), is on
543
+ # the screen, and how far the map has been turned. A point in the map
544
+ # is on the screen at (_x, _y) + _turn(its x and y in the map, _angle).
412
545
  self._x = 0
413
546
  self._y = 0
547
+ self._angle = 0
414
548
 
415
549
  def __repr__(self):
416
550
  return "<TileMap {!r} with layers {}>".format(self._name, list(self._layers))
@@ -461,17 +595,18 @@ class TileMap(Mapping):
461
595
 
462
596
  @property
463
597
  def tile_width(self):
464
- """The width of one square of the map on the screen, in pixels."""
465
- return self._map_tile_width * self._scale
598
+ """The width of one square of the map on the screen, in whole pixels."""
599
+ return _scaled_size(self._map_tile_width, self._scale)
466
600
 
467
601
  @property
468
602
  def tile_height(self):
469
- """The height of one square of the map on the screen, in pixels."""
470
- return self._map_tile_height * self._scale
603
+ """The height of one square of the map on the screen, in whole pixels."""
604
+ return _scaled_size(self._map_tile_height, self._scale)
471
605
 
472
606
  @property
473
607
  def x(self):
474
- """How far the map has been scrolled right, in pixels."""
608
+ """How far the map has been scrolled right, in pixels: the x of the
609
+ corner of square (0, 0) on the screen."""
475
610
  return self._x
476
611
 
477
612
  @x.setter
@@ -480,13 +615,40 @@ class TileMap(Mapping):
480
615
 
481
616
  @property
482
617
  def y(self):
483
- """How far the map has been scrolled down, in pixels."""
618
+ """How far the map has been scrolled down, in pixels: the y of the
619
+ corner of square (0, 0) on the screen."""
484
620
  return self._y
485
621
 
486
622
  @y.setter
487
623
  def y(self, y):
488
624
  self._scroll(0, y - self._y)
489
625
 
626
+ @property
627
+ def angle(self):
628
+ """How far the map has been turned with rotate(), counterclockwise in
629
+ degrees: 0, 90, 180 or 270."""
630
+ return self._angle
631
+
632
+ @angle.setter
633
+ def angle(self, angle):
634
+ raise AttributeError(
635
+ "A map's angle can't be set directly. To turn the map, use "
636
+ "level.rotate(90, around=player), which also says what to turn it around."
637
+ )
638
+
639
+ def _to_screen(self, x, y):
640
+ """The screen position of the point x, y pixels from the map's corner,
641
+ before turning."""
642
+ tx, ty = _turn(x, y, self._angle)
643
+ return self._x + tx, self._y + ty
644
+
645
+ def _square_topleft(self, col, row):
646
+ """The top left corner on the screen of the square at col, row."""
647
+ w, h = self.tile_width, self.tile_height
648
+ x1, y1 = self._to_screen(col * w, row * h)
649
+ x2, y2 = self._to_screen((col + 1) * w, (row + 1) * h)
650
+ return min(x1, x2), min(y1, y2)
651
+
490
652
  def _scroll(self, dx, dy):
491
653
  """Move the whole map by dx, dy pixels, in one pass over its tiles."""
492
654
  if not dx and not dy:
@@ -505,18 +667,17 @@ class TileMap(Mapping):
505
667
  )
506
668
  _check_number("x", x)
507
669
  _check_number("y", y)
670
+ # Turn the position back to how it would be on the unturned map.
671
+ map_x, map_y = _turn(x - self._x, y - self._y, -self._angle)
508
672
  return (
509
- math.floor((x - self._x) / self.tile_width),
510
- math.floor((y - self._y) / self.tile_height),
673
+ math.floor(map_x / self.tile_width),
674
+ math.floor(map_y / self.tile_height),
511
675
  )
512
676
 
513
677
  def map_to_pos(self, col, row):
514
678
  """Return the x and y, in pixels, of the center of the square at col, row."""
515
679
  col, row = _check_col_row(col, row)
516
- return (
517
- self._x + (col + 0.5) * self.tile_width,
518
- self._y + (row + 0.5) * self.tile_height,
519
- )
680
+ return self._to_screen((col + 0.5) * self.tile_width, (row + 0.5) * self.tile_height)
520
681
 
521
682
  def follow(self, actor, others=()):
522
683
  """Scroll the map so that actor is in the middle of the screen.
@@ -539,8 +700,11 @@ class TileMap(Mapping):
539
700
  others = [others]
540
701
 
541
702
  screen_width, screen_height = game.screen.get_size()
542
- dx = self._follow_shift(actor.x, self._x, self._cols * self.tile_width, screen_width)
543
- dy = self._follow_shift(actor.y, self._y, self._rows * self.tile_height, screen_height)
703
+ # The map's edges on the screen, which are swapped round once it's turned
704
+ x1, y1 = self._to_screen(0, 0)
705
+ x2, y2 = self._to_screen(self._cols * self.tile_width, self._rows * self.tile_height)
706
+ dx = self._follow_shift(actor.x, min(x1, x2), abs(x2 - x1), screen_width)
707
+ dy = self._follow_shift(actor.y, min(y1, y2), abs(y2 - y1), screen_height)
544
708
 
545
709
  if dx or dy:
546
710
  self._scroll(dx, dy)
@@ -561,6 +725,76 @@ class TileMap(Mapping):
561
725
  shift = max(shift, screen_size - (map_pos + map_size)) # nor its right/bottom edge
562
726
  return shift
563
727
 
728
+ def rotate(self, angle, around=None, others=()):
729
+ """Turn the whole map by angle degrees, counterclockwise like
730
+ Actor.angle. angle must be a quarter turn: 90, 180, 270, or -90 to
731
+ turn clockwise.
732
+
733
+ around is what to turn it around: an Actor (the map turns around its
734
+ pos, so it stays where it is) or an (x, y) position. Leave it out to
735
+ turn around the middle of the screen.
736
+
737
+ Every Actor in the list others is carried round with the map, so it
738
+ stays in the same place in the world, but it isn't turned itself: it
739
+ stays upright, so gravity pulls it towards the map's new down.
740
+ """
741
+ if isinstance(angle, bool) or not isinstance(angle, numbers.Real):
742
+ raise TypeError(f"angle should be a number of degrees, like 90 or -90, not {angle!r}.")
743
+ if angle % 90:
744
+ raise ValueError(
745
+ f"A map can only turn in quarter turns, like 90, 180 or -90, not {angle!r}. "
746
+ "Its tiles collide as rectangles with straight sides, which only works "
747
+ "when they're turned in quarter turns."
748
+ )
749
+ angle = int(angle) % 360
750
+ center_x, center_y = self._pivot(around)
751
+ if isinstance(others, Actor):
752
+ others = [others]
753
+
754
+ def turn_point(x, y):
755
+ tx, ty = _turn(x - center_x, y - center_y, angle)
756
+ return center_x + tx, center_y + ty
757
+
758
+ if angle:
759
+ self._angle = (self._angle + angle) % 360
760
+ self._x, self._y = turn_point(self._x, self._y)
761
+ for layer in self._layers.values():
762
+ for tile in layer._tiles.values():
763
+ # Turn where the tile is, rather than putting it back in
764
+ # its square, so tiles the game has moved stay moved.
765
+ tile._set_map_angle(self._angle)
766
+ tile.center = turn_point(*tile.center)
767
+ for thing in others:
768
+ # A layer of this map given by mistake has already been turned.
769
+ if isinstance(thing, Tile) and thing._layer is not None and thing._layer._map is self:
770
+ continue
771
+ thing.pos = turn_point(*thing.pos)
772
+
773
+ def _pivot(self, around):
774
+ """The point that rotate() turns the map around."""
775
+ if around is None:
776
+ from . import game
777
+
778
+ if game.screen is None:
779
+ raise RuntimeError(
780
+ "level.rotate() turns the map around the middle of the game window, so "
781
+ "call it in start() or update(), or say what to turn it around, like "
782
+ "level.rotate(90, around=player)."
783
+ )
784
+ width, height = game.screen.get_size()
785
+ return width / 2, height / 2
786
+ if isinstance(around, Actor):
787
+ return around.pos
788
+ try:
789
+ x, y = around
790
+ except (TypeError, ValueError):
791
+ raise TypeError(
792
+ f"around should be an Actor or an (x, y) position, not {around!r}."
793
+ ) from None
794
+ _check_number("x", x)
795
+ _check_number("y", y)
796
+ return x, y
797
+
564
798
  def draw(self):
565
799
  """Draw every visible layer, in order."""
566
800
  for layer in self._layers.values():
@@ -622,6 +856,7 @@ class TileMap(Mapping):
622
856
  flip_v=flip_v,
623
857
  flip_d=flip_d,
624
858
  layer=layer,
859
+ map_angle=self._angle,
625
860
  )
626
861
 
627
862
 
@@ -788,14 +1023,18 @@ def _tile_layers(element, visible=True):
788
1023
  yield from _tile_layers(child, child_visible)
789
1024
 
790
1025
 
791
- def load_tile_map(tmx_name, scale=1):
1026
+ def load_tile_map(tmx_name, scale=1.0):
792
1027
  """
793
1028
  Load a Tiled TMX map into a TileMap: a dict of
794
1029
  {layer_name: TileLayer}
795
1030
  where each layer works like a list of tile Actors.
796
1031
 
1032
+ scale can be a whole number or not, like 2 or 1.5. Tiles are rounded to
1033
+ whole pixels so that they meet without gaps.
1034
+
797
1035
  The TMX/TSX/image files are loaded through pgzero loaders from the maps folder.
798
1036
  """
1037
+ _check_scale(scale)
799
1038
  root = _load_xml_from_maps(tmx_name)
800
1039
 
801
1040
  orientation = root.attrib.get("orientation", "orthogonal")
@@ -816,7 +1055,7 @@ def load_tile_map(tmx_name, scale=1):
816
1055
  name=tmx_name,
817
1056
  map_tile_width=int(root.attrib["tilewidth"]),
818
1057
  map_tile_height=int(root.attrib["tileheight"]),
819
- scale=max(0.0, scale),
1058
+ scale=scale,
820
1059
  cols=int(root.attrib.get("width", 0)),
821
1060
  rows=int(root.attrib.get("height", 0)),
822
1061
  tilesets=tilesets,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pgzero-plus
3
- Version: 1.0.0
3
+ Version: 1.0.1
4
4
  Summary: An enhanced fork of Pygame Zero with additional features for teaching and developing 2D games
5
5
  Author-email: Michael D'Argenio <mjdargen@gmail.com>
6
6
  License-Expression: LGPL-3.0-or-later
@@ -12,6 +12,8 @@ Fixtures live in ``maps/``:
12
12
  * ``base64_layer.tmx`` / ``bad_gid.tmx`` - invalid maps.
13
13
 
14
14
  """
15
+ import inspect
16
+ import math
15
17
  import unittest
16
18
  from unittest import mock
17
19
  import xml.etree.ElementTree as ET
@@ -33,6 +35,7 @@ from pgzero.tilemaps import (
33
35
  _count_tiles_that_fit,
34
36
  _load_tilesets,
35
37
  _resolve_relative_map_path,
38
+ _turn,
36
39
  load_tile_map_actors,
37
40
  )
38
41
 
@@ -559,6 +562,71 @@ class SharedTileImageTest(TilemapTestBase):
559
562
  self.assertEqual(tile.pos, pos)
560
563
 
561
564
 
565
+ class FractionalScaleTest(TilemapTestBase):
566
+ """Scales that aren't whole numbers. level.tmx has 16 x 16 squares."""
567
+
568
+ def test_scale_with_whole_pixel_tiles(self):
569
+ level = load_tile_map("level", scale=1.5)
570
+ self.assertEqual((level.tile_width, level.tile_height), (24, 24))
571
+ for t in level["ground"]:
572
+ self.assertEqual((t.width, t.height), (24, 24))
573
+ self.assertEqual(t.topleft, (t.map_col * 24, t.map_row * 24))
574
+
575
+ def test_squares_are_rounded_to_whole_pixels(self):
576
+ """16 * 0.7 is 11.2, so the grid and the images are both 11 pixels."""
577
+ level = load_tile_map("level", scale=0.7)
578
+ self.assertEqual((level.tile_width, level.tile_height), (11, 11))
579
+ for t in level["ground"]:
580
+ self.assertEqual(t._orig_surf.get_size(), (11, 11))
581
+ self.assertEqual(t.topleft, (t.map_col * 11, t.map_row * 11))
582
+ self.assertEqual(level.map_to_pos(t.map_col, t.map_row), t.pos)
583
+
584
+ def test_no_gaps_between_tiles(self):
585
+ level = load_tile_map("level", scale=0.7)
586
+ screen = pygame.Surface((200, 200), pygame.SRCALPHA)
587
+ with mock.patch("pgzero.game.screen", screen):
588
+ for x in [0, 7, -3, 2.5]:
589
+ screen.fill((0, 0, 0, 0))
590
+ level.x = x
591
+ level.draw()
592
+ # Row 1 has a tile in every square, so it should be solid.
593
+ y = level.map_to_pos(0, 1)[1]
594
+ left = max(0, math.ceil(level.x))
595
+ right = math.floor(level.x + level.cols * level.tile_width)
596
+ empty = [px for px in range(left, right) if screen.get_at((px, int(y))).a == 0]
597
+ self.assertEqual(empty, [], f"level.x = {x}")
598
+
599
+ def test_new_tiles_on_the_rounded_grid(self):
600
+ level = load_tile_map("level", scale=0.7)
601
+ tile = level["ground"].set_tile(1, 0, level.create_tile(0, 0, tileset="tiles"))
602
+ self.assertEqual(tile.topleft, (11, 0))
603
+ self.assertEqual((tile.width, tile.height), (11, 11))
604
+
605
+ def test_tile_not_on_a_map(self):
606
+ ground = load_tile_map("level")["ground"]
607
+ tile = Tile(
608
+ tileset=ground[0]._tileset, tileset_firstgid=1, tile_col=0, tile_row=0,
609
+ map_col=2, map_row=1, map_tile_width=16, map_tile_height=16, scale=0.7,
610
+ )
611
+ self.assertEqual(tile.topleft, (22, 11))
612
+
613
+ def test_default_scale_is_a_float(self):
614
+ """So that type checkers accept a scale like 1.5."""
615
+ default = inspect.signature(load_tile_map).parameters["scale"].default
616
+ self.assertIsInstance(default, float)
617
+ self.assertEqual(default, 1)
618
+
619
+ def test_scale_must_be_a_number(self):
620
+ for scale in ["2", (2, 2), None, True]:
621
+ with self.assertRaisesRegex(TypeError, "scale should be a number"):
622
+ load_tile_map("level", scale=scale)
623
+
624
+ def test_scale_must_be_bigger_than_zero(self):
625
+ for scale in [0, -1, -0.5]:
626
+ with self.assertRaisesRegex(ValueError, "scale should be bigger than 0"):
627
+ load_tile_map("level", scale=scale)
628
+
629
+
562
630
  # level.tmx's "ground" layer is 4x2 squares of 16x16 pixels, with (1, 0) empty:
563
631
  #
564
632
  # (0, 0) tiles (0, 0) (1, 0) empty (2, 0) tiles (2, 1) (3, 0) tiles (0, 1)
@@ -1110,6 +1178,207 @@ class DrawTest(TilemapTestBase):
1110
1178
  self.assertEqual([call[0][0] for call in draw.call_args_list], self.level.layers)
1111
1179
 
1112
1180
 
1181
+ def same_pixels(a, b):
1182
+ return a.get_size() == b.get_size() and pygame.image.tobytes(a, "RGBA") == pygame.image.tobytes(b, "RGBA")
1183
+
1184
+
1185
+ class RotateTest(TilemapTestBase):
1186
+ """level.rotate(): the 4 x 2 ground layer of 16 x 16 squares, turned
1187
+ around (0, 0) unless a test says otherwise. Turning counterclockwise by
1188
+ 90 moves a point (x, y) to (y, -x)."""
1189
+
1190
+ def setUp(self):
1191
+ self.level = load_tile_map("level")
1192
+ self.ground = self.level["ground"]
1193
+
1194
+ def images(self):
1195
+ return {map_pos(t): t._build_transformed_surf() for t in self.ground}
1196
+
1197
+ def test_angle(self):
1198
+ self.assertEqual(self.level.angle, 0)
1199
+ for turn, angle in [(90, 90), (90, 180), (180, 0), (-90, 270), (450, 0), (90.0, 90)]:
1200
+ self.level.rotate(turn, around=(0, 0))
1201
+ self.assertEqual(self.level.angle, angle)
1202
+ self.assertIsInstance(self.level.angle, int)
1203
+
1204
+ def test_angle_cannot_be_set(self):
1205
+ with self.assertRaisesRegex(AttributeError, r"level.rotate\(90"):
1206
+ self.level.angle = 90
1207
+
1208
+ def test_tiles_turn_around_a_point(self):
1209
+ self.level.rotate(90, around=(0, 0))
1210
+ self.assertEqual(self.ground.get_tile(0, 0).center, (8, -8))
1211
+ self.assertEqual(self.ground.get_tile(3, 1).center, (24, -56))
1212
+ self.level.rotate(90, around=(100, 50))
1213
+ # (8, -8) is (-92, -58) from (100, 50), which turns to (-58, 92)
1214
+ self.assertEqual(self.ground.get_tile(0, 0).center, (42, 142))
1215
+
1216
+ def test_tile_images_turn(self):
1217
+ """Every tile, flipped in Tiled or not, looks like its image turned."""
1218
+ before = self.images()
1219
+ for angle in [90, 180, 270]:
1220
+ level = load_tile_map("level")
1221
+ level.rotate(angle, around=(0, 0))
1222
+ for tile in level["ground"]:
1223
+ expected = pygame.transform.rotate(before[map_pos(tile)], angle)
1224
+ self.assertTrue(same_pixels(tile._build_transformed_surf(), expected), (angle, map_pos(tile)))
1225
+
1226
+ def test_tiles_keep_their_tiled_flips(self):
1227
+ flips = {map_pos(t): (t.flip_h, t.flip_v, t.flip_d) for t in self.ground}
1228
+ self.level.rotate(90, around=(0, 0))
1229
+ self.assertEqual({map_pos(t): (t.flip_h, t.flip_v, t.flip_d) for t in self.ground}, flips)
1230
+
1231
+ def test_flipping_a_turned_tile(self):
1232
+ unturned = self.ground.get_tile(0, 0).copy()
1233
+ unturned.flip_h = True
1234
+ tile = self.ground.get_tile(0, 0)
1235
+ self.level.rotate(90, around=(0, 0))
1236
+ tile.flip_h = True
1237
+ self.assertTrue(tile.flip_h)
1238
+ expected = pygame.transform.rotate(unturned._build_transformed_surf(), 90)
1239
+ self.assertTrue(same_pixels(tile._build_transformed_surf(), expected))
1240
+
1241
+ def test_turned_tiles_share_images(self):
1242
+ tile = self.level.create_tile(1, 1, tileset="tiles")
1243
+ other = self.level.create_tile(1, 1, tileset="tiles")
1244
+ self.ground.set_tile(1, 0, tile)
1245
+ self.ground.set_tile(0, 0, other)
1246
+ self.level.rotate(90, around=(0, 0))
1247
+ self.assertIs(tile._build_transformed_surf(), other._build_transformed_surf())
1248
+
1249
+ def test_flipped_tiles_share_images(self):
1250
+ flipped = self.ground.get_tile(0, 1) # flipped horizontally in Tiled
1251
+ other = self.ground.set_tile(1, 0, flipped.copy())
1252
+ self.assertIs(other._build_transformed_surf(), flipped._build_transformed_surf())
1253
+
1254
+ def test_turning_all_the_way_round(self):
1255
+ topleft = {map_pos(t): t.topleft for t in self.ground}
1256
+ images = self.images()
1257
+ for _ in range(4):
1258
+ self.level.rotate(90, around=(30, 7))
1259
+ self.assertEqual({map_pos(t): t.topleft for t in self.ground}, topleft)
1260
+ for tile in self.ground:
1261
+ self.assertTrue(same_pixels(tile._build_transformed_surf(), images[map_pos(tile)]))
1262
+
1263
+ def test_squares_and_positions_after_turning(self):
1264
+ for angle in [90, 180, 270]:
1265
+ level = load_tile_map("level")
1266
+ level.x, level.y = 13, -4
1267
+ level.rotate(angle, around=(20, 30))
1268
+ for tile in level["ground"]:
1269
+ col, row = map_pos(tile)
1270
+ self.assertEqual(level.map_to_pos(col, row), tile.pos, (angle, col, row))
1271
+ self.assertEqual(level.pos_to_map(*tile.pos), (col, row), (angle, col, row))
1272
+
1273
+ def test_collisions_after_turning(self):
1274
+ self.level.rotate(90, around=(0, 0))
1275
+ player = Actor(pygame.Surface((4, 4)), center=(8, -40)) # where square (2, 0) is now
1276
+ self.assertEqual(map_pos(self.ground[player.collidelist(self.ground)]), (2, 0))
1277
+
1278
+ def test_actor_turned_around_stays_put(self):
1279
+ player = Actor(pygame.Surface((4, 6)), center=(40, 10)) # in square (2, 0)
1280
+ self.level.rotate(90, around=player)
1281
+ self.assertEqual(player.pos, (40, 10))
1282
+ self.assertEqual(self.level.pos_to_map(player.x, player.y), (2, 0))
1283
+
1284
+ def test_others_are_carried_round_but_not_turned(self):
1285
+ enemy = Actor(pygame.Surface((4, 6)), center=(40, 10))
1286
+ self.level.rotate(90, around=(0, 0), others=[enemy])
1287
+ self.assertEqual(enemy.pos, (10, -40))
1288
+ self.assertEqual((enemy.width, enemy.height, enemy.angle), (4, 6, 0))
1289
+ self.level.rotate(-90, around=(0, 0), others=enemy) # one Actor is fine too
1290
+ self.assertEqual(enemy.pos, (40, 10))
1291
+
1292
+ def test_a_layer_in_others_isnt_turned_twice(self):
1293
+ self.level.rotate(90, around=(0, 0), others=self.ground)
1294
+ self.assertEqual(self.ground.get_tile(0, 0).center, (8, -8))
1295
+
1296
+ def test_collision_rect_turns_with_the_tile(self):
1297
+ """A tile with an off-center collision_rect: its rect and its image
1298
+ both end up where turning them would put them."""
1299
+
1300
+ def turned_box(x, y, w, h):
1301
+ corners = [_turn(cx, cy, 90) for cx, cy in [(x, y), (x + w, y + h)]]
1302
+ return (min(c[0] for c in corners), min(c[1] for c in corners), h, w)
1303
+
1304
+ tile = self.ground.get_tile(0, 0)
1305
+ tile.collision_rect = (4, 8, 2, 3)
1306
+ body = tuple(tile._rect)
1307
+ image = (*tile._image_topleft(), 16, 16)
1308
+ self.level.rotate(90, around=(0, 0))
1309
+ self.assertEqual(tuple(tile._rect), turned_box(*body))
1310
+ self.assertEqual((*tile._image_topleft(), 16, 16), turned_box(*image))
1311
+
1312
+ def test_tiles_moved_by_the_game_stay_moved(self):
1313
+ tile = self.ground.get_tile(0, 0)
1314
+ tile.y -= 3 # e.g. a bouncing block
1315
+ self.level.rotate(90, around=(0, 0))
1316
+ self.assertEqual(tile.center, (5, -8))
1317
+
1318
+ def test_new_tiles_on_a_turned_map(self):
1319
+ self.level.rotate(90, around=(0, 0))
1320
+ tile = self.ground.set_tile(1, 0, self.level.create_tile(0, 0, tileset="tiles"))
1321
+ self.assertEqual(tile.pos, self.level.map_to_pos(1, 0))
1322
+ expected = pygame.transform.rotate(self.images()[0, 0], 0) # (0, 0) is the same kind, turned too
1323
+ self.assertTrue(same_pixels(tile._build_transformed_surf(), expected))
1324
+
1325
+ def test_tile_taken_off_and_put_back(self):
1326
+ tile = self.ground.pop(0)
1327
+ self.level.rotate(90, around=(0, 0))
1328
+ self.ground.set_tile(0, 0, tile)
1329
+ self.assertEqual(tile.pos, self.level.map_to_pos(0, 0))
1330
+ self.assertTrue(same_pixels(tile._build_transformed_surf(), self.ground.get_tile(0, 0)._build_transformed_surf()))
1331
+
1332
+ def test_scrolling_after_turning(self):
1333
+ self.level.rotate(90, around=(0, 0))
1334
+ topleft = {map_pos(t): t.topleft for t in self.ground}
1335
+ self.level.x += 10
1336
+ for tile in self.ground:
1337
+ x, y = topleft[map_pos(tile)]
1338
+ self.assertEqual(tile.topleft, (x + 10, y))
1339
+
1340
+ def test_follow_after_turning(self):
1341
+ """Turned, the map is 32 wide and 64 tall on a 32 x 16 screen."""
1342
+ self.level.rotate(90, around=(0, 0))
1343
+ player = Actor(pygame.Surface((4, 4)), pos=self.level.map_to_pos(0, 0))
1344
+ with mock.patch("pgzero.game.screen", pygame.Surface((32, 16))):
1345
+ self.level.follow(player)
1346
+ left = min(t.left for t in self.ground)
1347
+ top = min(t.top for t in self.ground)
1348
+ right = max(t.right for t in self.ground)
1349
+ bottom = max(t.bottom for t in self.ground)
1350
+ self.assertEqual((left, right), (0, 32)) # as wide as the screen, so it fits exactly
1351
+ self.assertEqual(bottom, 16) # square (0, 0) is at the bottom, so the map stops there
1352
+ self.assertEqual(top, -48)
1353
+ self.assertEqual(player.pos, self.level.map_to_pos(0, 0))
1354
+
1355
+ def test_turns_around_the_middle_of_the_screen(self):
1356
+ with mock.patch("pgzero.game.screen", pygame.Surface((200, 100))):
1357
+ self.level.rotate(180)
1358
+ # (8, 8) is (-92, -42) from (100, 50), which turns to (92, 42)
1359
+ self.assertEqual(self.ground.get_tile(0, 0).center, (192, 92))
1360
+
1361
+ def test_middle_of_the_screen_needs_the_game_window(self):
1362
+ with mock.patch("pgzero.game.screen", None):
1363
+ with self.assertRaisesRegex(RuntimeError, "around=player"):
1364
+ self.level.rotate(90)
1365
+
1366
+ def test_only_quarter_turns(self):
1367
+ for angle in [45, 1, 90.5]:
1368
+ with self.assertRaisesRegex(ValueError, "quarter turns"):
1369
+ self.level.rotate(angle, around=(0, 0))
1370
+ for angle in ["90", True, None]:
1371
+ with self.assertRaisesRegex(TypeError, "angle should be a number"):
1372
+ self.level.rotate(angle, around=(0, 0))
1373
+ self.assertEqual(self.level.angle, 0)
1374
+
1375
+ def test_bad_around(self):
1376
+ for around in ["player", 5, (1, 2, 3), ("a", 2)]:
1377
+ with self.assertRaises(TypeError):
1378
+ self.level.rotate(90, around=around)
1379
+ self.assertEqual(self.level.angle, 0)
1380
+
1381
+
1113
1382
  PROPERTIES_XML = """
1114
1383
  <map orientation="orthogonal" width="2" height="1" tilewidth="8" tileheight="8">
1115
1384
  <tileset firstgid="1" name="tiles" tilewidth="8" tileheight="8"
File without changes
File without changes
File without changes
@@ -9,8 +9,8 @@ __main__).
9
9
  """
10
10
 
11
11
  import sys
12
- from pgzero.runner import prepare_mod, run_mod
13
12
 
13
+ from pgzero.runner import prepare_mod, run_mod
14
14
 
15
15
  mod = sys.modules["__main__"]
16
16