双人游戏Arcade街机模块让角色上下斜坡示例

以下是部分代码预览:

"""
双人游戏Arcade街机模块让角色上下斜坡示例。
本程序加载csv地图,根据数字映射加载相应的图块,生成地图。
 
"""
import arcade
import os

SPRITE_SCALING = 0.5

SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600
SCREEN_TITLE = "双人游戏让角色上下斜坡示例:lixingqiu译"
SPRITE_PIXEL_SIZE = 128
GRID_PIXEL_SIZE = (SPRITE_PIXEL_SIZE * SPRITE_SCALING)

# 视口边距
VIEWPORT_MARGIN = 40
RIGHT_MARGIN = 150

# 物理参数
MOVEMENT_SPEED = 5
JUMP_SPEED = 14
GRAVITY = 0.5


def get_map():
    """加载以逗号隔开的csv地图,它其实只是一个地图块的二维映射"""
    map_file = open("map_with_ramps_2.csv")
    map_array = []
    for line in map_file:
        line = line.strip()
        map_row = line.split(",")
        for index, item in enumerate(map_row):
            map_row[index] = int(item)
        map_array.append(map_row)
    return map_array


class MyGame(arcade.Window):
    """ 主应用程序类,继承自Arcade的窗口类. """

    def __init__(self, width, height, title):
        """
        初始化方法,参数为宽度,高度,标题
        """

        super().__init__(width, height, title)

        # 设置工作目录
        file_path = os.path.dirname(os.path.abspath(__file__))
        os.chdir(file_path)

        # 角色列表定义
        self.all_sprites_list = None
        self.coin_list = None
        self.player_list = None

        # 玩家定义
        self.player_sprite = None
        self.wall_list = None
        self.physics_engine = None
        self.physics_engine2 = None
        self.view_left = 0
        self.view_bottom = 0
        self.game_over = False

    def start_new_game(self):
        """ Set up the game and initialize the variables. """

        # 实例化角色列表
        self.all_sprites_list = arcade.SpriteList()
        self.wall_list = arcade.SpriteList()
        self.player_list = arcade.SpriteList()

        # 实例化玩家1
        self.player_sprite = arcade.Sprite("images/character.png",
                                           SPRITE_SCALING)
        self.player_sprite.center_x = 64
        self.player_sprite.center_y = 270
        self.player_list.append(self.player_sprite)
        self.all_sprites_list.append(self.player_sprite)

        
        # 实例化玩家2
        self.player_sprite2 = arcade.Sprite("images/character.png",
                                           SPRITE_SCALING)
        self.player_sprite2.center_x = 128
        self.player_sprite2.center_y = 270
        self.player_list.append(self.player_sprite2)
        self.all_sprites_list.append(self.player_sprite2)
        

        map_array = get_map()

        # 设置背景颜色为亚马逊深绿色
        arcade.set_background_color(arcade.color.AMAZON)

        # 设置初始的视口边界
        # 这些值就是我们要滚到的地方
        self.view_left = 0
        self.view_bottom = 0

        self.game_over = False

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

        # 在绘画之前此渲染命令要先执行
        arcade.start_render()

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


    def on_key_press(self, key, modifiers):
        """
        按任意键时调用此方法
        """

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

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

        if self.player_sprite.right >= self.end_of_map: # 玩家跳出地图总宽度游戏结束
            self.game_over = True
            
        if self.player_sprite2.right >= self.end_of_map: # 玩家跳出地图总宽度游戏结束
            self.game_over = True


        # 所有角色更新
        if not self.game_over:
            self.physics_engine.update()
            self.physics_engine2.update()



def main():
    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
    window.start_new_game()
    arcade.run()


if __name__ == "__main__":
    main()

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

VIP免费查看

已有0人支付

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

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

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