Aller au contenu

Deuxième joueur

Andy

Ajouter un autre joueur peut permettre à un jeu de s’ouvrir à de nouvelles dimensions. Les joueurs peuvent par exemple s’assister mutuellement dans leur progression dans un esprit coopératif, ou à l’inverse concourir dans un esprit de compétition.

  1. load.js
    function loadAtlas() {
    loadSpriteAtlas("assets/sprites/prune-atlas.png", {
    "prune": {
    x: 0, y: 0,
    width: 244, height: 53,
    sliceX: 4,
    anims: {
    idle: { from: 0, to: 0 },
    run: { from: 0, to: 0 },
    sleep: { from: 1, to: 1 },
    jump: { from: 2, to: 2 },
    worry: { from: 3, to: 3 },
    },
    },
    })
    loadSpriteAtlas("assets/sprites/bean-atlas.png", {
    "bean": {
    x: 0, y: 0,
    width: 244, height: 53,
    sliceX: 4,
    anims: {
    idle: { from: 0, to: 0 },
    run: { from: 0, to: 0 },
    sleep: { from: 1, to: 1 },
    jump: { from: 2, to: 2 },
    worry: { from: 3, to: 3 },
    },
    },
    })
    loadSpriteAtlas("assets/sprites/splash.png", {
    "splash": {
    x: 0, y: 0,
    width: 384, height: 64,
    sliceX: 6,
    anims: { explode: { from: 0, to: 5, speed:30 } },
    },
    })
    }
  2. config.js
    const LEVEL_CONFIG = {
    tileWidth: 64,
    tileHeight: 64,
    backgroundColor: "afe1ff",
    gravity: 3200,
    tiles: {
    "&": () => [ // player 2
    sprite("prune"),
    platformerController({
    jumpKey: "z",
    leftKey: "q",
    downKey: "s",
    rightKey: "d",
    }),
    alive(),
    opacity(),
    scale(),
    health(1, 4),
    area(),
    anchor("bot"),
    body(),
    respawn(),
    falling(),
    ],
    "#": () => [ // player 1
    sprite("bean"),
    platformerController(),
    alive(),
    opacity(),
    scale(),
    health(1, 4),
    area(),
    anchor("bot"),
    body(),
    respawn(),
    falling(),
    ],
    "=": () => [ // block
    sprite("grass"),
    area(),
    body({ isStatic: true }),
    anchor("bot"),
    offscreen({ hide: true }),
    ],
    },
    };
  3. level.js
    const LEVELS = [
    {
    map: `
    ===
    # &
    =====
    `,
    },
    ]
example
config.js
platformerController({
jumpKey: "up",
leftKey: "left",
rightKey: "right",
downKey: "down",
jumpForce: 1000,
moveSpeed: 480,
glideDelay: 0.1,
airjumpForce: 800,
doubleJump: 0,
sleepDelay: 2
}),