Skip to content

Instantly share code, notes, and snippets.

@luap42
Last active December 15, 2021 12:20
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 luap42/4d788dffd602801492c536fce58129b3 to your computer and use it in GitHub Desktop.
Save luap42/4d788dffd602801492c536fce58129b3 to your computer and use it in GitHub Desktop.
Flappy Codidactyl JS
function ee() {
style = document.createElement("style");
style.innerText = ".fngm-ee { background-color: #000a; position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: 10000; } .fngm-cv { background-color: white; margin: 3rem; height: calc(100% - 6rem); width: calc(100% - 6rem); }";
document.body.appendChild(style);
eee = document.createElement("div");
eee.classList.add('fngm-ee');
eee.innerHTML = "<canvas class=\"fngm-cv\" height=1000 width=600></canvas>";
document.body.appendChild(eee);
eee.addEventListener("click", (e) => {
if(e.target == eee) {
style.remove();
eee.remove();
}
})
function loadImage(url) {
return new Promise(r => { let i = new Image(); i.onload = (() => r(i)); i.src = url; });
}
map = eee.querySelector("canvas");
ctx = map.getContext('2d');
game_state = {
codidactyl_position: 400,
codidactyl_direction: -1,
score: 0,
speed: 15,
started: false,
lost: false,
lollipops: []
};
map.width = map.getBoundingClientRect().width;
codidactyl_image = new Image();
lollipop_image = new Image();
candy_image = new Image();
codidactyl_image.onload = function () {
lollipop_image.onload = function () {
document.body.addEventListener("keydown", jump_handler);
document.body.addEventListener("click", jump_handler);
setTimeout(drawloop, 1);
}
lollipop_image.src = 'https://codidact.com/lollipop.png'
}
codidactyl_image.src = 'https://codidact.com/codidactyl.png'
async function mainloop() {
function codidactylHandle() {
if (game_state.lost) return;
setTimeout(codidactylHandle, game_state.speed);
game_state.codidactyl_position += game_state.codidactyl_direction;
if (game_state.codidactyl_position > 0) {
game_state.codidactyl_direction--;
} else if (game_state.codidactyl_position == 0) {
game_state.codidactyl_direction = 0;
}
}
async function lollipopHandle() {
if (game_state.lost) return;
setTimeout(lollipopHandle, game_state.speed);
lollipops = game_state.lollipops.map((k) => [k[0], k[1] - 5]);
recalc = lollipops.length == 0;
if (!recalc) {
recalc = lollipops[lollipops.length - 1][1] < 1700;
}
if (recalc) {
threshold = 0.05;
if (game_state.score > 500) threshold = 0.1
if (game_state.score > 1500) threshold = 0.15
if (Math.random() < threshold) {
lollipops.push([Math.random() * 0.6 + 0.2, 2050]);
}
}
if (lollipops[0][1] < -150) {
lollipops.shift();
} else if (lollipops[0][1] == 125) {
game_state.score += 1;
}
game_state.lollipops = lollipops;
}
async function collisionHandle() {
if (game_state.lost) return;
setTimeout(collisionHandle, game_state.speed);
if (game_state.lollipops.length == 0)
return;
relevant_lollipop = game_state.lollipops[0];
if (relevant_lollipop[1] > 250 - 125/2 && relevant_lollipop[1] < 250 + 125/2) {
upper_limit = 1000 * (1 - relevant_lollipop[0] + 0.2);
lower_limit = 1000 * (1 - relevant_lollipop[0] - 0.2) + 100;
if(game_state.codidactyl_position > upper_limit)
game_state.lost = true;
if(game_state.codidactyl_position < lower_limit)
game_state.lost = true;
}
if(game_state.codidactyl_position <= 50)
game_state.lost = true;
}
setTimeout(codidactylHandle, 1);
setTimeout(lollipopHandle, 2);
setTimeout(collisionHandle, 3);
}
function drawloop() {
setTimeout(drawloop, 16);
ctx.clearRect(0, 0, map.width, map.height);
if (!game_state.started) {
ctx.font = (4*map.width/100) + "px monospace";
ctx.fillStyle = "rgba(0,0,0,0.8)";
ctx.fillText("- Flappy Codidactyl -", map.width/4, 150)
ctx.fillText("Press Space to start", map.width/4, 350)
return;
}
ctx.beginPath();
ctx.moveTo(0, 950)
ctx.lineTo(10000, 950)
ctx.stroke()
ctx.drawImage(codidactyl_image, 250, 1000 - game_state.codidactyl_position, 100, 100)
for (let pos = 0; pos < game_state.lollipops.length; pos++) {
const elpos = game_state.lollipops[pos];
upper_img = 1000 * (elpos[0] - 0.2) - 1000;
lower_img = 1000 * (elpos[0] + 0.2);
ctx.drawImage(lollipop_image, elpos[1], lower_img, 125, 1000)
ctx.save()
ctx.translate(elpos[1] + 125 * 0.5, upper_img + 1000 * 0.5);
ctx.rotate(Math.PI);
ctx.translate(- elpos[1] - 125 * 0.5, - upper_img - 1000 * 0.5);
ctx.drawImage(lollipop_image, elpos[1], upper_img, 125, 1000)
ctx.restore();
}
ctx.font = "20px monospace";
ctx.fillStyle = "rgba(0,0,0,0.5)";
if (game_state.lost) {
ctx.fillText("You lost. Final Score: " + Math.floor(game_state.score), 0, 20)
ctx.fillText("Press Space to restart ", 0, 50)
} else {
ctx.fillText("Score: " + Math.floor(game_state.score), 0, 20)
}
}
function jump_handler(e) {
if (typeof e.code == "undefined" || e.code == "Space") {
if (!game_state.started) {
setTimeout(function () { game_state.started = true; mainloop() }, 50);
} else if(game_state.lost) {
game_state = {
codidactyl_position: 500,
codidactyl_direction: 0,
speed: 15,
score: 0,
started: true,
lost: false,
lollipops: []
};
mainloop()
} else {
game_state.codidactyl_direction = 16
}
}
}
}
(function() {
today = new Date();
if(today.getDate() != 1 || today.getMonth() != 3) return;
kon_state = 0;
window.addEventListener("keydown", (e) => {
if(document.body.querySelector(".fngm-ee")) return false;
if(kon_state == 0 && e.key == "ArrowUp") kon_state = 1;
else if(kon_state == 1 && e.key == "ArrowUp") kon_state = 2;
else if(kon_state == 2 && e.key == "ArrowUp") kon_state = 2;
else if(kon_state == 2 && e.key == "ArrowDown") kon_state = 3;
else if(kon_state == 3 && e.key == "ArrowDown") kon_state = 4;
else if(kon_state == 4 && e.key == "ArrowLeft") kon_state = 5;
else if(kon_state == 5 && e.key == "ArrowRight") kon_state = 6;
else if(kon_state == 6 && e.key == "ArrowLeft") kon_state = 7;
else if(kon_state == 7 && e.key == "ArrowRight") kon_state = 8;
else if(kon_state == 8 && e.key == "b") kon_state = 9;
else if(kon_state == 9 && e.key == "a") kon_state = 10;
else if(kon_state == 10 && e.key == "Enter") { kon_state = 11; ee(); }
else kon_state = 0;
})
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment