Skip to content

Commit e7c0aa2

Browse files
committed
Updated level 4, added handling for synchronized platform movement
1 parent af46195 commit e7c0aa2

File tree

2 files changed

+100
-26
lines changed

2 files changed

+100
-26
lines changed

arcade-platformer/arcade_platformer/arcade_platformer.py

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111
# Import libraries
12+
from os import sync
1213
import pathlib
1314

1415
import arcade
@@ -481,19 +482,41 @@ def setup(self) -> None:
481482
scaling=game.MAP_SCALING,
482483
)
483484
for sprite in moving_platforms_list:
485+
# Set the initial position of each moving platform
486+
if "boundary_left" in sprite.properties:
487+
sprite.left = sprite.properties["boundary_left"]
488+
if "boundary_bottom" in sprite.properties:
489+
sprite.bottom = sprite.properties["boundary_bottom"]
490+
484491
self.walls_list.append(sprite)
485492

486-
# Set the initial position of each moving platform to the left and/or
487-
# bottom
488-
for moving_platform in moving_platforms_list:
489-
if "boundary_left" in moving_platform.properties:
490-
moving_platform.left = moving_platform.properties[
491-
"boundary_left"
492-
]
493-
if "boundary_bottom" in moving_platform.properties:
494-
moving_platform.bottom = moving_platform.properties[
495-
"boundary_bottom"
496-
]
493+
# Process synchronized platforms
494+
synchronized_platforms_layer_name = "synch_platforms"
495+
synchronized_platforms_list = arcade.tilemap.process_layer(
496+
map,
497+
layer_name=synchronized_platforms_layer_name,
498+
scaling=game.MAP_SCALING,
499+
)
500+
501+
# Create a dict of grouped sprites
502+
self.synchronized_groups = {}
503+
for sprite in synchronized_platforms_list:
504+
# Get the current group number
505+
current_group = sprite.properties["group"]
506+
# If this group number isn't in the dict, create a new list
507+
if current_group not in self.synchronized_groups.keys():
508+
self.synchronized_groups[current_group] = list()
509+
# Append the sprite to the given list
510+
self.synchronized_groups[current_group].append(sprite)
511+
512+
# Set the initial position of each moving platform
513+
if "boundary_left" in sprite.properties:
514+
sprite.left = sprite.properties["boundary_left"]
515+
if "boundary_bottom" in sprite.properties:
516+
sprite.bottom = sprite.properties["boundary_bottom"]
517+
518+
# Add each sprite to the walls list so collision works
519+
self.walls_list.append(sprite)
497520

498521
# Set the background color
499522
background_color = arcade.color.AERO_BLUE
@@ -762,6 +785,22 @@ def on_update(self, delta_time: float) -> None:
762785
if self.player.left < 0:
763786
self.player.left = 0
764787

