第9步-使用切片地图编辑器#

../../_images/use_tileset.png

创建映射文件#

对于这一部分,我们将使用一个地图编辑器来构建地图,然后将其加载到 Mapfile 中,而不是使用特定点通过代码放置瓷砖。

要开始,请下载并安装 Tiled Map Editor 。(考虑一下捐赠,因为这是一个免费提供的很棒的项目。)

在https://doc.mapeditor.org/,上已经有很好的文档可用,因此在本教程中,我们假设您已经熟悉如何使用Tabled创建地图。如果不是,您可以查看平铺文档并返回到此处。

从教程中的这一点开始,每一章都将使用平铺地图。如果您还不想创建自己的,Arcade附带了几个示例 resources 文件夹,这是这些示例的来源,所以如果您不想创建自己的地图,也不必创建自己的地图。

我们将从基础知识开始 map.json Arcade提供的文件。您可以在平铺文件中打开此文件并查看其设置方式,但我们现在将介绍一些基本知识。您可以用“JSON”或“TMX”格式保存文件。

在这张地图上,我们有两个层,分别名为“平台”和“硬币”。站台层是玩家使用物理引擎将会碰撞到的所有积木,而硬币层是玩家可以捡到的所有硬币,以增加他们的得分。这张地图差不多就是这样了。

../../_images/layer_names.png

这些层将由Arcade自动加载为SpriteList,我们可以使用场景访问和绘制这些列表。让我们看看如何在地图中加载,首先我们将创建一个 tile_map 我们的init函数中的对象:

加载地图-创建对象#
        # Our TileMap Object
        self.tile_map = None

然后,我们将在设置函数中进行实际加载,我们的新设置函数将如下所示:

加载地图-设置地图#
 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
    def setup(self):
        """Set up the game here. Call this function to restart the game."""

        # Set up the Cameras
        self.camera = arcade.Camera(self.width, self.height)
        self.gui_camera = arcade.Camera(self.width, self.height)

        # Name of map file to load
        map_name = ":resources:tiled_maps/map.json"

        # Layer specific options are defined based on Layer names in a dictionary
        # Doing this will make the SpriteList for the platforms layer
        # use spatial hashing for detection.
        layer_options = {
            "Platforms": {
                "use_spatial_hash": True,
            },
        }

        # Read in the tiled map
        self.tile_map = arcade.load_tilemap(map_name, TILE_SCALING, layer_options)

        # Initialize Scene with our TileMap, this will automatically add all layers
        # from the map as SpriteLists in the scene in the proper order.
        self.scene = arcade.Scene.from_tilemap(self.tile_map)

        # Keep track of the score
        self.score = 0

        # Set up the player, specifically placing it at these coordinates.
        image_source = ":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png"
        self.player_sprite = arcade.Sprite(image_source, CHARACTER_SCALING)
        self.player_sprite.center_x = 128
        self.player_sprite.center_y = 128
        self.scene.add_sprite("Player", self.player_sprite)

        # --- Other stuff
        # Set the background color
        if self.tile_map.background_color:
            arcade.set_background_color(self.tile_map.background_color)

        # Create the 'physics engine'
        self.physics_engine = arcade.PhysicsEnginePlatformer(
            self.player_sprite, gravity_constant=GRAVITY, walls=self.scene["Platforms"]
        )

这几乎就是加载到Tilemap中所需的所有操作,我们从它创建了一个场景,并可以像使用它一样使用它,直到现在。但让我们来看看这个设置功能,看看所有的更新。

在第一部分中,我们定义了要加载的映射文件的名称,这个名称非常简单。

接下来,我们有一个 layer_options 变量。这是一本词典,可以让您为地图中的特定层指定特殊选项。在本例中,我们只是将空间散列添加到“Platels”层,但在这里我们还可以做其他一些事情。

可以为层设置的可用选项包括:

  • use_spatial_hash -使用空间散列生成层的SpriteList

  • scaling -设置精灵的逐层缩放

  • hit_box_algorithm -更改使用此SpriteList进行冲突检测时使用的命中框算法

  • hit_box_detail -更改使用此SpriteList进行冲突检测时使用的命中框详细信息

然后,我们实际上使用 arcade.load_tilemap 功能。这将向我们返回 arcade.TileMap 班级。现在,我们实际上不需要与这个物体进行太多的交互,但稍后我们会做一些更高级的事情,比如在地图编辑器中设置敌人的产卵点和移动路径。

最后,我们使用一种新的方法来创建场景, arcade.Scene.from_tilemap 功能。这使您可以指定TileMap对象,并将自动构建包含地图中所有层的场景,并按正确的渲染顺序排列。然后,您可以处理场景,就像我们在此之前所做的一样。

我们更改的最后一个小部分是当我们创建物理引擎时,我们现在必须使用“Platures”作为精灵列表的名称,因为这是 Mapfile 中我们的层的名称。

仅此而已!你现在应该有一个完整的游戏加载从一个 Mapfile 创建的平铺。

我们将在接下来的章节中使用平铺的一些内容包括:

  • 你遇到的平台(或者你可以把它们想象成墙)

  • 移动平台

  • 要捡起的硬币或物品

  • 不与之交互但显示在播放器后面的背景对象

  • 不与之交互但出现在玩家面前的前景对象

  • Insta-死亡街区和区域(如熔岩)

  • 梯子

  • 敌方产卵阵地

  • 敌军移动路径

