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
| import arcade
import arcade.gui
SCREEN_WIDTH = 1280
SCREEN_HEIGHT = 768
SCREEN_TITLE = "AtoB"
CHARACTER_SCALING = 1
TILE_SCALING = 1
SPEED_PLAYER = 2
GRAVITY = 0.25
JUMP_SPEED_PLAYER = 15
RIGHT_FACING = 0
LEFT_FACING = 1
class MyGame(arcade.View):
def __init__(self):
super().__init__()
self.manager = arcade.gui.UIManager()
self.manager.enable()
arcade.set_background_color(arcade.color.AMAZON)
self.scene = None
self.player = None
self.player_list=None
self.coin = None
self.setup()
self.phisics_engine = None
self.phisics_engine_2 = None
self.platform = None
self.camera = None
self.coin_list = None
self.ground = None
self.finish = None
self.box = None
self.sound_coin = None
self.sound = arcade.Sound(':resources:music/funkyrobot.mp3')
self.sound.play(0.001,1,0,1)
self.hurt_sound = arcade.Sound(':resources:sounds/hurt3.wav')
self.buttons = arcade.gui.UIBoxLayout()
self.manager.add(
arcade.gui.UIAnchorWidget(
anchor_x='center_x',
anchor_y='center_y',
child=self.buttons
)
)
def setup(self):
self.scene = arcade.Scene()
self.coin_list = arcade.SpriteList()
self.player_list = arcade.SpriteList()
self.player = arcade.AnimatedWalkingSprite()
for x in range(0, 1280, 64):
self.box = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", scale= 0.5) #Вот эта стена из спрайтов
self.box.center_x = 30
self.box.center_y = x
self.scene.add_sprite("Box", self.box)
for x in range(0, 1408, 128):
self.ground = arcade.Sprite(":resources:images/tiles/grassMid.png", TILE_SCALING)
self.ground.center_x = x
self.ground.center_y = 32
self.scene.add_sprite("Ground", self.ground)
for x in range(1375, 2500, 64):
self.ground = arcade.Sprite(":resources:images/tiles/lavaTop_high.png", scale= 0.5)
self.ground.center_x = x
self.ground.center_y = 32
self.scene.add_sprite("Ground", self.ground)
for x in range(2500, 3000, 128):
self.ground = arcade.Sprite(":resources:images/tiles/grassMid.png", TILE_SCALING)
self.ground.center_x = x
self.ground.center_y = 32
self.scene.add_sprite("Ground", self.ground)
for x in range(0, 1280, 64):
self.box = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", scale= 0.5)
self.box.center_x = 2900
self.box.center_y = x
self.scene.add_sprite("Box", self.box)
self.player = Player()
self.player.bottom = self.ground.top
self.player.center_x = 150
self.phisics_engine = arcade.PhysicsEnginePlatformer(self.player, gravity_constant=GRAVITY,
walls=self.scene.get_sprite_list("Ground"))
# ПЛАТФОРМА
coordinate_list = [(630, 250), (720, 250),(780,250) ,(1000, 300),(1500,250),(1590,250),(1850,350),(1940,350),(2100,300),(2190,300)]
for coordinate in coordinate_list:
platform = arcade.Sprite(
":resources:images/tiles/grassHalf_mid.png", scale=0.7
)
platform.position = coordinate
self.scene.add_sprite("Ground", platform)
coordinate_list_2= [(280, 140), (715, 340), (1000, 390),]
for coordinate in coordinate_list_2:
platform = arcade.Sprite(
":resources:images/tiles/spikes.png", scale=0.7
)
platform.position = coordinate
self.scene.add_sprite("Ground", platform)
coordinate_coin = [(150, 300), (800, 350), (1000, 140),(1550,320),(1930,420),(2150,370)]
for coordinate in coordinate_coin:
self.coin = arcade.Sprite(':resources:images/items/coinGold.png', 0.7)
self.coin.position = coordinate
self.coin_list.append(self.coin)
self.camera = arcade.Camera(SCREEN_WIDTH, SCREEN_HEIGHT)
def on_draw(self):
self.clear()
self.scene.draw()
self.coin_list.draw()
self.camera.use()
self.player.draw()
def update(self, delta_time):
self.player.update_animation()
self.scene.update()
self.phisics_engine.update()
self.center_camera_to_player()
self.phisics_engine.update()
for coin in self.coin_list:
clam_coin = arcade.check_for_collision(self.player, coin)
if clam_coin:
self.sound_coin = arcade.Sound(':resources:sounds/coin5.wav')
self.sound_coin.play(10, 0, False, 1)
coin.kill()
def on_key_press(self, key: int, modifiers: int):
if key == arcade.key.SPACE or key == arcade.key.UP:
if self.phisics_engine.can_jump():
self.player.change_y = JUMP_SPEED_PLAYER
elif key == arcade.key.LEFT or key == arcade.key.A:
self.player.change_x = -SPEED_PLAYER
elif key == arcade.key.RIGHT or key == arcade.key.D:
self.player.change_x = SPEED_PLAYER
if key == arcade.key.ESCAPE:
pause_view = Pause(self)
self.window.show_view(pause_view)
def on_key_release(self, key: int, modifiers: int):
if key == arcade.key.UP or key == arcade.key.W:
self.player.change_y = 0
elif key == arcade.key.DOWN or key == arcade.key.S:
self.player.change_y = 0
elif key == arcade.key.RIGHT or key == arcade.key.D:
self.player.change_x = 0
elif key == arcade.key.LEFT or key == arcade.key.A:
self.player.change_x = 0
def center_camera_to_player(self):
screen_center_x = self.player.center_x - (self.camera.viewport_width / 2)
screen_center_y = self.player.center_y - (
self.camera.viewport_height / 2
)
if screen_center_x < 0:
screen_center_x = 0
if screen_center_y < 0:
screen_center_y = 0
player_centered = screen_center_x, screen_center_y
self.camera.move_to(player_centered)
class Player(arcade.Sprite):
def __init__(self):
super().__init__()
self.person_face_direction = RIGHT_FACING
self.cur_texture = 0
self.idle = True
main_path = ':resources:images/animated_characters/male_adventurer/'
self.idle_textures = []
for i in range(1, 6):
texture = self.load_texture_pair(f'{main_path}maleAdventurer_idle.png')
self.idle_textures.append(texture)
self.run_textures = []
for i in range(1, 7):
texture = self.load_texture_pair(f'{main_path}maleAdventurer_walk{i}.png')
self.run_textures.append(texture)
self.texture = self.idle_textures[0][self.person_face_direction]
@staticmethod
def load_texture_pair(filename):
return [
arcade.load_texture(filename),
arcade.load_texture(filename, flipped_horizontally=True),
]
def update_animation(self, delta_time: float = 1 / 60):
if self.change_x < 0 and self.person_face_direction == RIGHT_FACING:
self.person_face_direction = LEFT_FACING
elif self.change_x > 0 and self.person_face_direction == LEFT_FACING:
self.person_face_direction = RIGHT_FACING
if self.change_x == 0:
self.cur_texture += .5
if self.cur_texture % 2 == 0:
self.texture = self.idle_textures[0][self.person_face_direction]
if self.cur_texture >= 5:
self.cur_texture = 0
self.texture = self.idle_textures[int(self.cur_texture)][
self.person_face_direction
]
if not self.idle:
self.cur_texture = int(self.cur_texture)
self.cur_texture += 1
if self.cur_texture >= 6:
self.cur_texture = 0
self.texture = self.run_textures[self.cur_texture][
self.person_face_direction
]
class StartMenu(arcade.View):
def on_show_view(self):
self.window.center_window()
arcade.set_background_color(arcade.color.WINE)
arcade.set_viewport(0, self.window.width, 0, self.window.height)
def on_draw(self):
arcade.start_render()
arcade.draw_text("Мой Платформер", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + 50,
arcade.color.BLACK, font_size=30, anchor_x="center")
arcade.draw_text("Нажмите чтобы начать", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - 50,
arcade.color.BLACK, font_size=20, anchor_x="center")
arcade.draw_text("Нажмите Q Для Выхода", 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()
class Pause(arcade.View):
def __init__(self, game):
super().__init__()
self.game = game
def on_show_view(self):
arcade.set_background_color(arcade.color.ELECTRIC_INDIGO)
def on_draw(self):
self.clear()
arcade.draw_text('ПАУЗА', SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + 100,
arcade.color.WHITE, font_size=50, anchor_x='center')
arcade.draw_text('НАЖМИТЕ Enter, ДЛЯ НОВОЙ ИГРЫ', SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2,
arcade.color.LIGHT_RED_OCHRE, font_size=20, anchor_x='center')
arcade.draw_text('НАЖМИТЕ Esc, ДЛЯ ПРОДОЛЖЕНИЯ', SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - 100,
arcade.color.LIGHT_RED_OCHRE, font_size=20, anchor_x='center')
def on_key_press(self, key: int, modifiers: int):
if key == arcade.key.ESCAPE:
self.window.show_view(self.game)
elif key == arcade.key.ENTER:
self.game.setup()
self.window.show_view(self.game)
class GameOver(arcade.View):
def __init__(self, game):
super().__init__()
self.game = game
self.texture = arcade.load_texture('game_over.png')
arcade.set_viewport(0, SCREEN_WIDTH - 1, 0, SCREEN_HEIGHT - 1)
def on_draw(self):
self.clear()
arcade.draw_rectangle_filled(50, 50, 500, 2000, arcade.color.DARK_SLATE_BLUE)
self.texture.draw_sized(SCREEN_WIDTH / 2 + 50, SCREEN_HEIGHT / 2,
SCREEN_WIDTH - 100, SCREEN_HEIGHT)
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int):
game = MyGame()
game.setup()
self.window.show_view(game)
if __name__ == "__main__":
window = arcade.Window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
start_view = StartMenu()
window.show_view(start_view)
arcade.run() |