python街机萤火虫勇闯黑夜迷宫_大型滚动地图

"""
萤火虫勇闯黑夜迷宫_大型滚动地图

"""

import random
import arcade
import os

SPRITE_SCALING = 1

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SPRITE_PIXEL_SIZE = 64

# 距离屏幕边距的最小距离 

VIEWPORT_MARGIN = 40
MOVEMENT_SPEED = 5

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

    def __init__(self, width, height):
        """
        初始化器
        """
        super().__init__(width, height,"萤火虫勇闯黑夜迷宫_大型滚动地图 lixingqiu")

        self.game_over = False

        # 角色列表
        self.player_list = None
        self.coin_list = None

        # 设置游戏及玩家相关变量
        self.score = 0
        self.player_sprite = None
        self.wall_list = None
        self.physics_engine = None
        self.view_bottom = 0
        self.view_left = 0

    def setup(self):
        """ 实例化变量 """

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

        # 设置蒙板
        self.mask = arcade.Sprite("images/mask.png")
        self.mask.scale = 2

        # 实例化玩家操作的角色
        self.score = 0
        self.player_sprite = arcade.Sprite("images/lixingqiu.png", 0.4)
        self.player_sprite.center_x = 128
        self.player_sprite.center_y = 270
        self.player_list.append(self.player_sprite)

        # 下面是加载地图        
        my_map = arcade.read_tiled_map(f"bigroom_1.tmx", SPRITE_SCALING)
        
        # 读取不可移动的平台数据阵列'ground'是一图层的名称
        map_array = my_map.layers_int_data['ground']        
          
        # 从墙生成地图列表
        self.wall_list = arcade.generate_sprites(my_map, 'ground', SPRITE_SCALING)
        self.wall_list.move(SPRITE_PIXEL_SIZE ,0)   # 水平方向和垂直方向移动
        
        # 加载简单型的物理引擎,简单的理解为让玩家和墙发生碰撞的封装好了的对象
        self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite,self.wall_list)
        
        # 读取key阵列, picked是地图中的一个层,这个层里是可拾取的道具。
        keys_array = my_map.layers_int_data['picked']
        
        # 生成地图中可拾取的小东西角色列表
        self.小东西_list = arcade.generate_sprites(my_map, 'picked', SPRITE_SCALING)
        self.小东西_list.move(SPRITE_PIXEL_SIZE ,0) 

        self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list)

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

        # 设置视口边界
        self.view_left = 0
        self.view_bottom = 0

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

        # 开始渲染
        arcade.start_render()

        # 画各个角色
        self.wall_list.draw()
        self.player_list.draw()
        self.小东西_list.draw()
        self.mask.draw()

        if self.game_over :
            
            # 写Game Over
            output = "Game Over"
            arcade.draw_text(output, 100+SCREEN_WIDTH//2,SCREEN_HEIGHT//2, arcade.color.GREEN,54)

                

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

        if key == arcade.key.UP:
            self.player_sprite.change_y = MOVEMENT_SPEED
        elif key == arcade.key.DOWN:
            self.player_sprite.change_y = -MOVEMENT_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.UP or key == arcade.key.DOWN:
            self.player_sprite.change_y = 0
        elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
            self.player_sprite.change_x = 0

    def update(self, delta_time):
        """ 游戏逻辑设定,结果都是更新坐标/角度,删除/添加角色等。"""

        # 调用物理引擎
        self.physics_engine.update()

        # 蒙板跟随玩家角色
        self.mask.center_x = self.player_sprite.center_x
        self.mask.center_y = self.player_sprite.center_y

        # 玩家与key的碰撞检测,返回列表,列表不为空则把里面的key删除
        hits = arcade.check_for_collision_with_list(self.player_sprite, self.小东西_list)
        for dx in hits: dx.kill()

        if len(self.小东西_list) == 0 and not self.game_over:
            print("游戏结束,画一个Game Over。")
            self.game_over = True


        # --- 管理滚动
        changed = False

        # 左滚动
        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

        # 右滚动
        right_bndry = self.view_left + SCREEN_WIDTH - VIEWPORT_MARGIN
        if self.player_sprite.right > right_bndry:
            self.view_left += self.player_sprite.right - right_bndry
            changed = True

        # 上滚动
        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

        # 下滚动
        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

        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)
    window.setup()
    arcade.run()

if __name__ == "__main__":
    main()

python arcade 萤火虫勇闯黑夜迷宫_大型滚动地图 lixingqiu下面gif动录制作软件把图片进行了压缩,所以呈现了“圆圈”效果。实际上效果像上图这样。python arcade 萤火虫勇闯黑夜迷宫_大型滚动地图 lixingqiu