源代码#

加载地图#
  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
"""
Platformer Game
"""
import arcade

# Constants
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 650
SCREEN_TITLE = "Platformer"

# Constants used to scale our sprites from their original size
CHARACTER_SCALING = 1
TILE_SCALING = 0.5
COIN_SCALING = 0.5

# Movement speed of player, in pixels per frame
PLAYER_MOVEMENT_SPEED = 10
GRAVITY = 1
PLAYER_JUMP_SPEED = 20


class MyGame(arcade.Window):
    """
    Main application class.
    """

    def __init__(self):

        # Call the parent class and set up the window
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)

        # Our TileMap Object
        self.tile_map = None

        # Our Scene Object
        self.scene = None

        # Separate variable that holds the player sprite
        self.player_sprite = None

        # Our physics engine
        self.physics_engine = None

        # A Camera that can be used for scrolling the screen
        self.camera = None

        # A Camera that can be used to draw GUI elements
        self.gui_camera = None

        # Keep track of the score
        self.score = 0

        # Load sounds
        self.collect_coin_sound = arcade.load_sound(":resources:sounds/coin1.wav")
        self.jump_sound = arcade.load_sound(":resources:sounds/jump1.wav")

        arcade.set_background_color(arcade.csscolor.CORNFLOWER_BLUE)

    def setup(self):
        """Set up the game here. Call this function to restart the game."""

        # Set up the Cameras
        self.camera = arcade.Camera(self.width, self.height)
        self.gui_camera = arcade.Camera(self.width, self.height)

        # Name of map file to load
        map_name = ":resources:tiled_maps/map.json"

        # Layer specific options are defined based on Layer names in a dictionary
        # Doing this will make the SpriteList for the platforms layer
        # use spatial hashing for detection.
        layer_options = {
            "Platforms": {
                "use_spatial_hash": True,
            },
        }

        # Read in the tiled map
        self.tile_map = arcade.load_tilemap(map_name, TILE_SCALING, layer_options)

        # Initialize Scene with our TileMap, this will automatically add all layers
        # from the map as SpriteLists in the scene in the proper order.
        self.scene = arcade.Scene.from_tilemap(self.tile_map)

        # Keep track of the score
        self.score = 0

        # Set up the player, specifically placing it at these coordinates.
        image_source = ":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png"
        self.player_sprite = arcade.Sprite(image_source, CHARACTER_SCALING)
        self.player_sprite.center_x = 128
        self.player_sprite.center_y = 128
        self.scene.add_sprite("Player", self.player_sprite)

        # --- Other stuff
        # Set the background color
        if self.tile_map.background_color:
            arcade.set_background_color(self.tile_map.background_color)

        # Create the 'physics engine'
        self.physics_engine = arcade.PhysicsEnginePlatformer(
            self.player_sprite, gravity_constant=GRAVITY, walls=self.scene["Platforms"]
        )

    def on_draw(self):
        """Render the screen."""

        # Clear the screen to the background color
        self.clear()

        # Activate the game camera
        self.camera.use()

        # Draw our Scene
        self.scene.draw()

        # Activate the GUI camera before drawing GUI elements
        self.gui_camera.use()

        # Draw our score on the screen, scrolling it with the viewport
        score_text = f"Score: {self.score}"
        arcade.draw_text(
            score_text,
            10,
            10,
            arcade.csscolor.WHITE,
            18,
        )

    def on_key_press(self, key, modifiers):
        """Called whenever a key is pressed."""

        if key == arcade.key.UP or key == arcade.key.W:
            if self.physics_engine.can_jump():
                self.player_sprite.change_y = PLAYER_JUMP_SPEED
                arcade.play_sound(self.jump_sound)
        elif key == arcade.key.LEFT or key == arcade.key.A:
            self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
        elif key == arcade.key.RIGHT or key == arcade.key.D:
            self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED

    def on_key_release(self, key, modifiers):
        """Called when the user releases a key."""

        if key == arcade.key.LEFT or key == arcade.key.A:
            self.player_sprite.change_x = 0
        elif key == arcade.key.RIGHT or key == arcade.key.D:
            self.player_sprite.change_x = 0

    def center_camera_to_player(self):
        screen_center_x = self.player_sprite.center_x - (self.camera.viewport_width / 2)
        screen_center_y = self.player_sprite.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)

    def on_update(self, delta_time):
        """Movement and game logic"""

        # Move the player with the physics engine
        self.physics_engine.update()

        # See if we hit any coins
        coin_hit_list = arcade.check_for_collision_with_list(
            self.player_sprite, self.scene["Coins"]
        )

        # Loop through each coin we hit (if any) and remove it
        for coin in coin_hit_list:
            # Remove the coin
            coin.remove_from_sprite_lists()
            # Play a sound
            arcade.play_sound(self.collect_coin_sound)
            # Add one to the score
            self.score += 1

        # Position the camera
        self.center_camera_to_player()


def main():
    """Main function"""
    window = MyGame()
    window.setup()
    arcade.run()


if __name__ == "__main__":
    main()