"""
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
# Movement speed of player, in pixels per frame
PLAYER_MOVEMENT_SPEED = 5
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 Scene Object
self.scene = None
# Separate variable that holds the player sprite
self.player_sprite = None
# Our physics engine
self.physics_engine = None
arcade.set_background_color(arcade.csscolor.CORNFLOWER_BLUE)
def setup(self):
"""Set up the game here. Call this function to restart the game."""
# Initialize Scene
self.scene = arcade.Scene()
# 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 = 64
self.player_sprite.center_y = 128
self.scene.add_sprite("Player", self.player_sprite)
# Create the ground
# This shows using a loop to place multiple sprites horizontally
for x in range(0, 1250, 64):
wall = arcade.Sprite(":resources:images/tiles/grassMid.png", TILE_SCALING)
wall.center_x = x
wall.center_y = 32
self.scene.add_sprite("Walls", wall)
# Put some crates on the ground
# This shows using a coordinate list to place sprites
coordinate_list = [[512, 96], [256, 96], [768, 96]]
for coordinate in coordinate_list:
# Add a crate on the ground
wall = arcade.Sprite(
":resources:images/tiles/boxCrate_double.png", TILE_SCALING
)
wall.position = coordinate
self.scene.add_sprite("Walls", wall)
# Create the 'physics engine'
# 实例化一个物理引擎,将player和walls加入其中
self.physics_engine = arcade.PhysicsEngineSimple(
self.player_sprite, self.scene.get_sprite_list("Walls")
)
def on_draw(self):
"""Render the screen."""
# Clear the screen to the background color
self.clear()
# Draw our Scene
self.scene.draw()
# 添加控制事件
def on_key_press(self, key, modifiers):
"""Called whenever a key is pressed."""
if key == arcade.key.UP or key == arcade.key.W:
self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED
elif key == arcade.key.DOWN or key == arcade.key.S:
self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED
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.UP or key == arcade.key.W:
self.player_sprite.change_y = 0
elif key == arcade.key.DOWN or key == arcade.key.S:
self.player_sprite.change_y = 0
elif 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 on_update(self, delta_time):
"""Movement and game logic"""
# Move the player with the physics engine
self.physics_engine.update()
def main():
"""Main function"""
window = MyGame()
window.setup()
arcade.run()
if __name__ == "__main__":
main()
这是一个平台游戏的主程序。在这个游戏中,玩家可以使用键盘控制角色的移动。
首先,我们导入了arcade模块来构建游戏。
接下来,我们定义了一些常量,包括屏幕的宽度和高度以及游戏的标题。
然后,我们定义了一个MyGame类,继承自arcade.Window类。这个类是我们的主应用程序类,负责设置游戏并处理用户输入。
在__init__方法中,我们首先调用父类的__init__方法来设置窗口的大小和标题。然后,我们初始化场景对象和玩家精灵对象,并设置背景颜色。
在setup方法中,我们设置了游戏的初始状态。我们创建了一个场景对象,并在场景中添加了玩家精灵和地面精灵。然后,我们创建了一个物理引擎,并将玩家精灵和地面精灵添加到物理引擎中。
在on_draw方法中,我们清空屏幕,并绘制场景。
on_key_press和on_key_release方法分别处理按键按下和释放的事件。根据按下或释放的键位,我们改变玩家精灵的速度,从而实现角色的移动。
on_update方法用于更新游戏逻辑。在这个方法中,我们使用物理引擎来移动玩家精灵。
最后,我们定义了一个main函数,在其中创建了MyGame实例,并调用setup方法。然后,我们调用arcade.run()来启动游戏循环。
如果在执行这个文件时,将会运行这个main函数。