Files
functional-programming-snake/js/solutions/pong/model.js
T

39 lines
1.2 KiB
JavaScript

// m11 pong model: the same ideas as the snake model, for a different game.
/** @typedef {{ball: {x: number, y: number}, vel: {dx: number, dy: number}, player: number, cpu: number,
* held: {up: boolean, down: boolean}, score: {player: number, cpu: number},
* status: 'Playing'|'Over', seed: number, frame: number}} PongState */
// The court, in cells. `player` and `cpu` are the y of the top cell of each paddle.
export const W = 80
export const H = 50
/** Pixels per cell. */
export const PCELL = 8
/** Paddle height in cells. */
export const PADDLE = 8
/** Column of the player's paddle (left) and the cpu's paddle (right). */
export const PLAYER_X = 2
export const CPU_X = 77
/** Points needed to win the match. */
export const WIN = 5
export const initialPong = (seed) => ({
ball: { x: 40, y: 25 },
vel: { dx: 1, dy: 1 },
player: 21,
cpu: 21,
held: { up: false, down: false },
score: { player: 0, cpu: 0 },
status: 'Playing',
seed,
frame: 0,
})
/** Everything that can happen in a match, as data. */
export const PongAction = {
Press: (key) => ({ tag: 'Press', key }),
Release: (key) => ({ tag: 'Release', key }),
Frame: { tag: 'Frame' },
Restart: { tag: 'Restart' },
}