๐ŸŽฎArcadeLab

Pixel Platformer Maker

by PixelStar14
385 lines12.9 KB๐Ÿ› ๏ธ Phaser (2D game framework)
โ–ถ Play
<!-- ARCADELAB
title: Pixel Platformer Maker
description: Build your own custom platformer levels and play them instantly!
libraries: phaser
-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pixel Platformer Maker</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            background-color: #1a1a1a;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            color: #fff;
            overflow: hidden;
        }
        #game-container {
            box-shadow: 0 10px 30px rgba(0,0,0,0.5);
            border-radius: 8px;
            overflow: hidden;
        }
    </style>
</head>
<body>

<div id="game-container"></div>

<script>
class BootScene extends Phaser.Scene {
    constructor() {
        super('BootScene');
    }

    create() {
        // Generate procedural pixel graphics textures dynamically so no external assets are required
        const graphics = this.add.graphics();

        // 1. Ground / Block (Brown brick)
        graphics.fillStyle(0x8B4513);
        graphics.fillRect(0, 0, 32, 32);
        graphics.lineStyle(2, 0x5c2c16);
        graphics.strokeRect(0, 0, 32, 32);
        graphics.generateTexture('block', 32, 32);
        graphics.clear();

        // 2. Coin (Yellow circle)
        graphics.fillStyle(0xFFD700);
        graphics.fillCircle(16, 16, 10);
        graphics.lineStyle(2, 0xDAA520);
        graphics.strokeCircle(16, 16, 10);
        graphics.generateTexture('coin', 32, 32);
        graphics.clear();

        // 3. Enemy (Red spiky/creature block)
        graphics.fillStyle(0xCC0000);
        graphics.fillRect(4, 8, 24, 20);
        graphics.fillStyle(0xFFFFFF);
        graphics.fillRect(8, 12, 6, 6);
        graphics.fillRect(18, 12, 6, 6);
        graphics.fillStyle(0x000000);
        graphics.fillRect(10, 14, 2, 2);
        graphics.fillRect(20, 14, 2, 2);
        graphics.generateTexture('enemy', 32, 32);
        graphics.clear();

        // 4. Goal Flag (Green pole + flag)
        graphics.fillStyle(0x888888);
        graphics.fillRect(14, 0, 4, 32);
        graphics.fillStyle(0x00FF00);
        graphics.fillTriangle(18, 2, 30, 10, 18, 18);
        graphics.generateTexture('goal', 32, 32);
        graphics.clear();

        // 5. Player (Mario-ish blue/red sprite)
        graphics.fillStyle(0x0000FF); // Overalls
        graphics.fillRect(8, 16, 16, 12);
        graphics.fillStyle(0xFF0000); // Shirt & Hat
        graphics.fillRect(6, 8, 20, 10);
        graphics.fillRect(12, 4, 12, 4);
        graphics.fillStyle(0xFFCC99); // Face
        graphics.fillRect(12, 10, 10, 8);
        graphics.generateTexture('player', 32, 32);
        graphics.clear();

        graphics.destroy();

        this.scene.start('GameScene');
    }
}

class GameScene extends Phaser.Scene {
    constructor() {
        super('GameScene');
    }

    create() {
        this.cameras.main.setBackgroundColor('#5c94fc'); // Classic sky blue

        // Game state: 'BUILD' or 'PLAY'
        this.mode = 'BUILD';
        this.selectedTool = 'block'; // 'block', 'coin', 'enemy', 'goal', 'eraser'

        // Grid system (25 cols x 15 rows = 800x480)
        this.gridSize = 32;
        this.cols = 25;
        this.rows = 15;

        // Level grid storage: maps "x,y" to type string
        this.levelData = {};

        // Groups
        this.blocksGroup = this.physics.add.staticGroup();
        this.coinsGroup = this.physics.add.staticGroup();
        this.enemiesGroup = this.physics.add.group({ immovable: true, allowGravity: false });
        this.goalGroup = this.physics.add.staticGroup();

        // Default Spawn Point
        this.spawnX = 64;
        this.spawnY = 384;

        // Create UI Overlay
        this.createUI();

        // Create Player
        this.player = this.physics.add.sprite(this.spawnX, this.spawnY, 'player');
        this.player.setCollideWorldBounds(false);
        this.player.body.setGravityY(800);
        this.player.setActive(false);
        this.player.setVisible(true);

        // Collisions for Play Mode
        this.physics.add.collider(this.player, this.blocksGroup);
        this.physics.add.overlap(this.player, this.coinsGroup, this.collectCoin, null, this);
        this.physics.add.overlap(this.player, this.enemiesGroup, this.hitEnemy, null, this);
        this.physics.add.overlap(this.player, this.goalGroup, this.reachGoal, null, this);

        // Input Setup
        this.cursors = this.input.keyboard.createCursorKeys();
        this.keySpace = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);
        
