Skip to content

Instantly share code, notes, and snippets.

@gabrielflorit
Last active March 1, 2020 06:24
Show Gist options
  • Save gabrielflorit/b0a6fb11bed0520bbc476ed8e19bf430 to your computer and use it in GitHub Desktop.
Save gabrielflorit/b0a6fb11bed0520bbc476ed8e19bf430 to your computer and use it in GitHub Desktop.
SCRIPT-8
const bounceBalls = state => {
state.actors.forEach(ball => {
if (ball.x < 0 || ball.x > 128 - 8) {
ball.dx *= -1
}
if (ball.y < 0 || ball.y > 128 - 8) {
ball.dy *= -1
}
})
}
const moveBalls = (state, extra = 0) => {
state.actors.forEach(ball => {
ball.x += ball.dx + extra
ball.y += ball.dy + extra
})
}
init = state => {
// If you're going to use `state.actors`,
// make sure to give each item in `actors`
// a unique `id`.
state.actors = [
{
id: 'ball0',
x: 10,
y: 60,
dx: 2,
dy: 1,
sprite: 0
},
{
id: 'ball1',
x: 30,
y: 90,
dx: -1,
dy: -2,
sprite: 1
},
{
id: 'ball2',
x: 60,
y: 30,
dx: -1,
dy: -1,
sprite: 2
}
]
}
update = (state, input, elapsed) => {
bounceBalls(state)
moveBalls(state, 0)
}
draw = state => {
clear()
rectStroke(0, 0, 128, 128, 5)
// `drawActors` is a built-in SCRIPT-8 function.
drawActors(state)
}
{
"iframeVersion": "0.1.280",
"lines": [
63,
0,
0,
0,
0,
0,
0,
0
]
}
{
"0": [
" 000000 ",
"00222200",
"02222220",
"02277220",
"02272220",
"02222220",
"00222200",
" 000000 "
],
"1": [
" 333333 ",
"33777733",
"37777773",
"37707773",
"37700773",
"37777773",
"33777733",
" 333333 "
],
"2": [
" 444444 ",
"44000044",
"40000004",
"40077004",
"40007004",
"40000004",
"44000044",
" 444444 "
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment