-
Notifications
You must be signed in to change notification settings - Fork 5
/
script.min.js
119 lines (111 loc) · 6.83 KB
/
script.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"use strict";
document.body.classList.remove('no-js');
var i = 0,
jump = 0,
foodSpawn = 0,
fruitSpawn = randomIntFromInterval(130, 20),
enemySpawn = 30,
powerupSpawn = randomIntFromInterval(180, 60),
invincible = 0,
timeLeft = 5,
playerPos = 0,
started = 0,
score = 0,
runGame, powerupTime, button = document.querySelector("button"),
grid = [],
food = [],
fruit = [],
enemy = [],
powerup = [];
function randomIntFromInterval(o, e) {
return Math.floor(Math.random() * (e - o + 1) + o)
}
function powerupTimer() {
timeLeft <= 1 && (clearInterval(powerupTime), invincible = 0, timeLeft = 6);
--timeLeft
}
function playerJump() {
switch (jump) {
case 5:
grid[playerPos] = String.fromCharCode(10240) + "^";
break;
case 4:
grid[playerPos] = String.fromCharCode(10240) + "^";
break;
case 3:
grid[playerPos] = String.fromCharCode(10240) + "^";
break;
default:
grid[playerPos] = String.fromCharCode(10240) + "C"
}
}
function gameLogic() {
if (started == 1) {
for (i = 0; i < 55; i++) grid[i] = String.fromCharCode(10240);
invincible == 1 ? (grid[playerPos] = String.fromCharCode(10240) + timeLeft + String.fromCharCode(9730)) : jump > 0 ? (--jump, playerJump()) : (grid[playerPos] = String.fromCharCode(10240) + "C");
if (foodSpawn > 0) --foodSpawn;
else if (!(enemy.includes(55))) {
i = 0;
while (food[i] > 0) i++;
food[i] = 55;
foodSpawn = 1
}
for (i = 0; i < food.length; i++) --food[i], food[i] >= 0 && (food[i] == playerPos ? (invincible == 1 ? (grid[food[i]] = String.fromCharCode(10240) + timeLeft + String.fromCharCode(9730), food[i] = 0, ++score) : jump > 2 ? (playerJump(), food[i] = 0) : (grid[food[i]] = String.fromCharCode(10240) + "o", food[i] = 0, ++score)) : (grid[food[i]] = String.fromCharCode(183)));
if (fruitSpawn > 0) --fruitSpawn;
else if (!(food.includes(53)) && !(enemy.includes(54))) {
i = 0;
while (fruit[i] > 0) i++;
fruit[i] = 55;
fruitSpawn = randomIntFromInterval(170, 50)
}
for (i = 0; i < fruit.length; i++) --fruit[i], fruit[i] >= 0 && (fruit[i] == playerPos ? (invincible == 1 ? (grid[fruit[i]] = String.fromCharCode(10240) + timeLeft + String.fromCharCode(9730), fruit[i] = 0, score += 5) : jump > 2 ? (playerJump(), fruit[i] = 0) : (grid[food[i]] = String.fromCharCode(10240) + "o", fruit[i] = 0, score += 5)) : (grid[fruit[i]] = String.fromCharCode(9643)));
if (enemySpawn > 0) --enemySpawn;
else if (!(food.includes(53))) {
i = 0;
while (enemy[i] > 0) i++;
enemy[i] = 55;
score < 50 ? (enemySpawn = randomIntFromInterval(26, 6)) : score < 180 ? (enemySpawn = randomIntFromInterval(20, 4)) : score < 280 ? (enemySpawn = randomIntFromInterval(13, 4)) : (enemySpawn = randomIntFromInterval(8, 4))
}
for (i = 0; i < enemy.length; i++) --enemy[i], enemy[i] >= 0 && (enemy[i] == playerPos ? (invincible == 1 ? (grid[enemy[i]] = String.fromCharCode(10240) + timeLeft + String.fromCharCode(9730), enemy[i] = 0, --score) : jump > 2 ? (playerJump(), enemy[i] = 0) : (grid[enemy[i]] = String.fromCharCode(10240) + String.fromCharCode(10240) + String.fromCharCode(1161) + String.fromCharCode(10240), enemy[i] = 0, --score, started = 0)) : (grid[enemy[i]] = String.fromCharCode(9731)));
if (powerupSpawn > 0) --powerupSpawn;
else if (!(food.includes(53)) && !(enemy.includes(54))) {
i = 0;
while (powerup[i] > 0) i++;
powerup[i] = 55;
powerupSpawn = randomIntFromInterval(400, 100)
}
for (i = 0; i < powerup.length; i++) --powerup[i], powerup[i] >= 0 && (powerup[i] == playerPos ? (jump > 2 ? (playerJump(), powerup[i] = 0) : (powerupTime = setInterval(powerupTimer, 1e3), grid[powerup[i]] = String.fromCharCode(10240) + timeLeft + String.fromCharCode(9730), powerup[i] = 0, invincible = 1)) : (grid[powerup[i]] = String.fromCharCode(9730)));
history.replaceState(null, '', "#" + grid.join(''))
} else clearInterval(runGame), button !== null && (button.innerHTML == "Sound: on" && document.getElementById("death").play()), grid = [], food = [], fruit = [], enemy = [], powerup = [], jump = 0, foodSpawn = 0, fruitSpawn = randomIntFromInterval(150, 80), enemySpawn = 30, powerupSpawn = randomIntFromInterval(250, 60), localStorage.getItem("highScore") < score && localStorage.setItem("highScore", score), setTimeout(function () {
history.replaceState(null, '', "#" + String.fromCharCode(10240) + String.fromCharCode(10240) + String.fromCharCode(1161) + String.fromCharCode(10240) + String.fromCharCode(10240) + "💀 YOU" + String.fromCharCode(10240) + "DIED!" + String.fromCharCode(10240) + "Score:" + String.fromCharCode(10240) + score);
button !== null && displayScore()
}, 200), setTimeout(function () {
history.replaceState(null, '', "#" + String.fromCharCode(10240) + String.fromCharCode(10240) + String.fromCharCode(1161) + String.fromCharCode(10240) + String.fromCharCode(10240) + "GAME" + String.fromCharCode(10240) + "OVER!" + String.fromCharCode(10240) + "Score:" + String.fromCharCode(10240) + score + String.fromCharCode(10240) + "(Press" + String.fromCharCode(10240) + "any" + String.fromCharCode(10240) + "key" + String.fromCharCode(10240) + "to" + String.fromCharCode(10240) + "restart)");
startGame()
}, 1600)
}
function keyPress() {
started = 1;
document.onkeydown = function (o) {
o.preventDefault();
var e = o.keyCode;
e == 38 && jump === 0 && (jump = 5)
}
}
function startGame() {
document.onkeydown = function (o) {
var e = o.keyCode;
(e == 224 || e == 91) || (o.preventDefault(), keyPress(), score = 0, runGame = setInterval(gameLogic, 100), button !== null && (button.innerHTML == "Sound: on" && document.getElementById("start").play()))
}
}
history.replaceState(null, '', location.pathname.replace(/\b\/$/, ''));
history.replaceState(null, '', "#" + String.fromCharCode(10240) + "PRESS" + String.fromCharCode(10240) + "ANY" + String.fromCharCode(10240) + "KEY" + String.fromCharCode(10240) + "TO" + String.fromCharCode(10240) + "PLAY");
startGame();
button !== null && (localStorage.getItem("soundPref") == "Sound: off" && (button.innerHTML = "Sound: off"));
function toggleSound() {
button.innerHTML == "Sound: on" ? (button.innerHTML = "Sound: off", localStorage.setItem("soundPref", button.innerHTML)) : (button.innerHTML = "Sound: on", localStorage.setItem("soundPref", button.innerHTML))
}
function displayScore() {
(localStorage.getItem("highScore") === null) || (document.getElementById("high-score").innerHTML = localStorage.getItem("highScore"))
}
button !== null && displayScore()