        // Mouse input for building
        this.input.on('pointerdown', (pointer) => this.handlePointer(pointer));
        this.input.on('pointermove', (pointer) => {
            if (pointer.isDown) this.handlePointer(pointer);
        });

        // Load a nice default starter layout
        this.loadDefaultLevel();
    }

    createUI() {
        // Top Toolbar Background
        const toolbar = this.add.graphics();
        toolbar.fillStyle(0x222222, 0.9);
        toolbar.fillRect(0, 0, 800, 50);
        toolbar.setScrollFactor(0);
        toolbar.setDepth(100);

        // Mode switch button
        this.modeButtonText = this.add.text(20, 15, '๐ŸŽฎ PLAY LEVEL', { fontSize: '16px', fill: '#00ff00', fontStyle: 'bold' })
            .setInteractive()
            .setScrollFactor(0)
            .setDepth(101);

        this.modeButtonText.on('pointerdown', () => {
            this.toggleMode();
        });

        // Tool buttons configuration
        const tools = [
            { name: 'block', label: '๐Ÿงฑ Block', x: 180 },
            { name: 'coin', label: '๐Ÿช™ Coin', x: 280 },
            { name: 'enemy', label: '๐Ÿ‘พ Enemy', x: 380 },
            { name: 'goal', label: '๐Ÿšฉ Goal', x: 480 },
            { name: 'eraser', label: '๐Ÿงน Eraser', x: 580 }
        ];

        this.toolButtons = {};
        tools.forEach(t => {
            const btn = this.add.text(t.x, 15, t.label, { fontSize: '15px', fill: '#ffffff' })
                .setInteractive()
                .setScrollFactor(0)
                .setDepth(101);

            btn.on('pointerdown', () => {
                if (this.mode === 'BUILD') {
                    this.selectedTool = t.name;
                    this.updateToolHighlights();
                }
            });
            this.toolButtons[t.name] = btn;
        });

        // Instructions
        this.instructionText = this.add.text(400, 460, 'Click grid to place items. Switch to PLAY to test!', { fontSize: '14px', fill: '#ffff00' })
            .setOrigin(0.5, 0)
            .setScrollFactor(0)
            .setDepth(101);

        this.updateToolHighlights();
    }

    updateToolHighlights() {
        for (let key in this.toolButtons) {
            if (key === this.selectedTool) {
                this.toolButtons[key].setStyle({ fill: '#00ffff', fontStyle: 'bold' });
            } else {
                this.toolButtons[key].setStyle({ fill: '#ffffff', fontStyle: 'normal' });
            }
        }
    }

    toggleMode() {
        if (this.mode === 'BUILD') {
            this.mode = 'PLAY';
            this.modeButtonText.setText('๐Ÿ”จ BUILD MODE');
            this.modeButtonText.setStyle({ fill: '#ffaa00' });
            
            // Hide tool buttons highlight in play mode
            for (let key in this.toolButtons) {
                this.toolButtons[key].setAlpha(0.4);
            }

            // Reset player to start position and activate physics
            this.player.setPosition(this.spawnX, this.spawnY);
            this.player.setVelocity(0, 0);
            this.player.setActive(true);
            this.instructionText.setText('Use LEFT/RIGHT to move, UP to jump! Press BUILD to edit.');
        } else {
            this.mode = 'BUILD';
            this.modeButtonText.setText('๐ŸŽฎ PLAY LEVEL');
            this.modeButtonText.setStyle({ fill: '#00ff00' });

            for (let key in this.toolButtons) {
                this.toolButtons[key].setAlpha(1);
            }

            this.player.setActive(false);
            this.player.setVelocity(0, 0);
            this.player.setPosition(this.spawnX, this.spawnY);
            this.instructionText.setText('Click grid to place items. Switch to PLAY to test!');
        }
    }

    handlePointer(pointer) {
        if (this.mode !== 'BUILD') return;
        if (pointer.y < 50) return; // Don't build over toolbar

        const col = Math.floor(pointer.x / this.gridSize);
        const row = Math.floor(pointer.y / this.gridSize);
        const key = `${col},${row}`;

        if (this.selectedTool === 'eraser') {
            if (this.levelData[key]) {
                this.levelData[key].destroy();
                delete this.levelData[key];
            }
        } else {
            if (this.levelData[key]) {
                this.levelData[key].destroy();
            }

            const x = col * this.gridSize + this.gridSize / 2;
            const y = row * this.gridSize + this.gridSize / 2;

            let obj;
            if (this.selectedTool === 'block') {
                obj = this.blocksGroup.create(x, y, 'block');
            } else if (this.selectedTool === 'coin') {
                obj = this.coinsGroup.create(x, y, 'coin');
            } else if (this.selectedTool === 'enemy') {
                obj = this.enemiesGroup.create(x, y, 'enemy');
            } else if (this.selectedTool === 'goal') {
                obj = this.goalGroup.create(x, y, 'goal');
            }

            if (obj) {
                obj.refreshBody && obj.refreshBody();
                this.levelData[key] = obj;
            }
        }
    }

    loadDefaultLevel() {
        // Create an initial floor so player has ground when testing
        for (let c = 0; c < this.cols; c++) {
            if (c < 10 || c > 12) { // leave a small gap or solid floor
                const x = c * this.gridSize + this.gridSize / 2;
                const y = 13 * this.gridSize + this.gridSize / 2;
                const block = this.blocksGroup.create(x, y, 'block');
                block.refreshBody();
                this.levelData[`${c},13`] = block;
            }
        }
        // Add a platform
        for (let c = 5; c < 9; c++) {
            const x = c * this.gridSize + this.gridSize / 2;
            const y = 9 * this.gridSize + this.gridSize / 2;
            const block = this.blocksGroup.create(x, y, 'block');
            block.refreshBody();
            this.levelData[`${c},9`] = block;
        }
        // Add a goal flag at the end
        const gx = 22 * this.gridSize + this.gridSize / 2;
        const gy = 12 * this.gridSize + this.gridSize / 2;
        const goal = this.goalGroup.create(gx, gy, 'goal');
        goal.refreshBody();
        this.levelData[`22,12`] = goal;
    }

    collectCoin(player, coin) {
        coin.destroy();
    }

    hitEnemy(player, enemy) {
        // If player is falling down onto the enemy, destroy enemy. Otherwise reset player.
        if (player.body.velocity.y > 0 && player.y < enemy.y) {
            enemy.destroy();
            player.setVelocityY(-350);
        } else {
            this.resetPlayer();
        }
    }

    reachGoal(player, goal) {
        this.instructionText.setText('๐ŸŽ‰ LEVEL COMPLETE! Toggle back to build more.');
        this.player.setVelocity(0, 0);
        this.player.setActive(false);
    }

    resetPlayer() {
        this.player.setPosition(this.spawnX, this.spawnY);
        this.player.setVelocity(0, 0);
    }

    update() {
        if (this.mode === 'PLAY' && this.player.active) {
            if (this.cursors.left.isDown) {
                this.player.setVelocityX(-200);
            } else if (this.cursors.right.isDown) {
                this.player.setVelocityX(200);
            } else {
                this.player.setVelocityX(0);
            }

            if (this.cursors.up.isDown && this.player.body.touching.down) {
                this.player.setVelocityY(-450);
            }

            // Fall off screen check
            if (this.player.y > 550) {
                this.resetPlayer();
            }
        }
    }
}

const config = {
    type: Phaser.AUTO,
    width: 800,
    height: 480,
    parent: 'game-container',
    physics: {
        default: 'arcade',
        arcade: {
            gravity: { y: 0 },
            debug: false
        }
    },
    scene: [BootScene, GameScene]
};

const game = new Phaser.Game(config);
</script>

</body>
</html>

Game Source: Pixel Platformer Maker

Creator: PixelStar14

Libraries: phaser

Complexity: complex (385 lines, 12.9 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: pixel-platformer-maker-pixelstar14" to link back to the original. Then publish at arcadelab.ai/publish.