Last active
February 4, 2019 08:38
-
-
Save pcoutin/323387c6ec470af44390ba4798a428b0 to your computer and use it in GitHub Desktop.
SCRIPT-8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// title: PONG | |
initialState = { | |
paddles: { | |
a: {x: 0, y: 50}, | |
b: {x: 125, y: 50} | |
}, | |
ball: { | |
x: 4, | |
y: 56, | |
dx: 42, | |
dy: -4 | |
}, | |
scoreA: 0, | |
scoreB: 0, | |
started: false, | |
}; | |
// TODO integer movements, make moving paddle affect ball, | |
// better collision, networking | |
const PADDLE_WIDTH = 3; | |
const PADDLE_HEIGHT = 12; | |
const PADDLE_SPEED = 0.1; | |
const BALL_SPEED = 1/400; | |
const HUD_HEIGHT = 13; | |
const serveBall = () => ({ | |
x: 4, | |
y: random(20, 100), | |
dx: 42, | |
dy: random(-10, 10) | |
}); | |
const inRange = (value, low, high) => | |
value >= low && value <= high | |
const collisionBallPaddle = (ball, paddle) => | |
inRange(ball.x, paddle.x, paddle.x + PADDLE_WIDTH) && | |
inRange(ball.y, paddle.y, paddle.y + PADDLE_HEIGHT) | |
const movePaddle = (paddle, inUp, inDown, elapsed) => { | |
const PADDLE_YMAX = 128 - PADDLE_HEIGHT; | |
if (inUp) { | |
const newY = paddle.y - PADDLE_SPEED*elapsed; | |
paddle.y = clamp(newY, HUD_HEIGHT, PADDLE_YMAX); | |
} else if (inDown) { | |
const newY = paddle.y + PADDLE_SPEED*elapsed; | |
paddle.y = clamp(newY, HUD_HEIGHT, PADDLE_YMAX); | |
} | |
} | |
update = (state, input, elapsed) => { | |
if (input.start) { | |
state.started = true; | |
} | |
// Ball movement | |
if (state.started) { | |
state.ball.x += state.ball.dx * elapsed * BALL_SPEED; | |
state.ball.y += state.ball.dy * elapsed * BALL_SPEED; | |
} | |
// Paddle movement | |
movePaddle(state.paddles.a, input.up, input.down, elapsed) | |
movePaddle(state.paddles.b, input.a, input.b, elapsed) | |
// Ball collision with walls at top & bottom | |
if (state.ball.y > 127 || state.ball.y < HUD_HEIGHT) { | |
state.ball.dy *= -1; | |
} | |
// Ball collision with paddles | |
if (collisionBallPaddle(state.ball, state.paddles.a)) { | |
state.ball.dx = Math.abs(state.ball.dx); | |
} | |
if (collisionBallPaddle(state.ball, state.paddles.b)) { | |
state.ball.dx = -Math.abs(state.ball.dx); | |
} | |
// Scoring | |
if (state.ball.x > 127) { | |
state.scoreB += 1 | |
} | |
if (state.ball.x < 0) { | |
state.scoreA += 1 | |
} | |
if (inRange(state.ball.x, 0, 127) === false) { | |
state.started = false | |
state.ball = serveBall() | |
} | |
state.ball.x = clamp(state.ball.x, 0, 127); | |
state.ball.y = clamp(state.ball.y, 0, 127); | |
} | |
const drawPaddle = paddle => | |
rectFill(paddle.x, paddle.y, PADDLE_WIDTH, PADDLE_HEIGHT, 0) | |
draw = state => { | |
clear(); | |
rectFill(0, 0, 128, HUD_HEIGHT, 5); // HUD | |
print(4, 4, state.scoreB); | |
print(115, 4, state.scoreA); | |
// mesh thing | |
range(0, 26).forEach(i => rectFill(63, i*5, 2, 3, 5)) | |
drawPaddle(state.paddles.a) | |
drawPaddle(state.paddles.b) | |
circFill(state.ball.x, state.ball.y, 2, 1) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment