Skip to content

Instantly share code, notes, and snippets.

@spacehunter
Last active April 5, 2020 18:12
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/7207a4a3ba4086daca19f352f458d46e to your computer and use it in GitHub Desktop.
Save spacehunter/7207a4a3ba4086daca19f352f458d46e to your computer and use it in GitHub Desktop.
SCRIPT-8
init = state => {
state.time = 0;
state.ship = { x: 67, y: 14, xSpeed: 0, ySpeed: 0 };
state.asteroids = [{
x: random(20,120), y: 25, xSpeed: 0.25, ySpeed: 0
},{
x: random(20,120), y: 25, xSpeed: 1, ySpeed: 0
}];
}
update = (state, key, elapsed) => {
state.time += elapsed / 42;
state.delta = elapsed * (1 / 24);
// if(state.showHelp) {
// if(key.a) state.showHelp = false;
// }
updateShip(state, key);
updateAsteroids(state);
}
let updateAsteroids = (state) => {
state.asteroids.map(a => {
a.x += a.xSpeed;
if (a.x > 128) a.x = -20
})
}
let updateShip = (state, key) => {
if(!key.left && !key.right && !key.up && !key.down) {
const [xR, yR] = [random() - 0.5, random() - 0.5];
state.ship.xSpeed += xR / 32 * 2 * state.delta;
state.ship.ySpeed += yR / 64 * 2 * state.delta
}
if(key.left) state.ship.xSpeed -= 0.2 * state.delta;
if(key.right) state.ship.xSpeed += 0.2 * state.delta;
if(key.down) state.ship.ySpeed += 0.1 * state.delta;
if(key.up) state.ship.ySpeed -= 0.1 * 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; }
}
draw = state => {
clear(6)
drawAsteroids(state.asteroids, state.time);
drawShip(state.ship, state.time);
}
let drawShip = (ship, time) => {
const animPhase = time / 3 % 1;
const shade = (90 - ship.y) / 90 * -3 + 1 | 0;
sprite(ship.x - 8, ship.y - 8, 0, shade)
// sprite(copter.x, copter.y - 8, animPhase < 0.5 ? 1 : 3, shade)
// sprite(copter.x - 8, copter.y, 16, shade)
// sprite(copter.x, copter.y, 17, shade)
// sprite(copter.x + 8, copter.y, animPhase < 0.33 ? 18 : animPhase < 0.67 ? 19 : 20, shade)
}
let drawAsteroids = (asteroids) => {
asteroids.map(a => {
sprite(a.x, a.y, 1)
})
}
{
"iframeVersion": "0.1.280",
"lines": [
64,
21,
0,
0,
0,
0,
0,
0
]
}
{
"0": [
" 0 ",
" 060 ",
" 000 ",
" 000 ",
" 00000 ",
" 0000000",
" 0000000",
" 0 0"
],
"1": [
" 22 ",
" 22 2 ",
" 2 2 ",
"2 2",
"2 2",
" 22 2",
" 2 2 ",
" 22 "
],
"2": [
" 222 ",
" 2 2",
" 22 ",
"2 ",
"2 ",
"2 ",
" 2 ",
" 2 "
],
"3": [
" ",
"2 ",
" 22 ",
" 2 ",
" 2 ",
" 2 ",
" 2 ",
" 2 "
],
"4": [
" 222 ",
" 22 22",
"2 ",
"2 ",
"2 ",
"2 ",
"2 ",
" 2 "
],
"5": [
" ",
"22222 ",
" 222",
" ",
" ",
" ",
" ",
" "
],
"6": [
" ",
" ",
" ",
"2 ",
" 222 ",
" 22 ",
" 2 ",
" 2"
],
"7": [
" ",
" ",
" ",
" ",
" ",
" ",
" ",
"2 "
],
"18": [
" 2 ",
" 2 ",
" 2 22",
" 222 ",
" ",
" ",
" ",
" "
],
"19": [
" 2 ",
" 2 ",
" 22 ",
"2 ",
" ",
" ",
" ",
" "
],
"20": [
" 2 ",
" 2 ",
" 2 ",
" 2 ",
" 2 ",
" 2 ",
" 2 ",
" 222"
],
"23": [
"2 ",
" 2 ",
" 2 ",
" 2 ",
" 2 ",
" 2 ",
" 2 ",
" 2 "
],
"37": [
"2 ",
"2 ",
" 2 ",
" 2 ",
" 2 ",
" 22 ",
" 222",
" "
],
"38": [
" ",
" ",
" ",
" 2",
" 22 ",
" 222 ",
"22 ",
" "
],
"39": [
" 2 ",
" 2 ",
" 22 ",
"2 ",
" ",
" ",
" ",
" "
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment