Python街机arcade模块加载csv地图示例程序

Python街机arcade模块加载csv地图示例程序

"""
加载一个csv格式的地图。csv文件是地图的图块映射列表。里面是按行存储的数字。每行的每个数字映射地图中图块的编号。
"""

import arcade
import os

SPRITE_SCALING = 0.5

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Python街机arcade模块加载csv地图示例程序"
SPRITE_PIXEL_SIZE = 128
GRID_PIXEL_SIZE = (SPRITE_PIXEL_SIZE * SPRITE_SCALING)

# How many pixels to keep as a minimum margin between the character
# and the edge of the screen.
VIEWPORT_MARGIN = 40
RIGHT_MARGIN = 150

# 物理参数
MOVEMENT_SPEED = 5
JUMP_SPEED = 14
GRAVITY = 0.5     # 代表重力加速度


def get_map(filename):
    """
    加载逗号隔开数字的二维数字表,解析成嵌套列表。
    """
    map_file = open(filename)
    map_array = []
    for line in map_file:       # 文件中的每一行
        line = line.strip()     # 去掉空白字符
        map_row = line.split(",") # 以逗号分隔
        # 执行下列这个for循环是为了把item转换成整数,
        # 相当于 map_row = [ int(item) for item in map_row ]
        for index, item in enumerate(map_row):   
            map_row[index] = int(item)
        map_array.append(map_row)
    return map_array


class MyGame(arcade.Window):
    """ 应用程序的主要类. """

    def __init__(self):
        """
        初始化方法
        """
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)

        # 设置游戏的工作目录,这是为 "python -m" 启动程序而设 
        file_path = os.path.dirname(os.path.abspath(__file__))
        os.chdir(file_path)

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

        # 玩家定义
        self.player_sprite = None

        self.physics_engine = None
        self.view_left = 0
        self.view_bottom = 0
        self.game_over = False

    def setup(self):
        """ 设置游戏的一些变量. """

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

        # 实例化玩家所操作角色
        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)

        # 得到 2维地图的映射表
        map_array = get_map("map.csv")
        print(map_array)
        # 得到地图最右边的像素值
        self.end_of_map = len(map_array[0]) * GRID_PIXEL_SIZE # 地图宽度
        # 下面取出每个图块编号,根据号码生成wall
        for row_index, row in enumerate(map_array):
            for column_index, item in enumerate(row):

                # 对于此地图来说,
                # -1 = empty            空
                # 0  = box             盒子 
                # 1  = grass left edge 左边角草
                # 2  = grass middle    中间的草
                # 3  = grass right edge右边角草
                if item == -1:
                    continue
                elif item == 0:
                    wall = arcade.Sprite("images/boxCrate_double.png", SPRITE_SCALING)
                elif item == 1:
                    wall = arcade.Sprite("images/grassLeft.png", SPRITE_SCALING)
                elif item == 2:
                    wall = arcade.Sprite("images/grassMid.png", SPRITE_SCALING)
                elif item == 3:
                    wall = arcade.Sprite("images/grassRight.png", SPRITE_SCALING)

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

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

        # 设置背景颜色
        arcade.set_background_color(arcade.color.AMAZON)

        # Set the view port boundaries
        # These numbers set where we have 'scrolled' to.
        self.view_left = 0
        self.view_bottom = 0

        self.game_over = False

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

        # 此命令放在所有重绘命令之前
        arcade.start_render()

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

        # Put the text on the screen.
        # Adjust the text position based on the view port so that we don't
        # scroll the text too.
        distance = self.player_sprite.right
        output = f"Distance: {distance}"
        arcade.draw_text(output, self.view_left + 10, self.view_bottom + 20, arcade.color.WHITE, 14)

        if self.game_over:
            arcade.draw_text("Game Over", 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

    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):
        """ Movement and game logic """

        if self.player_sprite.right >= self.end_of_map:
            self.game_over = True

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

        # --- Manage Scrolling ---

        # Track if we need to change the view port

        changed = False

        # Scroll left
        left_bndry = self.view_left + VIEWPORT_MARGIN
        if self.player_sprite.left < left_bndry:
            self.view_left -= left_bndry - self.player_sprite.left
            changed = True

        # Scroll right
        right_bndry = self.view_left + SCREEN_WIDTH - RIGHT_MARGIN
        if self.player_sprite.right > right_bndry:
            self.view_left += self.player_sprite.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 += self.player_sprite.top - top_bndry
            changed = True

        # Scroll down
        bottom_bndry = self.view_bottom + VIEWPORT_MARGIN
        if self.player_sprite.bottom < bottom_bndry:
            self.view_bottom -= bottom_bndry - self.player_sprite.bottom
            changed = True
        print(self.player_sprite.left,self.view_left)
        # If we need to scroll, go ahead and do it.
        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()
    window.setup()
    arcade.run()


if __name__ == "__main__":
    main()

 

李兴球

李兴球的博客是Python创意编程原创博客

评论已关闭。