🎮ArcadeLab

校园突围

by HyperLion37
93 lines3.7 KB🛠️ Phaser (2D game framework)
▶ Play
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/phaser@3.80.1/dist/phaser.min.js"></script>
<script>
const config = {
    type: Phaser.AUTO,
    width:800,
    height:500,
    physics:{default:'arcade',arcade:{gravity:{y:300},debug:false}},
    scene:[BootScene,Scene1,Scene2,Scene3]
};
new Phaser.Game(config);

//启动加载场景,防止直接卡死
class BootScene extends Phaser.Scene{
    constructor(){super("boot");}
    preload(){
        //改用内置纯色图形代替远程图片,彻底解决图片加载卡死!
    }
    create(){
        this.scene.start("level1");
    }
}

class Scene1 extends Phaser.Scene{
    constructor(){super("level1");}
    create(){
        //直接生成矩形代替外部sprite,不需要联网加载角色图
        this.player = this.physics.add.sprite(50,400,null).setDisplaySize(28,44);
        this.player.setCollideWorldBounds(true);
        this.platforms=this.physics.add.staticGroup();
        this.platforms.create(400,480).setDisplaySize(800,40).refreshBody();
        this.platforms.create(200,350).setDisplaySize(140,20).refreshBody();
        this.platforms.create(550,280).setDisplaySize(140,20).refreshBody();
        this.physics.add.collider(this.player,this.platforms);
        this.cursors=this.input.keyboard.createCursorKeys();
        this.next=this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.X);
    }
    update(){
        if(this.cursors.left.isDown) this.player.setVelocityX(-160);
        else if(this.cursors.right.isDown) this.player.setVelocityX(160);
        else this.player.setVelocityX(0);
        if(this.cursors.up.isDown && this.player.body.touching.down) this.player.setVelocityY(-220);
        if(Phaser.Input.Keyboard.JustDown(this.next)) this.scene.start("level2");
    }
}
class Scene2 extends Phaser.Scene{
    constructor(){super("level2");}
    create(){
        this.player=this.physics.add.sprite(50,400,null).setDisplaySize(28,44);
        this.player.setCollideWorldBounds(true);
        this.platforms=this.physics.add.staticGroup();
        this.platforms.create(400,480).setDisplaySize(800,40).refreshBody();
        this.platforms.create(120,360).setDisplaySize(120,20).refreshBody();
        this.platforms.create(400,300).setDisplaySize(120,20).refreshBody();
        this.platforms.create(680,230).setDisplaySize(120,20).refreshBody();
        this.physics.add.collider(this.player,this.platforms);
        this.cursors=this.input.keyboard.createCursorKeys();
        this.next=this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.X);
    }
    update(){
        if(this.cursors.left.isDown) this.player.setVelocityX(-160);
        else if(this.cursors.right.isDown) this.player.setVelocityX(160);
        else this.player.setVelocityX(0);
        if(this.cursors.up.isDown && this.player.body.touching.down) this.player.setVelocityY(-220);
        if(Phaser.Input.Keyboard.JustDown(this.next)) this.scene.start("level3");
    }
}
class Scene3 extends Phaser.Scene{
    constructor(){super("level3");}
    create(){
        this.player=this.physics.add.sprite(50,400,null).setDisplaySize(28,44);
        this.player.setCollideWorldBounds(true);
        this.platforms=this.physics.add.staticGroup();
        this.platforms.create(400,480).setDisplaySize(800,40).refreshBody();
        this.physics.add.collider(this.player,this.platforms);
        this.cursors=this.input.keyboard.createCursorKeys();
    }
    update(){
        if(this.cursors.left.isDown) this.player.setVelocityX(-160);
        else if(this.cursors.right.isDown) this.player.setVelocityX(160);
        else this.player.setVelocityX(0);
        if(this.cursors.up.isDown && this.player.body.touching.down) this.player.setVelocityY(-220);
    }
}
</script>
</body>
</html>

Game Source: 校园突围

Creator: HyperLion37

Libraries: phaser

Complexity: moderate (93 lines, 3.7 KB)

The full source code is displayed above on this page.

Remix Instructions

To remix this game, copy the source code above and modify it. Add a ARCADELAB header at the top with "remix_of: game-hyperlion37" to link back to the original. Then publish at arcadelab.ai/publish.