pymunk_demo_platformer_05.py Full Listing
pymunk_demo_platformer_05.py
1"""
2Example of Pymunk Physics Engine Platformer
3"""
4
5from typing import Optional
6import arcade
7
8SCREEN_TITLE = "PyMunk Platformer"
9
10# How big are our image tiles?
11SPRITE_IMAGE_SIZE = 128
12
13# Scale sprites up or down
14SPRITE_SCALING_PLAYER = 0.5
15SPRITE_SCALING_TILES = 0.5
16
17# Scaled sprite size for tiles
18SPRITE_SIZE = int(SPRITE_IMAGE_SIZE * SPRITE_SCALING_PLAYER)
19
20# Size of grid to show on screen, in number of tiles
21SCREEN_GRID_WIDTH = 25
22SCREEN_GRID_HEIGHT = 15
23
24# Size of screen to show, in pixels
25SCREEN_WIDTH = SPRITE_SIZE * SCREEN_GRID_WIDTH
26SCREEN_HEIGHT = SPRITE_SIZE * SCREEN_GRID_HEIGHT
27
28# --- Physics forces. Higher number, faster accelerating.
29
30# Gravity
31GRAVITY = 1500
32
33# Damping - Amount of speed lost per second
34DEFAULT_DAMPING = 1.0
35PLAYER_DAMPING = 0.4
36
37# Friction between objects
38PLAYER_FRICTION = 1.0
39WALL_FRICTION = 0.7
40DYNAMIC_ITEM_FRICTION = 0.6
41
42# Mass (defaults to 1)
43PLAYER_MASS = 2.0
44
45# Keep player from going too fast
46PLAYER_MAX_HORIZONTAL_SPEED = 450
47PLAYER_MAX_VERTICAL_SPEED = 1600
48
49
50class GameWindow(arcade.Window):
51 """Main Window"""
52
53 def __init__(self, width, height, title):
54 """Create the variables"""
55
56 # Init the parent class
57 super().__init__(width, height, title)
58
59 # Player sprite
60 self.player_sprite: arcade.Sprite | None = None
61
62 # Sprite lists we need
63 self.player_list: arcade.SpriteList | None = None
64 self.wall_list: arcade.SpriteList | None = None
65 self.bullet_list: arcade.SpriteList | None = None
66 self.item_list: arcade.SpriteList | None = None
67
68 # Track the current state of what key is pressed
69 self.left_pressed: bool = False
70 self.right_pressed: bool = False
71
72 # Physics engine
73 self.physics_engine: arcade.PymunkPhysicsEngine | None = None
74
75 # Set background color
76 self.background_color = arcade.color.AMAZON
77
78 def setup(self):
79 """Set up everything with the game"""
80
81 # Create the sprite lists
82 self.player_list = arcade.SpriteList()
83 self.bullet_list = arcade.SpriteList()
84
85 # Map name
86 map_name = ":resources:/tiled_maps/pymunk_test_map.json"
87
88 # Load in TileMap
89 tile_map = arcade.load_tilemap(map_name, SPRITE_SCALING_TILES)
90
91 # Pull the sprite layers out of the tile map
92 self.wall_list = tile_map.sprite_lists["Platforms"]
93 self.item_list = tile_map.sprite_lists["Dynamic Items"]
94
95 # Create player sprite
96 self.player_sprite = arcade.Sprite(
97 ":resources:images/animated_characters/female_person/femalePerson_idle.png",
98 SPRITE_SCALING_PLAYER,
99 )
100 # Set player location
101 grid_x = 1
102 grid_y = 1
103 self.player_sprite.center_x = SPRITE_SIZE * grid_x + SPRITE_SIZE / 2
104 self.player_sprite.center_y = SPRITE_SIZE * grid_y + SPRITE_SIZE / 2
105 # Add to player sprite list
106 self.player_list.append(self.player_sprite)
107
108 # --- Pymunk Physics Engine Setup ---
109
110 # The default damping for every object controls the percent of velocity
111 # the object will keep each second. A value of 1.0 is no speed loss,
112 # 0.9 is 10% per second, 0.1 is 90% per second.
113 # For top-down games, this is basically the friction for moving objects.
114 # For platformers with gravity, this should probably be set to 1.0.
115 # Default value is 1.0 if not specified.
116 damping = DEFAULT_DAMPING
117
118 # Set the gravity. (0, 0) is good for outer space and top-down.
119 gravity = (0, -GRAVITY)
120
121 # Create the physics engine
122 self.physics_engine = arcade.PymunkPhysicsEngine(damping=damping, gravity=gravity)
123
124 # Add the player.
125 # For the player, we set the damping to a lower value, which increases
126 # the damping rate. This prevents the character from traveling too far
127 # after the player lets off the movement keys.
128 # Setting the moment of inertia to PymunkPhysicsEngine.MOMENT_INF prevents it from
129 # rotating.
130 # Friction normally goes between 0 (no friction) and 1.0 (high friction)
131 # Friction is between two objects in contact. It is important to remember
132 # in top-down games that friction moving along the 'floor' is controlled
133 # by damping.
134 self.physics_engine.add_sprite(
135 self.player_sprite,
136 friction=PLAYER_FRICTION,
137 mass=PLAYER_MASS,
138 moment_of_inertia=arcade.PymunkPhysicsEngine.MOMENT_INF,
139 collision_type="player",
140 max_horizontal_velocity=PLAYER_MAX_HORIZONTAL_SPEED,
141 max_vertical_velocity=PLAYER_MAX_VERTICAL_SPEED,
142 )
143
144 # Create the walls.
145 # By setting the body type to PymunkPhysicsEngine.STATIC the walls can't
146 # move.
147 # Movable objects that respond to forces are PymunkPhysicsEngine.DYNAMIC
148 # PymunkPhysicsEngine.KINEMATIC objects will move, but are assumed to be
149 # repositioned by code and don't respond to physics forces.
150 # Dynamic is default.
151 self.physics_engine.add_sprite_list(
152 self.wall_list,
153 friction=WALL_FRICTION,
154 collision_type="wall",
155 body_type=arcade.PymunkPhysicsEngine.STATIC,
156 )
157
158 # Create the items
159 self.physics_engine.add_sprite_list(
160 self.item_list, friction=DYNAMIC_ITEM_FRICTION, collision_type="item"
161 )
162
163 def on_key_press(self, key, modifiers):
164 """Called whenever a key is pressed."""
165 pass
166
167 def on_key_release(self, key, modifiers):
168 """Called when the user releases a key."""
169 pass
170
171 def on_update(self, delta_time):
172 """Movement and game logic"""
173 self.physics_engine.step()
174
175 def on_draw(self):
176 """Draw everything"""
177 self.clear()
178 self.wall_list.draw()
179 self.bullet_list.draw()
180 self.item_list.draw()
181 self.player_list.draw()
182
183
184def main():
185 """Main function"""
186 window = GameWindow(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
187 window.setup()
188 arcade.run()
189
190
191if __name__ == "__main__":
192 main()