788+
# Update the synchronized platforms
789+
for synch_group in self.synchronized_groups.values():
790+
# Find the master
791+
792+
for sprite in synch_group:
793+
if sprite.properties["order"] == 0:
794+
master = sprite
795+
796+
# Update the rest based on the master
797+
for sprite in synch_group:
798+
if sprite.properties["order"] != 0:
799+
diff_x = sprite.properties["diff_x"]
800+
diff_y = sprite.properties["diff_y"]
801+
sprite.left = master.left + diff_x
802+
sprite.bottom = master.bottom + diff_y
803+
765804
# Check if we've picked up a coin
766805
coins_hit = arcade.check_for_collision_with_list(
767806
sprite=self.player, sprite_list=self.coins_list

arcade-platformer/assets/platform_level_04.tmx

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,85 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<map version="1.2" tiledversion="1.3.1" orientation="orthogonal" renderorder="right-down" compressionlevel="0" width="25" height="20" tilewidth="128" tileheight="128" infinite="0" backgroundcolor="#72cdd5" nextlayerid="7" nextobjectid="37">
2+
<map version="1.2" tiledversion="1.3.4" orientation="orthogonal" renderorder="right-down" compressionlevel="0" width="25" height="20" tilewidth="128" tileheight="128" infinite="0" backgroundcolor="#72cdd5" nextlayerid="8" nextobjectid="43">
33
<tileset firstgid="1" source="arcade_platformer.tsx"/>
44
<layer id="1" name="ground" width="25" height="20">
55
<data encoding="base64" compression="zlib">
66
eAFjYBgFQy0EuIAO5kbCPGgeYAHyBdEwKxlq0LSMcskMAfT4QI8LmLHEqoOpH6XJCwHkcMYVFzCTSVEL0zNKkxYCsDAmFBekmUqaavTyEsQXAmJ20oxBUc0G5CGbywzkY8MomsjgIJsJAGJwAnw=
77
</data>
88
</layer>
9-
<objectgroup id="6" name="moving_platforms">
9+
<objectgroup id="7" name="synch_platforms">
1010
<object id="16" gid="13" x="960" y="2350" width="128" height="128">
1111
<properties>
12-
<property name="boundary_bottom" type="float" value="192"/>
13-
<property name="boundary_top" type="float" value="640"/>
12+
<property name="boundary_bottom" type="float" value="128"/>
13+
<property name="boundary_top" type="float" value="768"/>
1414
<property name="change_y" type="float" value="4"/>
15+
<property name="group" type="int" value="0"/>
16+
<property name="order" type="int" value="0"/>
1517
</properties>
1618
</object>
17-
<object id="17" gid="13" x="1280" y="2100" width="128" height="128"/>
1819
<object id="18" gid="13" x="960" y="1838" width="128" height="128">
1920
<properties>
20-
<property name="boundary_bottom" type="float" value="576"/>
21-
<property name="boundary_top" type="float" value="1088"/>
22-
<property name="change_y" type="float" value="4"/>
21+
<property name="diff_x" type="float" value="0"/>
22+
<property name="diff_y" type="float" value="512"/>
23+
<property name="group" type="int" value="0"/>
24+
<property name="order" type="int" value="1"/>
2325
</properties>
2426
</object>
25-
<object id="19" gid="13" x="1280" y="1588" width="128" height="128"/>
2627
<object id="20" gid="13" x="960" y="1326" width="128" height="128">
2728
<properties>
28-
<property name="boundary_bottom" type="float" value="1216"/>
29-
<property name="boundary_top" type="float" value="1664"/>
30-
<property name="change_y" type="float" value="4"/>
29+
<property name="diff_x" type="float" value="0"/>
30+
<property name="diff_y" type="float" value="940"/>
31+
<property name="group" type="int" value="0"/>
32+
<property name="order" type="int" value="2"/>
3133
</properties>
3234
</object>
33-
<object id="21" gid="13" x="1280" y="1076" width="128" height="128"/>
3435
<object id="22" gid="13" x="960" y="814" width="128" height="128">
3536
<properties>
36-
<property name="boundary_bottom" type="float" value="1728"/>
37-
<property name="boundary_top" type="float" value="2176"/>
37+
<property name="diff_x" type="float" value="0"/>
38+
<property name="diff_y" type="float" value="1408"/>
39+
<property name="group" type="int" value="0"/>
40+
<property name="order" type="int" value="3"/>
41+
</properties>
42+
</object>
43+
<object id="37" gid="13" x="1990" y="2228.34" width="128" height="128">
44+
<properties>
45+
<property name="boundary_bottom" type="float" value="128"/>
46+
<property name="boundary_top" type="float" value="768"/>
3847
<property name="change_y" type="float" value="4"/>
48+
<property name="group" type="int" value="1"/>
49+
<property name="order" type="int" value="0"/>
50+
</properties>
51+
</object>
52+
<object id="38" gid="13" x="1990" y="1789.66" width="128" height="128">
53+
<properties>
54+
<property name="diff_x" type="float" value="0"/>
55+
<property name="diff_y" type="float" value="512"/>
56+
<property name="group" type="int" value="1"/>
57+
<property name="order" type="int" value="1"/>
58+
</properties>
59+
</object>
60+
<object id="39" gid="13" x="1990" y="1318" width="128" height="128">
61+
<properties>
62+
<property name="diff_x" type="float" value="0"/>
63+
<property name="diff_y" type="float" value="1024"/>
64+
<property name="group" type="int" value="1"/>
65+
<property name="order" type="int" value="2"/>
3966
</properties>
4067
</object>
68+
</objectgroup>
69+
<objectgroup id="6" name="moving_platforms">
70+
<object id="17" gid="13" x="1280" y="2100" width="128" height="128"/>
71+
<object id="19" gid="13" x="1280" y="1588" width="128" height="128"/>
72+
<object id="21" gid="13" x="1280" y="1076" width="128" height="128"/>
4173
<object id="23" gid="13" x="1820" y="640" width="128" height="128">
4274
<properties>
4375
<property name="boundary_left" type="float" value="1820"/>
4476
<property name="boundary_right" type="float" value="1960"/>
4577
<property name="change_x" type="float" value="4"/>
4678
</properties>
4779
</object>
80+
<object id="40" gid="13" x="1792" y="2100" width="128" height="128"/>
81+
<object id="41" gid="13" x="1792" y="1588" width="128" height="128"/>
82+
<object id="42" gid="13" x="1792" y="1076" width="128" height="128"/>
4883
</objectgroup>
4984
<layer id="3" name="goal" width="25" height="20">
5085
<data encoding="base64" compression="zlib">

0 commit comments

Comments
 (0)