双人游戏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()

        # 地图的总宽度
        self.end_of_map = len(map_array[0]) * GRID_PIXEL_SIZE

        map_items = ["images/boxCrate_double.png",
                     "images/grassCenter.png",
                     "images/grassCorner_left.png",
                     "images/grassCorner_right.png",
                     "images/grassHill_left.png",
                     "images/grassHill_right.png",
                     "images/grassLeft.png",
                     "images/grassMid.png",
                     "images/grassRight.png",
                     "images/stoneHalf.png"
                     ]
        for row_index, row in enumerate(map_array):
            for column_index, item in enumerate(row):

                if item == -1:         # 地图空的地方表示为-1
                    continue
                else:
                    wall = arcade.Sprite(map_items[item],
                                         SPRITE_SCALING)

                    # Change the collision polygon to be a ramp instead of
                    # a rectangle
                    # 改变碰撞的多边形,而不是矩形
                    if item == 4: # 左斜直角三角形块
                        wall.points = ((-wall.width // 2, wall.height // 2),
                                       (wall.width // 2, -wall.height // 2),
                                       (-wall.width // 2, -wall.height // 2))
                    elif item == 5: # 右斜直角三角形块
                        wall.points = ((-wall.width // 2, -wall.height // 2),
                                       (wall.width // 2, -wall.height // 2),
                                       (wall.width // 2, wall.height // 2))

                wall.right = column_index * 64
                wall.top = (7 - row_index) * 64
                self.all_sprites_list.append(wall)
                self.wall_list.append(wall)

        self.physics_engine  = \
            arcade.PhysicsEnginePlatformer(self.player_sprite,
                                           self.wall_list,
                                           gravity_constant=GRAVITY)
        self.physics_engine2 = \
            arcade.PhysicsEnginePlatformer(self.player_sprite2,
                                           self.wall_list,
                                           gravity_constant=GRAVITY)

        # 设置背景颜色为亚马逊深绿色
        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()

        # 放得分文本在屏幕上,self.view_left是变化的再加上10这样它好像就静止不动了
        distance = self.player_sprite.right
        output = "距离: {}".format(distance)
        arcade.draw_text(output, self.view_left + 10, self.view_bottom + 20,
                         arcade.color.RED, 14,font_name='simsun')

        if self.game_over:
            output = "Game Over"
            arcade.draw_text(output, self.view_left + 200,
                             self.view_bottom + 200,
                             arcade.color.WHITE, 30)

    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

            
        if key == arcade.key.W:
            if self.physics_engine2.can_jump():
                self.player_sprite2.change_y = JUMP_SPEED
        elif key == arcade.key.A:
            self.player_sprite2.change_x = -MOVEMENT_SPEED
        elif key == arcade.key.D:
            self.player_sprite2.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
        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()

        # --- Manage Scrolling ---

        # 跟踪是否需要重新设置视口矩形

        changed = False

        # Scroll left 视口往左滚
        left_bndry = self.view_left + VIEWPORT_MARGIN
        if self.player_sprite.left < left_bndry:
            self.view_left -= int(left_bndry - self.player_sprite.left)
            changed = True        
        if self.player_sprite2.left < left_bndry:
            self.view_left -= int(left_bndry - self.player_sprite2.left)
            changed = True
            

        # Scroll right 视口往右滚
        right_bndry = self.view_left + SCREEN_WIDTH - RIGHT_MARGIN
        if self.player_sprite.right > right_bndry:
            self.view_left += int(self.player_sprite.right - right_bndry)
            changed = True            
        if self.player_sprite2.right > right_bndry:
            self.view_left += int(self.player_sprite2.right - right_bndry)
            changed = True


        # Scroll up 视口往上滚
        top_bndry = self.view_bottom + SCREEN_HEIGHT - VIEWPORT_MARGIN
        if self.player_sprite.top > top_bndry:
            self.view_bottom += int(self.player_sprite.top - top_bndry)
            changed = True            
        if self.player_sprite2.top > top_bndry:
            self.view_bottom += int(self.player_sprite2.top - top_bndry)
            changed = True


        # Scroll down 视口往下滚
        bottom_bndry = self.view_bottom + VIEWPORT_MARGIN
        if self.player_sprite.bottom < bottom_bndry:
            self.view_bottom -= int(bottom_bndry - self.player_sprite.bottom)
            changed = True            
        if self.player_sprite2.bottom < bottom_bndry:
            self.view_bottom -= int(bottom_bndry - self.player_sprite2.bottom)
            changed = True


        # 重新设置视口,观察地图的矩形区域
        if changed:
            arcade.set_viewport(self.view_left,
                                SCREEN_WIDTH + self.view_left,
                                self.view_bottom,
                                SCREEN_HEIGHT + self.view_bottom)


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


if __name__ == "__main__":
    main()