平台模板#

快速开始创建您自己的平台!

平台模板屏幕截图

关于此模板#

这是一个让你尽快开始编写侧滚平台的模板。我建议采取以下步骤:

  1. 创建一个文件夹来存放代码。将此示例复制到该文件夹,并将其命名为 main.py

  2. 确保示例代码运行良好。

  3. 将要使用的磁贴图像复制到包含代码的文件夹的子目录中。

  4. 创建一个非常简单的平铺地图使用 Tiled Map Editor 。我建议只创造一个地板,而不是其他的。有关如何创建切片地图的详细信息,请参阅 第9步-添加声音

  5. 将文件保存到与源代码相同的目录中。如果创建单独的平铺集,还要将其保存到与代码相同的目录中。

  6. 更新代码以加载您的文件而不是地图。

  7. 测试并确保它工作正常。

  8. 既然你确信一切正常,那就做你自己的平台吧!

警告

注意目录!

最常见的错误之一是将地图和切片集保存到与代码不同的目录中。或者在该文件夹中没有磁贴图像。如果所有内容都不在同一文件夹(或子文件夹)中,则很难在以后将其打包。

有关更详细的说明,请参阅教程 简单平台

源代码#

template_platformer.py#
  1"""
  2Platformer Template
  3
  4If Python and Arcade are installed, this example can be run from the command line with:
  5python -m arcade.examples.template_platformer
  6"""
  7import arcade
  8
  9# --- Constants
 10SCREEN_TITLE = "Platformer"
 11
 12SCREEN_WIDTH = 1000
 13SCREEN_HEIGHT = 650
 14
 15# Constants used to scale our sprites from their original size
 16CHARACTER_SCALING = 1
 17TILE_SCALING = 0.5
 18COIN_SCALING = 0.5
 19SPRITE_PIXEL_SIZE = 128
 20GRID_PIXEL_SIZE = SPRITE_PIXEL_SIZE * TILE_SCALING
 21
 22# Movement speed of player, in pixels per frame
 23PLAYER_MOVEMENT_SPEED = 10
 24GRAVITY = 1
 25PLAYER_JUMP_SPEED = 20
 26
 27
 28class MyGame(arcade.Window):
 29    """
 30    Main application class.
 31    """
 32
 33    def __init__(self):
 34
 35        # Call the parent class and set up the window
 36        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT,
 37                         SCREEN_TITLE, resizable=True)
 38
 39        # Our TileMap Object
 40        self.tile_map = None
 41
 42        # Our Scene Object
 43        self.scene = None
 44
 45        # Separate variable that holds the player sprite
 46        self.player_sprite = None
 47
 48        # Our physics engine
 49        self.physics_engine = None
 50
 51        # A Camera that can be used for scrolling the screen
 52        self.camera_sprites = None
 53
 54        # A non-scrolling camera that can be used to draw GUI elements
 55        self.camera_gui = None
 56
 57        # Keep track of the score
 58        self.score = 0
 59
 60        # What key is pressed down?
 61        self.left_key_down = False
 62        self.right_key_down = False
 63
 64    def setup(self):
 65        """Set up the game here. Call this function to restart the game."""
 66
 67        # Setup the Cameras
 68        self.camera_sprites = arcade.camera.Camera2D()
 69        self.camera_gui = arcade.camera.Camera2D()
 70
 71        # Name of map file to load
 72        map_name = ":resources:tiled_maps/map.json"
 73
 74        # Layer specific options are defined based on Layer names in a dictionary
 75        # Doing this will make the SpriteList for the platforms layer
 76        # use spatial hashing for detection.
 77        layer_options = {
 78            "Platforms": {
 79                "use_spatial_hash": True,
 80            },
 81        }
 82
 83        # Read in the tiled map
 84        self.tile_map = arcade.load_tilemap(map_name, TILE_SCALING, layer_options)
 85
 86        # Initialize Scene with our TileMap, this will automatically add all layers
 87        # from the map as SpriteLists in the scene in the proper order.
 88        self.scene = arcade.Scene.from_tilemap(self.tile_map)
 89
 90        # Set the background color
 91        if self.tile_map.background_color:
 92            self.background_color = self.tile_map.background_color
 93
 94        # Keep track of the score
 95        self.score = 0
 96
 97        # Set up the player, specifically placing it at these coordinates.
 98        src = ":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png"
 99        self.player_sprite = arcade.Sprite(src, scale=CHARACTER_SCALING)
