Sunday, February 4, 2024

Phaser Game | HTML, CSS, JS

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Phaser Game</title>
    <script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>
</head>
<body>
    <script>
        const config = {
            type: Phaser.AUTO,
            width: 800,
            height: 600,
            physics: {
                default: 'arcade',
                arcade: {
                    gravity: { y: 0 },
                    debug: false
                }
            },
            scene: {
                preload: preload,
                create: create,
                update: update
            }
        };

        const game = new Phaser.Game(config);
        let player;

        function preload() {
            this.load.image('spaceship', 'path/to/spaceship.png');
        }

        function create() {
            player = this.physics.add.image(400, 500, 'spaceship');
            player.setCollideWorldBounds(true);
        }

        function update() {
            const cursors = this.input.keyboard.createCursorKeys();

            if (cursors.left.isDown) {
                player.setVelocityX(-160);
            } else if (cursors.right.isDown) {
                player.setVelocityX(160);
            } else {
                player.setVelocityX(0);
            }

            if (cursors.up.isDown) {
                player.setVelocityY(-160);
            } else if (cursors.down.isDown) {
                player.setVelocityY(160);
            } else {
                player.setVelocityY(0);
            }
        }
    </script>
</body>
</html>

Phaser Game

No comments:

Post a Comment

Chapterwise weightage of Social Science class 9 | 2023-24

  Chapterwise weightage of social science. 1. History Chapter 1 - 5 marks Chapter 2 - 6 marks  Chapter 3 - 7 marks  Map - 2 marks 2. Democra...