pymunk_demo_platformer_05.py Full Listing#
pymunk_demo_platformer_05.py#
1"""
2Example of Pymunk Physics Engine Platformer
3"""
4import math
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: Optional[arcade.Sprite] = None
61
62 # Sprite lists we need
63 self.player_list: Optional[arcade.SpriteList] = None
64 self.wall_list: Optional[arcade.SpriteList] = None
65 self.bullet_list: Optional[arcade.SpriteList] = None
66 self.item_list: Optional[arcade.SpriteList] = 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 = Optional[arcade.PymunkPhysicsEngine]
74
75 # Set background color
76 arcade.set_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(":resources:images/animated_characters/female_person/femalePerson_idle.png",
97 SPRITE_SCALING_PLAYER)
98 # Set player location
99 grid_x = 1
100 grid_y = 1
101 self.player_sprite.center_x = SPRITE_SIZE * grid_x + SPRITE_SIZE / 2
102 self.player_sprite.center_y = SPRITE_SIZE * grid_y + SPRITE_SIZE / 2
103 # Add to player sprite list
104 self.player_list.append(self.player_sprite)
105
106 # --- Pymunk Physics Engine Setup ---
107
108 # The default damping for every object controls the percent of velocity
109 # the object will keep each second. A value of 1.0 is no speed loss,
110 # 0.9 is 10% per second, 0.1 is 90% per second.
111 # For top-down games, this is basically the friction for moving objects.
112 # For platformers with gravity, this should probably be set to 1.0.
113 # Default value is 1.0 if not specified.
114 damping = DEFAULT_DAMPING
115
116 # Set the gravity. (0, 0) is good for outer space and top-down.
117 gravity = (0, -GRAVITY)
118
119 # Create the physics engine
120 self.physics_engine = arcade.PymunkPhysicsEngine(damping=damping,
121 gravity=gravity)
122
123 # Add the player.
124 # For the player, we set the damping to a lower value, which increases
125 # the damping rate. This prevents the character from traveling too far
126 # after the player lets off the movement keys.
127 # Setting the moment to PymunkPhysicsEngine.MOMENT_INF prevents it from
128 # rotating.
129 # Friction normally goes between 0 (no friction) and 1.0 (high friction)
130 # Friction is between two objects in contact. It is important to remember
131 # in top-down games that friction moving along the 'floor' is controlled
132 # by damping.
133 self.physics_engine.add_sprite(self.player_sprite,
134 friction=PLAYER_FRICTION,
135 mass=PLAYER_MASS,
136 moment=arcade.PymunkPhysicsEngine.MOMENT_INF,
137 collision_type="player",
138 max_horizontal_velocity=PLAYER_MAX_HORIZONTAL_SPEED,
139 max_vertical_velocity=PLAYER_MAX_VERTICAL_SPEED)
140
141 # Create the walls.
142 # By setting the body type to PymunkPhysicsEngine.STATIC the walls can't
143 # move.
144 # Movable objects that respond to forces are PymunkPhysicsEngine.DYNAMIC
145 # PymunkPhysicsEngine.KINEMATIC objects will move, but are assumed to be
146 # repositioned by code and don't respond to physics forces.
147 # Dynamic is default.
148 self.physics_engine.add_sprite_list(self.wall_list,
149 friction=WALL_FRICTION,
150 collision_type="wall",
151 body_type=arcade.PymunkPhysicsEngine.STATIC)
152
153 # Create the items
154 self.physics_engine.add_sprite_list(self.item_list,
155 friction=DYNAMIC_ITEM_FRICTION,
156 collision_type="item")
157
158 def on_key_press(self, key, modifiers):
159 """Called whenever a key is pressed. """
160 pass
161
162 def on_key_release(self, key, modifiers):
163 """Called when the user releases a key. """
164 pass
165
166 def on_update(self, delta_time):
167 """ Movement and game logic """
168 self.physics_engine.step()
169
170 def on_draw(self):
171 """ Draw everything """
172 self.clear()
173 self.wall_list.draw()
174 self.bullet_list.draw()
175 self.item_list.draw()
176 self.player_list.draw()
177
178def main():
179 """ Main function """
180 window = GameWindow(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
181 window.setup()
182 arcade.run()
183
184
185if __name__ == "__main__":
186 main()