100        self.player_sprite.center_x = 128
101        self.player_sprite.center_y = 128
102        self.scene.add_sprite("Player", self.player_sprite)
103
104        # --- Other stuff
105        # Create the 'physics engine'
106        self.physics_engine = arcade.PhysicsEnginePlatformer(
107            self.player_sprite, gravity_constant=GRAVITY, walls=self.scene["Platforms"]
108        )
109
110    def on_draw(self):
111        """Render the screen."""
112
113        # Clear the screen to the background color
114        self.clear()
115
116        # Activate the game camera
117        self.camera_sprites.use()
118
119        # Draw our Scene
120        # Note, if you a want pixelated look, add pixelated=True to the parameters
121        self.scene.draw()
122
123        # Activate the GUI camera before drawing GUI elements
124        self.camera_gui.use()
125
126        # Draw our score on the screen, scrolling it with the viewport
127        score_text = f"Score: {self.score}"
128        arcade.draw_text(score_text,
129                         x=10,
130                         y=10,
131                         color=arcade.csscolor.WHITE,
132                         font_size=18)
133
134    def update_player_speed(self):
135
136        # Calculate speed based on the keys pressed
137        self.player_sprite.change_x = 0
138
139        if self.left_key_down and not self.right_key_down:
140            self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
141        elif self.right_key_down and not self.left_key_down:
142            self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
143
144    def on_key_press(self, key, modifiers):
145        """Called whenever a key is pressed."""
146
147        # Jump
148        if key == arcade.key.UP or key == arcade.key.W:
149            if self.physics_engine.can_jump():
150                self.player_sprite.change_y = PLAYER_JUMP_SPEED
151
152        # Left
153        elif key == arcade.key.LEFT or key == arcade.key.A:
154            self.left_key_down = True
155            self.update_player_speed()
156
157        # Right
158        elif key == arcade.key.RIGHT or key == arcade.key.D:
159            self.right_key_down = True
160            self.update_player_speed()
161
162    def on_key_release(self, key, modifiers):
163        """Called when the user releases a key."""
164        if key == arcade.key.LEFT or key == arcade.key.A:
165            self.left_key_down = False
166            self.update_player_speed()
167        elif key == arcade.key.RIGHT or key == arcade.key.D:
168            self.right_key_down = False
169            self.update_player_speed()
170
171    def center_camera_to_player(self):
172        # Find where player is, then calculate lower left corner from that
173        screen_center_x = self.player_sprite.center_x
174        screen_center_y = self.player_sprite.center_y
175
176        # Set some limits on how far we scroll
177        if screen_center_x - self.width/2 < 0:
178            screen_center_x = self.width/2
179        if screen_center_y - self.height/2 < 0:
180            screen_center_y = self.height/2
181
182        # Here's our center, move to it
183        player_centered = screen_center_x, screen_center_y
184        self.camera_sprites.position = arcade.math.lerp_2d(self.camera_sprites.position, player_centered, 0.1)
185
186    def on_update(self, delta_time):
187        """Movement and game logic"""
188
189        # Move the player with the physics engine
190        self.physics_engine.update()
191
192        # See if we hit any coins
193        coin_hit_list = arcade.check_for_collision_with_list(
194            self.player_sprite, self.scene["Coins"]
195        )
196
197        # Loop through each coin we hit (if any) and remove it
198        for coin in coin_hit_list:
199            # Remove the coin
200            coin.remove_from_sprite_lists()
201            # Add one to the score
202            self.score += 1
203
204        # Position the camera
205        self.center_camera_to_player()
206
207    def on_resize(self, width: int, height: int):
208        """ Resize window """
209        super().on_resize(width, height)
210        self.camera_sprites.match_screen(and_projection=True)
211        self.camera_gui.match_screen(and_projection=True)
212
213
214def main():
215    """Main function"""
216    window = MyGame()
217    window.setup()
218    arcade.run()
219
220
221if __name__ == "__main__":
222    main()