Skip to content

Instantly share code, notes, and snippets.

@spacehunter
Last active April 20, 2020 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spacehunter/673ad3713e2305d161d1005b653d4755 to your computer and use it in GitHub Desktop.
Save spacehunter/673ad3713e2305d161d1005b653d4755 to your computer and use it in GitHub Desktop.
SCRIPT-8
// title: MM2
init = state => {
state.time = 0;
state.ship = { x: 64, y: 115, xSpeed: 0, ySpeed: 0 };
state.x = [10,13,26,30,29,31,26,22,8,1,2,1,4];
state.ay = [0,2,1,8,15,20,31,31,29,22,16,7,0];
state.asteroids = []
createAsteroids(state);
}
update = (state, key, elapsed) => {
state.time += elapsed / 42;
state.delta = elapsed * (1 / 24);
updateShip(state, key);
updateAsteroids(state);
}
draw = state => {
clear(3);
sprite(state.ship.x, state.ship.y, 0);
drawAsteroids(state);
}
function createAsteroids(state, key) {
range(25).map(() => state.asteroids.push({
x: random(0, 128), y: random(0, 100), xSpeed: random(-0.5, 0.5), size: random(2, 12), deg: random(360), dspd: random(-5, 5)
}));
}
function updateAsteroids(state, key) {
state.asteroids.map(a => {
a.x += a.xSpeed;
if (a.x < -15) a.x = 138;
if (a.x > 138) a.x = -15;
})
}
function drawAsteroids(state, spring=1) {
state.asteroids.map(aster => {
let roids = [];
state.x.map( (a, i) => {
let x = aster.x+a/aster.size*spring;
let y = aster.y+state.ay[i]/aster.size*spring;
roids.push([x,y]);
});
polyStroke(roids, aster.deg += aster.dspd, 5)
});
}
function updateShip(state, key) {
if(key.left) state.ship.xSpeed -= 0.04 * state.delta;
if(key.right) state.ship.xSpeed += 0.04 * state.delta;
if(key.down) state.ship.ySpeed += 0.04 * state.delta;
if(key.up) state.ship.ySpeed -= 0.04 * state.delta;
// decelerate
if(state.ship.xSpeed !== 0) {
state.ship.x += state.ship.xSpeed * state.delta;
state.ship.xSpeed *= .95;
}
if(state.ship.ySpeed !== 0) {
state.ship.y += state.ship.ySpeed * state.delta;
state.ship.ySpeed *= .95;
}
// limit
if(state.ship.y < 2) {
state.ship.y = 2;
state.ship.ySpeed = 0;
}
if(state.ship.y > state.maxY) {
state.ship.y = state.maxY;
state.ship.ySpeed = 0;
}
if(state.ship.x < 8) { state.ship.x = 8; }
if(state.ship.x > 127 - 12) { state.ship.x = 127 - 12; }
}
{
"iframeVersion": "0.1.280",
"lines": [
80,
0,
0,
0,
0,
0,
0,
0
]
}
{
"0": [
" 0 ",
" 0 0 ",
" 000 ",
" 00000 ",
" 00000 ",
" 0000000",
" 0077700",
" 07 70"
],
"1": [
" ",
" 77 ",
" 557 ",
" 57 57 ",
" 75775 ",
" 55 ",
" ",
" "
],
"2": [
" ",
" 55 ",
" 55 55 ",
"5 5",
"5 5",
" 5 5",
" 5 55 ",
" 55 "
],
"16": [
" 7 ",
" 222 ",
" 22222 ",
" 1114111",
" 1114111",
" 22222 ",
" 222 ",
" 7 "
],
"17": [
" ",
" ",
" 5 ",
" 5 5 ",
" 5 57 ",
" 5 5 ",
" 5 ",
" "
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment