This is a SCRIPT-8 cassette.
Created
June 29, 2018 06:18
-
-
Save gabrielflorit/3ddb15bfa2afd07404b93c5ad8a4a615 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: Break-10 | |
const e = { | |
paddle: { | |
width: 24, | |
height: 4, | |
color: 3 | |
}, | |
ball: { | |
width: 4, | |
height: 4, | |
color: 0 | |
} | |
} | |
initialState = { | |
actors: [ | |
{ | |
name: 'paddle', | |
x: 64 - e.paddle.width / 2, | |
y: 128 - e.paddle.height - 2 | |
}, | |
{ | |
name: 'ball', | |
x: 80 - e.ball.width / 2, | |
y: 27, | |
dx: 1, | |
dy: 1 | |
} | |
] | |
} | |
const movePaddle = (paddle, input) => { | |
if (input.left && paddle.x > 0) paddle.x = Math.floor(paddle.x - 1) | |
if (input.right && paddle.x < 128 - e.paddle.width) { | |
paddle.x = Math.floor(paddle.x + 1) | |
} | |
} | |
const bounceBallOffWalls = ball => { | |
if (ball.x < 1 || ball.x > 127 - e.ball.width) { | |
ball.dx *= -1 | |
playPhrase(0) | |
} | |
if (ball.y < 1) { | |
ball.dy *= -1 | |
playPhrase(0) | |
} | |
} | |
const bounceBallOffPaddle = (ball, paddle, state) => { | |
if ( | |
// Left bounce | |
ball.y + e.ball.height > paddle.y && | |
ball.x + e.ball.width >= paddle.x && | |
ball.x <= paddle.x | |
) { | |
ball.dx = -1 | |
} else if ( | |
// Right bounce | |
ball.y + e.ball.height > paddle.y && | |
ball.x <= paddle.x + e.paddle.width && | |
ball.x + e.ball.width >= paddle.x + e.paddle.width | |
) { | |
ball.dx = 1 | |
} else if ( | |
// Top bounce | |
ball.y + e.ball.height >= paddle.y && | |
ball.x + e.ball.width >= paddle.x && | |
ball.x <= paddle.x + e.paddle.width | |
) { | |
ball.dy *= -1 | |
playPhrase(1) | |
} | |
} | |
const moveBall = ball => { | |
ball.x = Math.floor(ball.x + ball.dx) | |
ball.y = Math.floor(ball.y + ball.dy) | |
} | |
const loseBall = (ball, state) => { | |
if (ball.y > 127) { | |
ball.x = random(e.ball.width, 128 - e.ball.width * 2) | |
ball.y = 64 | |
} | |
} | |
update = (state, input) => { | |
const paddle = state.actors.find(d => d.name === 'paddle') | |
const ball = state.actors.find(d => d.name === 'ball') | |
movePaddle(paddle, input) | |
moveBall(ball) | |
bounceBallOffWalls(ball) | |
bounceBallOffPaddle(ball, paddle, state) | |
loseBall(ball, state) | |
} | |
const drawActor = (actor, fade) => { | |
if (actor.name === 'paddle') { | |
rectFill( | |
actor.x, | |
actor.y, | |
e.paddle.width, | |
e.paddle.height, | |
e.paddle.color + (fade ? 3 : 0) | |
) | |
} | |
if (actor.name === 'ball') { | |
rectFill( | |
actor.x, | |
actor.y, | |
e.ball.width, | |
e.ball.height, | |
e.ball.color + (fade ? 5 : 0) | |
) | |
} | |
} | |
drawActors = (state, fade) => { | |
state.actors.forEach(actor => drawActor(actor, fade)) | |
} | |
draw = state => { | |
clear() | |
rectStroke(0, 0, 128, 128, 6) | |
drawActors(state) | |
} |
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
{ | |
"0": [ | |
"0c17" | |
], | |
"1": [ | |
"0c37" | |
] | |
} |
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