有封面和过关界面的_小昆虫接金币闯关 _Python迷宫跳跃型闯关游戏模版

"""
小昆虫接金币闯关,这是用Python Arcade街机模块制作的一个多关卡小游戏。
相对于前一个版本,这个游戏有开始界面,不过是用海龟画图模块制作的。
每过一关还有“按空格键进入下一关的“的过关提示画面。

全部关卡过完后,有“通关界面”。本游戏需要先安装arcade模块和有相关素材才能正确运行。
作者是李兴球。
"""

import arcade
import time

SPRITE_SCALING = 1                    # 定义缩放比例
SCREEN_WIDTH = 960                    # 定义所渲染的屏幕宽度 
SCREEN_HEIGHT = 768                   # 定义所渲染的屏幕高度 
SCREEN_TITLE = "有封面和过关界面的_小昆虫接金币闯关_lixingqiu "
SPRITE_PIXEL_SIZE = 64               # 地图方块尺寸 

# 定义物理常数 
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.game_over = False
        self.last_time = None       # 用来算fps的上次时间变量
        self.frame_count = 0        # 帧计数器
        self.fps_message = None     # fps字符串

        self.level = 1              # 开始关卡号
        self.max_level = 3          # 关卡数量

    def setup(self):
        """ 设置与初始化变量的值. """
        self.status = "level_end"
        self.level_cover = arcade.Sprite("过关界面.png")
        self.level_cover.textures.append(arcade.load_texture("通关界面.png"))
        self.level_cover.center_x = SCREEN_WIDTH//2
        self.level_cover.center_y = SCREEN_HEIGHT//2
        
        
        # 角色列表初始化
        self.player_list = arcade.SpriteList()
        self.coin_list = arcade.SpriteList()

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

        # 给昆虫设定坐标
        self.player_sprite.center_x = 128
        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):

        self.status = "level_playing"
        # 读取第level关的地图
        my_map = arcade.read_tiled_map(f"lxq_{level}.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.PhysicsEnginePlatformer(self.player_sprite,
                                                             self.wall_list,
                                                             gravity_constant=GRAVITY)        
        # 读取金币阵列,added by lixingqiu,picked是地图中的一个层,这个层里是可拾取的道具。
        coins_array = my_map.layers_int_data['picked']
        # 生成金币列表
        self.coin_list = arcade.generate_sprites(my_map, 'picked', SPRITE_SCALING)
        self.coin_list.move(SPRITE_PIXEL_SIZE ,0)        

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

    def on_draw(self):
        """
        渲染屏幕
        """
        self.frame_count += 1      # 帧计数器

        # 开始渲染屏幕
        arcade.start_render()
        if self.status == "level_playing": # 如果正在玩游戏
            # 画所有的角色
            self.player_list.draw()
            self.wall_list.draw()
            self.coin_list.draw()
            # 计算FPS,帧率
            if self.last_time and self.frame_count % 60 == 0:      # 约每秒画一次
                fps = 1.0 / (time.time() - self.last_time) * 60
                self.fps_message = f"FPS: {fps:5.0f}"

            if self.fps_message:
                arcade.draw_text(self.fps_message, 10,40, arcade.color.BLACK, 14)

            if self.frame_count % 60 == 0:
                self.last_time = time.time()
        else:                               # 否则本关结束,如果游戏没有结束,则渲染过关界面,否则画游戏结束画面。
            
            if self.game_over: self.level_cover.set_texture(1) # 游戏结束,把这个角色的造型换成通关图片。
            self.level_cover.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

        if key == arcade.key.SPACE:
            if self.status == "level_end":                
                self.level += 1
                self.load_level(self.level)
                self.player_sprite.center_x = 128  # 角色重新摆位
                self.player_sprite.center_y = 64

    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):
        """ 移动与游戏的逻辑,这个方法不断地执行,如果本关金币数加载完了,那么此关结束 """

        if len(self.coin_list)==0:           # 如果金币都接完成,准备进入下一关
            self.status = "level_end"
            if self.level == self.max_level:                 
                self.game_over = True

        # 角色更新
        if not self.game_over:
            self.physics_engine.update()
        # 角色与金币的碰撞检测,返回列表,列表不为空则把里面的金币删除并加分
        coins_hit = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list)
        for coin in coins_hit:
            coin.kill()
            self.score += 1
            
def display_cover(cover_image):
    """显示封面"""
    import turtle
    screen = turtle.Screen()
    screen.bgpic(cover_image)
    screen.setup(SCREEN_WIDTH,SCREEN_HEIGHT)
    screen.title(SCREEN_TITLE)
    screen.onkeypress(lambda:screen.bye(),"space") # 绑定空格键
    screen.listen()
    screen.mainloop()
    
def main():
     
    display_cover("封面.png") 
    window = MyGame()
    window.setup()
    arcade.run()

if __name__ == "__main__":
    main()