使用Phaser制作一个简单游戏

栏目: 后端 · 发布时间: 5年前

<!DOCTYPE html>
<html lang="en">
<head>
    <title>App</title>
    <script src="//cdn.jsdelivr.net/npm/phaser@3.16.2/dist/phaser.js"></script>
</head>
<body>
<script>
    new Phaser.Game({
        type: Phaser.AUTO,
        width: 800,
        height: 600,
        physics: {
            default: 'arcade',
            arcade: {
                gravity: {
                    y: 300
                },
                debug: false
            }
        },
        scene: {
            preload: function () {
                this.load.image('sky', 'assets/sky.png');
                this.load.image('ground', 'assets/platform.png');
                this.load.image('star', 'assets/star.png');
                this.load.image('bomb', 'assets/bomb.png');
                this.load.spritesheet('dude', 'assets/dude.png', {
                    frameWidth: 32,
                    frameHeight: 48
                })
            },
            create: function () {
                this.gameOver = false;
                this.add.image(400, 300, 'sky');
                const platforms = this.physics.add.staticGroup();
                platforms.create(400, 568, 'ground').setScale(2).refreshBody();
                platforms.create(600, 400, 'ground');
                platforms.create(50, 250, 'ground');
                platforms.create(750, 220, 'ground');
                const player = this.physics.add.sprite(100, 450, 'dude');
                this.player = player;
                player.setBounce(0.2);
                player.setCollideWorldBounds(true);
                this.anims.create({
                    key: 'left',
                    frames: this.anims.generateFrameNumbers('dude', {
                        start: 0,
                        end: 3
                    }),
                    frameRate: 10,
                    repeat: -1
                });
                this.anims.create({
                    key: 'right',
                    frames: this.anims.generateFrameNumbers('dude', {
                        start: 5,
                        end: 8
                    }),
                    frameRate: 10,
                    repeat: -1
                });
                this.anims.create({
                    key: 'turn',
                    frames: [
                        {
                            key: 'dude',
                            frame: 4
                        }
                    ]
                });
                this.cursors = this.input.keyboard.createCursorKeys();
                const stars = this.physics.add.group({
                    key: 'star',
                    repeat: 11,
                    setXY: {
                        x: 12,
                        y: 0,
                        stepX: 70
                    }
                });
                stars.children.iterate(function (child) {
                    child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
                });
                const bombs = this.physics.add.group();
                let score = 0;
                const scoreText = this.add.text(16, 16, 'score: 0', {
                    fontSize: '32px',
                    fill: '#000'
                });
                this.physics.add.collider(player, platforms);
                this.physics.add.collider(stars, platforms);
                this.physics.add.collider(bombs, platforms);
                this.physics.add.overlap(player, stars, function (player, star) {
                    star.disableBody(true, true);
                    score += 10;
                    scoreText.setText("Score: " + score);
                    if (stars.countActive(true) !== 0) return;
                    stars.children.iterate(function (child) {
                        child.enableBody(true, child.x, 0, true, true);
                    });
                    const x = (player.x < 400) ? Phaser.Math.Between(400, 800) :
                        Phaser.Math.Between(0, 400);
                    const bomb = bombs.create(x, 16, 'bomb');
                    bomb.setBounce(1);
                    bomb.setCollideWorldBounds(true);
                    bomb.setVelocity(Phaser.Math.Between(-200, 200), 20);
                    bomb.allowGravity = false;
                }, null, this);
                this.physics.add.collider(player, bombs, player => {
                    this.physics.pause();
                    player.setTint(0xff0000);
                    player.anims.play('turn');
                    this.gameOver = true;
                }, null, this);
            },
            update: function () {
                if (this.gameOver) return;
                const cursors = this.cursors;
                const player = this.player;
                if (cursors.left.isDown) {
                    player.setVelocityX(-160);
                    player.anims.play('left', true);
                } else if (cursors.right.isDown) {
                    player.setVelocityX(160);
                    player.anims.play('right', true);
                } else {
                    player.setVelocityX(0);
                    player.anims.play('turn');
                }
                if (cursors.up.isDown && player.body.touching.down) {
                    player.setVelocity(-330);
                }
            }
        }
    });
</script>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Java语言程序设计(基础篇 原书第10版)

Java语言程序设计(基础篇 原书第10版)

[美]粱勇(Y.Daniel Liang) / 戴开宇 / 机械工业出版社 / 2015-7 / 85.00元

《Java语言程序设计(基础篇 原书第10版)》是Java语言的经典教材,中文版分为基础篇和进阶篇,主要介绍程序设计基础、面向对象编程、GUI程序设计、数据结构和算法、高级Java程序设计等内容。本书以示例讲解解决问题的技巧,提供大量的程序清单,每章配有大量复习题和编程练习题,帮助读者掌握编程技术,并应用所学技术解决实际应用开发中遇到的问题。您手中的这本是其中的基础篇,主要介绍了基本程序设计、语法......一起来看看 《Java语言程序设计(基础篇 原书第10版)》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试