Last active
June 23, 2018 02:58
-
-
Save gabrielflorit/66c668a3a83e69736c6fbdc80d49282f 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
{ | |
"0": { | |
"0": { | |
"0": 0 | |
} | |
}, | |
"1": { | |
"0": { | |
"0": 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
// title: Break-8 | |
const e = { | |
paddle: { | |
width: 64, | |
height: 8, | |
color: 3 | |
}, | |
ball: { | |
width: 32, | |
height: 32, | |
color: 0 | |
} | |
} | |
initialState = { | |
won: 0, | |
lost: 0, | |
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 | |
playSong(0) | |
} | |
if (ball.y < 1) { | |
ball.dy *= -1 | |
playSong(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 | |
state.won++ | |
e.ball.width = Math.max(1, e.ball.width / 2) | |
e.ball.height = Math.max(1, e.ball.height / 2) | |
e.paddle.width = Math.max(1, e.paddle.width / 2) | |
playSong(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 | |
state.lost++ | |
e.ball.width = Math.max(1, e.ball.width / 2) | |
e.ball.height = Math.max(1, e.ball.height / 2) | |
e.paddle.width = Math.max(1, e.paddle.width / 2) | |
} | |
} | |
update = (state, input) => { | |
const paddle = state.actors.find(d => d.name === 'paddle') | |
const ball = state.actors.find(d => d.name === 'ball') | |
movePaddle(paddle, input) | |
// paddle.x = ball.x | |
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) | |
range(state.won).forEach(i => { | |
sprite(2 + i * 6, 2, 2) | |
}) | |
range(state.lost).forEach(i => { | |
sprite(2 + i * 6, 8, 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
{ | |
"0": [ | |
"0c17" | |
], | |
"1": [ | |
"0c#37" | |
] | |
} |
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": { | |
"0": 0 | |
}, | |
"1": { | |
"0": 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
{ | |
"1": [ | |
"3 3 ", | |
" 3 3 ", | |
" 3 ", | |
" 3 3 ", | |
"3 3 ", | |
" ", | |
" ", | |
" " | |
], | |
"2": [ | |
" 0 0 ", | |
" ", | |
" ", | |
"0 0 ", | |
" 000 ", | |
" ", | |
" ", | |
" " | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment