Files

25 lines
960 B
JavaScript

// m11 pong view: the same Scene shapes as the snake view, so the same drawScene and runtime draw it.
import { PCELL, PADDLE, PLAYER_X, CPU_X } from './model.js'
const rect = (x, y, w, h, color) => ({ tag: 'Rect', x, y, w, h, color })
const text = (x, y, str, size) => ({ tag: 'Text', x, y, text: str, color: '#ffffff', size, align: 'center' })
const paddle = (column, y) => rect(column * PCELL, y * PCELL, PCELL, PADDLE * PCELL, '#ffffff')
const overlay = (state) =>
state.status === 'Over'
? [
text(320, 200, state.score.player > state.score.cpu ? 'You Win!' : 'CPU Wins', 32),
text(320, 230, 'Press R to restart', 16),
]
: []
export const pongView = (state) => [
rect(0, 0, 640, 400, '#111111'),
paddle(PLAYER_X, state.player),
paddle(CPU_X, state.cpu),
rect(state.ball.x * PCELL, state.ball.y * PCELL, PCELL, PCELL, '#ffeb3b'),
text(320, 30, `${state.score.player} ${state.score.cpu}`, 24),
...overlay(state),
]