import arcade
# Константы игры
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
GRAVITY = 0.3
PLAYER_JUMP_SPEED = 10
PLAYER_MOVE_SPEED = 5
class GameWindow(arcade.Window):
"""Основное окно игры"""
def __init__(self):
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, "Мой платформер")
self.set_location(400, 200)
self.player = None
self.obstacles = None
self.ground_list = None
self.victory_text = None
self.game_over_text = None
self.physics_engine = None
self.is_game_over = False
arcade.set_background_color(arcade.color.LIGHT_BLUE)
def setup(self):
"""Настройка игры"""
self.player = arcade.Sprite("images/player.png", 0.5)
self.player.center_x = SCREEN_WIDTH // 2
self.player.center_y = SCREEN_HEIGHT // 2
self.obstacles = arcade.SpriteList()
self.ground_list = arcade.SpriteList()
ground_texture = arcade.make_soft_square_texture(SCREEN_WIDTH, 100, arcade.color.DARK_GREEN)
ground_sprite = arcade.Sprite()
ground_sprite.center_x = SCREEN_WIDTH // 2
ground_sprite.center_y = 50
ground_sprite.width = SCREEN_WIDTH
ground_sprite.height = 100
ground_sprite.texture = ground_texture
self.ground_list.append(ground_sprite)
self.victory_text = arcade.create_text("Вы победили!", arcade.color.WHITE, 36)
self.game_over_text = arcade.create_text("Игра окончена", arcade.color.WHITE, 36)
self.physics_engine = arcade.PhysicsEnginePlatformer(self.player, self.ground_list, GRAVITY)
def on_draw(self):
"""Отрисовка игры"""
arcade.start_render()
if self.is_game_over:
arcade.render_text(self.game_over_text, SCREEN_WIDTH // 2 - 100, SCREEN_HEIGHT // 2)
else:
self.ground_list.draw()
self.player.draw()
self.obstacles.draw()
if len(self.obstacles) == 0:
arcade.render_text(self.victory_text, SCREEN_WIDTH // 2 - 100, SCREEN_HEIGHT // 2)
def on_update(self, delta_time):
"""Обновление игры"""
if self.is_game_over:
return
self.physics_engine.update()
if len(self.obstacles) == 0:
return
for obstacle in self.obstacles:
if self.player.collides_with_sprite(obstacle):
self.is_game_over = True
break
def on_key_press(self, key, modifiers):
"""Реакция на нажатие клавиш"""
if key == arcade.key.UP or key == arcade.key.W:
if self.physics_engine.can_jump():
self.player.change_y = PLAYER_JUMP_SPEED
elif key == arcade.key.LEFT or key == arcade.key.A:
self.player.change_x = -PLAYER_MOVE_SPEED
elif key == arcade.key.RIGHT or key == arcade.key.D:
self.player.change_x = PLAYER_MOVE_SPEED
def on_key_release(self, key, modifiers):
"""Реакция на отжатие клавиш"""
if key == arcade.key.LEFT or key == arcade.key.A or key == arcade.key.RIGHT or key == arcade.key.D:
self.player.change_x = 0
def add_obstacle(self, obstacle):
"""Добавление препятствия"""
self.obstacles.append(obstacle)
# Запуск игры
if __name__ == '__main__':
game_window = GameWindow()
game_window.setup()
arcade.run()