Arcade街机平铺地图多关卡示例

以下是部分代码预览:

"""
Arcade街机平铺地图多关卡示例
本程序加载tmx地图,生成关卡,根据角色到达地图最右边界来决定进入下一关的。

"""

import arcade
import os
import time

SPRITE_SCALING = 0.5

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Python平铺地图多关卡示例_by lixingqiu"
SPRITE_PIXEL_SIZE = 128
GRID_PIXEL_SIZE = (SPRITE_PIXEL_SIZE * SPRITE_SCALING)

# 视口边距
VIEWPORT_MARGIN_TOP = 60
VIEWPORT_MARGIN_BOTTOM = 60
VIEWPORT_RIGHT_MARGIN = 270
VIEWPORT_LEFT_MARGIN = 270

# Physics
MOVEMENT_SPEED = 5
JUMP_SPEED = 23
GRAVITY = 1.1


class MyGame(arcade.Window):
    """ 继承自窗口的游戏类. """

    def __init__(self):
        """
        初始化方法
        """
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
        
        # 定义角色列表
        self.wall_list = None
        self.player_list = None
        self.coin_list = None

        # 定义玩家相关变量
        self.score = 0
        self.player_sprite = None

        self.physics_engine = None
        self.view_left = 0
        self.view_bottom = 0
        self.game_over = False
        self.last_time = None       # 用来算fps的上次时间变量
        self.frame_count = 0        # 帧计数器
        self.fps_message = None     # fps字符串

        self.level = 1
        self.max_level = 2

    def setup(self):
        """ 设置与初始化变量的值. """

        # 角色列表初始化
        self.player_list = arcade.SpriteList()
        self.coin_list = arcade.SpriteList()

        # 玩家操作的角色实例化,这里是一只昆虫
        self.player_sprite = arcade.Sprite("images/lixingqiu.png", SPRITE_SCALING)

        # 给昆虫设定坐标
        self.player_sprite.center_x = 64
        self.player_sprite.center_y = 64
        self.player_list.append(self.player_sprite)

        self.load_level(self.level) # 加载第一关

        self.game_over = False

    def load_level(self, level):
        # 读取第一关的地图
        my_map = arcade.read_tiled_map(f"level_{level}.tmx", SPRITE_SCALING)

        # 其它情况,如有背景颜色属性
        if my_map.backgroundcolor:
            arcade.set_background_color(my_map.backgroundcolor)

        # 设置视口以便移动这个矩形
        self.view_left = 0
        self.view_bottom = 0

    def on_draw(self):
        """
        渲染屏幕
        """

        self.frame_count += 1      # 帧计数器

        # 开始渲染屏幕
        arcade.start_render()

        # 画所有的角色
        self.player_list.draw()
        self.wall_list.draw()
        self.coin_list.draw()


    def on_key_press(self, key, modifiers):
        """
        当按键时调用此方法
        """
        if key == arcade.key.UP:
            if self.physics_engine.can_jump():
                self.player_sprite.change_y = JUMP_SPEED
        elif key == arcade.key.LEFT:
            self.player_sprite.change_x = -MOVEMENT_SPEED
        elif key == arcade.key.RIGHT:
            self.player_sprite.change_x = MOVEMENT_SPEED

    def on_key_release(self, key, modifiers):
        """
        当松开键时调用此方法
        """
        if key == arcade.key.LEFT or key == arcade.key.RIGHT:
            self.player_sprite.change_x = 0

    def update(self, delta_time):
        """ 移动与游戏的逻辑 """


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


if __name__ == "__main__":
    main()

如需要查看完整代码,请扫码付款:

VIP免费查看

已有0人支付

本站所有作品,教程等皆为原创,盗版必究。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。亦可直接向微信号scratch8付款购买。QQ群免费教学群:225792826 (提供真实姓名可入群)
李兴球的博客_Python创意编程技术前沿 » Arcade街机平铺地图多关卡示例

学本领,探索更大的世界!

李兴球博客 风火轮编程主页