1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
| import math
import arcade
import os
IMAGE_SIZE = 128
SCREEN_START_WIDTH = 10
SCREEN_START_HEIGHT = 7
TILE_SCALING = 0.4
PLAYER_SCALE = 0.7
PLAYER_SIZE = int(IMAGE_SIZE * PLAYER_SCALE)
PLAYER_MOVE_FORCE_IN_AIR = 900
PLAYER_JUMP_IMPULSE = 1600
SCREEN_WIDTH = 128 * SCREEN_START_WIDTH
SCREEN_HEIGHT = 128 * SCREEN_START_HEIGHT
GRAVITY = 1500
DEFAULT_DAMPING = 1.0
PLAYER_DAMPING = 0.4
PLAYER_FRICTION = 1.0
WALL_FRICTION = 0.7
DYNAMIC_ITEM_FRICTION = 0.6
PLAYER_MASS = 2.0
PLAYER_MAX_HORIZONTAL_SPEED = 200
PLAYER_MAX_VERTICAL_SPEED = 1500
PLAYER_MOVE_FORCE_ON_GROUND = 8000
PLAYER_HEALTH = 3
PLAYER_SCORE = 0
OBJECT_SCALE = 1
OBJECT_START_X = 950
OBJECT_START_Y = 115
DEAD_ZONE = 0.1
RIGHT_FACING = 0
LEFT_FACING = 1
DISTANCE_TO_CHANGE_TEXTURE = 20
BULLET_MOVE_FORCE = 4500
BULLET_MASS = 0.1
BULLET_GRAVITY = 300
class MyGame(arcade.View):
def __init__(self):
super().__init__()
self.player = None
self.player_list = None
self.wall_list = None
self.bullet_list = None
self.item_list = None
self.physics_engine = None
self.object_list = None
self.object = None
self.left_pressed = False
self.right_pressed = False
arcade.set_background_color(arcade.csscolor.CADET_BLUE)
file_path = os.path.dirname(os.path.abspath(__file__))
os.chdir(file_path)
window.set_mouse_visible(True)
arcade.set_background_color(arcade.color.CYAN)
def setup(self):
self.player = Player()
self.player_list = arcade.SpriteList()
self.player_list.append(self.player)
self.object_list = arcade.SpriteList()
self.object = Enemie()
self.object_list.append(self.object)
self.background_music = arcade.Sound(':resources:music/1918.mp3')
self.background_music.play(volume=0.25)
self.wall_list = arcade.SpriteList()
self.bullet_list = arcade.SpriteList()
self.item_list = arcade.SpriteList()
map_name = ':resources:/tiled_maps/pymunk_test_map.json' # Как заменить эту карту на свою
tile_map = arcade.load_tilemap(map_name, TILE_SCALING)
self.wall_list = tile_map.sprite_lists['Platforms']
self.item_list = tile_map.sprite_lists['Dynamic Items']
damping = DEFAULT_DAMPING
gravity = (0, -GRAVITY)
self.physics_engine = arcade.PymunkPhysicsEngine(damping=damping,
gravity=gravity)
self.physics_engine.add_sprite(
self.player,
mass=PLAYER_MASS,
friction=PLAYER_FRICTION,
moment=arcade.PymunkPhysicsEngine.MOMENT_INF,
collision_type='player',
max_horizontal_velocity=PLAYER_MAX_HORIZONTAL_SPEED,
max_vertical_velocity=PLAYER_MAX_VERTICAL_SPEED
)
self.physics_engine.add_sprite_list(
self.wall_list,
friction=WALL_FRICTION,
collision_type='walls',
body_type=arcade.PymunkPhysicsEngine.STATIC
)
self.physics_engine.add_sprite_list(
self.item_list,
friction=DYNAMIC_ITEM_FRICTION,
collision_type='item'
)
def wall_hit_handler(bullet_sprite, _wall_sprite, _arbiter, _space, _data):
bullet_sprite.remove_from_sprite_lists()
self.physics_engine.add_collision_handler('bullet', 'wall', post_handler=wall_hit_handler)
def item_hit_handler(bullet_sprite, item_sprite, _arbiter, _space, _data):
bullet_sprite.remove_from_sprite_lists()
item_sprite.remove_from_sprite_lists()
self.physics_engine.add_collision_handler('bullet', 'item', post_handler=item_hit_handler)
def on_key_press(self, key: int, modifiers: int):
if key == arcade.key.LEFT or key == arcade.key.A:
self.left_pressed = True
if key == arcade.key.RIGHT or key == arcade.key.D:
self.right_pressed = True
if key == arcade.key.SPACE:
if self.physics_engine.is_on_ground(self.player):
impulse = (0, PLAYER_JUMP_IMPULSE)
self.physics_engine.apply_impulse(self.player, impulse)
elif key == arcade.key.ESCAPE:
menu_view = MenuView()
window.show_view(menu_view)
def on_key_release(self, key: int, modifiers: int):
if key == arcade.key.LEFT or key == arcade.key.A:
self.left_pressed = False
if key == arcade.key.RIGHT or key == arcade.key.D:
self.right_pressed = False
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int):
bullet = BulletSprite(20, 5, arcade.color.WHITE)
self.bullet_list.append(bullet)
start_x = self.player.center_x
start_y = self.player.center_y
bullet.position = self.player.position
dest_x = x
dest_y = y
x_diff = dest_x = start_x
y_diff = dest_y = start_y
angle = math.atan2(y_diff, x_diff)
size = max(self.player.width, self.player.height) / 2
bullet.center_x += size * math.cos(angle)
bullet.center_y += size * math.sin(angle)
bullet.angle = math.degrees(angle)
bullet_gravity = (0, -BULLET_GRAVITY)
self.physics_engine.add_sprite(
bullet,
damping=1.0,
friction=0.6,
collision_type='bullet',
gravity=bullet_gravity,
elasticity=0.9
)
force = (BULLET_MOVE_FORCE, 0)
self.physics_engine.apply_force(bullet, force)
def on_update(self, delta_time: float):
self.player.is_collided = False
self.player.update()
if self.player.collides_with_sprite(self.object) and not self.player.is_collided:
self.player.health -= 1
self.player.is_collided = True
self.object.center_x = OBJECT_START_X
self.object.center_y = OBJECT_START_Y
force = (-PLAYER_MOVE_FORCE_ON_GROUND, 0)
self.physics_engine.apply_force(self.player, force)
self.physics_engine.set_friction(self.player, 0)
elif self.right_pressed and not self.left_pressed:
force = (PLAYER_MOVE_FORCE_ON_GROUND, 0)
self.physics_engine.apply_force(self.player, force)
self.physics_engine.set_friction(self.player, 0)
elif self.left_pressed and not self.right_pressed:
force = (-PLAYER_MOVE_FORCE_ON_GROUND, 0)
self.physics_engine.apply_force(self.player, force)
self.physics_engine.set_friction(self.player, 0)
else:
self.physics_engine.set_friction(self.player, 1.0)
self.physics_engine.step()
def on_draw(self):
self.clear()
self.player_list.draw()
self.wall_list.draw()
self.item_list.draw()
self.bullet_list.draw()
self.object_list.draw()
arcade.draw_text("Health: {}".format(self.player.health), 10, SCREEN_HEIGHT - 20, arcade.color.BLACK, 14)
arcade.draw_text("Score: {}".format(self.player.score), 10, SCREEN_HEIGHT - 40, arcade.color.BLACK, 14)
class Player(arcade.Sprite):
def __init__(self):
super().__init__(':resources:images/animated_characters/male_adventurer/maleAdventurer_idle.png', PLAYER_SCALE)
self.center_x = 150
self.center_y = 96
self.health = PLAYER_HEALTH
self.score = PLAYER_SCORE
self.is_collided = False
main_path = ':resources:images/animated_characters/male_adventurer/maleAdventurer'
self.idle_texture_pair = arcade.load_texture_pair(f'{main_path}_idle.png')
self.jump_texture_pair = arcade.load_texture_pair(f'{main_path}_jump.png')
self.fall_texture_pair = arcade.load_texture_pair(f'{main_path}_fall.png')
self.walk_textures = []
for i in range(8):
texture = arcade.load_texture_pair(f'{main_path}_walk{i}.png')
self.walk_textures.append(texture)
self.texture = self.idle_texture_pair[0]
self.hit_box = self.texture.hit_box_points
self.character_direction = RIGHT_FACING
self.cur_texture = 0
self.x_odometer = 0
def pymunk_moved(self, physics_engine, dx, dy, d_angle):
if dx < -DEAD_ZONE and self.character_direction == RIGHT_FACING:
self.character_direction = LEFT_FACING
elif dx > DEAD_ZONE and self.character_direction == LEFT_FACING:
self.character_direction = RIGHT_FACING
is_on_ground = physics_engine.is_on_ground(self)
self.x_odometer += dx
if not is_on_ground:
if dy > DEAD_ZONE:
self.texture = self.jump_texture_pair[self.character_direction]
return
elif dy < -DEAD_ZONE:
self.texture = self.fall_texture_pair[self.character_direction]
return
if abs(dx) <= DEAD_ZONE:
self.texture = self.idle_texture_pair[self.character_direction]
return
if abs(self.x_odometer) > DISTANCE_TO_CHANGE_TEXTURE:
self.x_odometer = 0
self.cur_texture += 1
if self.cur_texture > 7:
self.cur_texture = 0
self.texture = self.walk_textures[self.cur_texture][self.character_direction]
class Enemie(arcade.Sprite):
def __init__(self):
super().__init__(":resources:images/animated_characters/zombie/zombie_idle.png", OBJECT_SCALE)
self.center_x = OBJECT_START_X
self.center_y = OBJECT_START_Y
def update(self):
self.center_x += self.change_x
self.center_y += self.change_y
if self.right >= SCREEN_WIDTH or self.left <= 0:
self.change_x *= -1
class BulletSprite(arcade.SpriteSolidColor):
def pymunk_moved(self, physics_engine, dx, dy, d_angle):
if self.center_y < -250:
self.remove_from_sprite_lists()
class MenuView(arcade.View):
def on_show(self):
arcade.set_background_color(arcade.color.WHITE)
def on_draw(self):
arcade.start_render()
arcade.draw_text("My Platformer Game", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + 50,
arcade.color.BLACK, font_size=30, anchor_x="center")
arcade.draw_text("Click to Start", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - 50,
arcade.color.BLACK, font_size=20, anchor_x="center")
arcade.draw_text("Press Q to Quit", SCREEN_WIDTH - 100, 20,
arcade.color.BLACK, font_size=14, anchor_x="right")
def on_mouse_press(self, _x, _y, _button, _modifiers):
game_view = MyGame()
game_view.setup()
self.window.show_view(game_view)
def on_key_press(self, _key, _modifiers):
if _key == arcade.key.Q:
self.window.close()
if __name__ == "__main__":
window = arcade.Window(SCREEN_WIDTH, SCREEN_HEIGHT, "Game")
start_view = MenuView()
window.show_view(start_view)
arcade.run() |