помогите с Python Arcade

jul

Новичок
Пользователь
Май 13, 2023
1
0
1
Здравствуйте, помогите пожалуйста, не получается добавить сбор монет в простой платформер на Arcade
Учусь совсем ничего, и большинство кода создано из конспекта, так что явно есть ошибки, но никак не могу разобраться
Само задание выглядело так:
Реализовать прототип игры платформер с использованием движка для продвинутой физики. В игре персонаж прыгает по платформам и собирает монеты. При столкновении с вражеским объектом, игра останавливается.
прошу о помощи)
import arcade import os SPRITE_SCALING = 0.5 SPRITE_NATIVE_SIZE = 128 SPRITE_SIZE = int(SPRITE_NATIVE_SIZE * SPRITE_SCALING) SCREEN_WIDTH = 1600 SCREEN_HEIGHT = 500 SCREEN_TITLE = "тип платформер" VIEWPORT_MARGIN = 40 RIGHT_MARGIN = 150 COIN_SCALING = 0.5 MOVEMENT_SPEED = 5 JUMP_SPEED = 14 GRAVITY = 0.5 class MyGame(arcade.Window): def __init__(self): super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) file_path = os.path.dirname(os.path.abspath(__file__)) os.chdir(file_path) self.wall_list = None self.enemy_list = None self.player_list = None self.player_sprite = None self.physics_engine = None self.view_left = 0 self.view_bottom = 0 self.game_over = False self.collect_coin_sound = arcade.load_sound(":resources:sounds/coin1.wav") self.jump_sound = arcade.load_sound(":resources:sounds/jump1.wav") def setup(self): self.wall_list = arcade.SpriteList() self.enemy_list = arcade.SpriteList() self.player_list = arcade.SpriteList() for x in range(0, SCREEN_WIDTH, SPRITE_SIZE): wall = arcade.Sprite(":resources:images/tiles/grassMid.png", SPRITE_SCALING) wall.bottom = 0 wall.left = x self.wall_list.append(wall) for x in range(SPRITE_SIZE , SPRITE_SIZE * 2, SPRITE_SIZE): wall = arcade.Sprite(":resources:images/items/flagRed2.png", SPRITE_SCALING) wall.bottom = SPRITE_SIZE * 1 wall.left = x * 18 self.wall_list.append(wall) for x in range(SPRITE_SIZE * 3, SPRITE_SIZE * 10, SPRITE_SIZE): wall = arcade.Sprite(":resources:images/tiles/stoneCenter_rounded.png", SPRITE_SCALING) wall.bottom = SPRITE_SIZE * 4 wall.left = x self.wall_list.append(wall) for x in range(0, SCREEN_WIDTH, SPRITE_SIZE * 5): wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", SPRITE_SCALING) wall.bottom = SPRITE_SIZE wall.left = x self.wall_list.append(wall) enemy = arcade.Sprite(":resources:images/enemies/slimeBlue_move.png", SPRITE_SCALING) enemy.bottom = SPRITE_SIZE enemy.left = SPRITE_SIZE * 11 enemy.change_x = 1 self.enemy_list.append(enemy) enemy = arcade.Sprite(":resources:images/enemies/mouse.png", SPRITE_SCALING) enemy.bottom = SPRITE_SIZE * 5 enemy.left = SPRITE_SIZE * 4 enemy.boundary_right = SPRITE_SIZE * 10 enemy.boundary_left = SPRITE_SIZE * 4 enemy.change_x = 2 self.enemy_list.append(enemy) self.player_sprite = arcade.Sprite(":resources:images/animated_characters/robot/robot_idle.png", SPRITE_SCALING) self.player_list.append(self.player_sprite) self.player_sprite.center_x = 50 self.player_sprite.center_y = 270 self.physics_engine = arcade.PhysicsEnginePlatformer(self.player_sprite, self.wall_list, gravity_constant=GRAVITY) arcade.set_background_color(arcade.color.DIAMOND) for x in range(1, 1, 2): coin = arcade.Sprite(":resources:images/items/coinGold.png", COIN_SCALING) coin.center_x = x coin.center_y = 96 def on_draw(self): self.clear() self.player_list.draw() self.wall_list.draw() self.enemy_list.draw() def on_key_press(self, key, modifiers): if key == arcade.key.W: if self.physics_engine.can_jump(): self.player_sprite.change_y = JUMP_SPEED elif key == arcade.key.A: self.player_sprite.change_x = -MOVEMENT_SPEED elif key == arcade.key.D: self.player_sprite.change_x = MOVEMENT_SPEED def on_key_release(self, key, modifiers): if key == arcade.key.A or key == arcade.key.D: self.player_sprite.change_x = 0 def on_update(self, delta_time): if not self.game_over: self.enemy_list.update() for enemy in self.enemy_list: if len(arcade.check_for_collision_with_list(enemy, self.wall_list)) > 0: enemy.change_x *= -1 elif enemy.boundary_left is not None and enemy.left < enemy.boundary_left: enemy.change_x *= -1 elif enemy.boundary_right is not None and enemy.right > enemy.boundary_right: enemy.change_x *= -1 self.physics_engine.update() if len(arcade.check_for_collision_with_list(self.player_sprite, self.enemy_list)) > 0: self.game_over = True coin_hit_list = arcade.check_for_collision_with_list( self.player_sprite, self.scene["Coins"] ) for coin in coin_hit_list: coin.remove_from_sprite_lists() arcade.play_sound(self.collect_coin_sound) def main(): window = MyGame() window.setup() arcade.run() if __name__ == "__main__": main()
 
Последнее редактирование:

Форум IT Специалистов