-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
155 lines (139 loc) · 3.92 KB
/
game.js
File metadata and controls
155 lines (139 loc) · 3.92 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
The Kebab Game -- https://kebabgame.github.io
Made by Dennis2008
*/
let money = 0;
let typeOfCoin = 1;
let seconds = 0, minutes = 0;
let coin = new Image(100, 100);
let kebap = document.getElementById("kebap"); //gets the html element of kebap
kebap.style.left = "100px"; kebap.style.top = "100px"; //sets some style settings
main();
function main()
{
//let fork = new Image(100, 100);
spawnCoin(); //spawns the first coin
//spawnFork();
window.addEventListener("keydown", function(e) {
move(e.code);
});
setInterval(function (){
document.getElementById("timer").innerHTML = "Timer: " + minutes + "min and " + seconds + "s";
seconds++;
if(seconds == 60)
{
minutes++;
seconds = 0;
}
}, 1000);
}
function move(b) { //checks the button's input and moves the kebab
if(b=="ArrowLeft" || b=="KeyA") {
kebap.style.left=(parseInt(kebap.style.left)-50)+"px";
}
else if(b=="ArrowRight" || b=="KeyD") {
kebap.style.left=(parseInt(kebap.style.left)+50)+"px";
}
else if(b=="ArrowUp" || b=="KeyW") {
kebap.style.top=(parseInt(kebap.style.top)-50)+"px";
}
else if(b=="ArrowDown" || b=="KeyS") {
kebap.style.top=(parseInt(kebap.style.top)+50)+"px";
}
else if(b=="Digit1")
{
changeFood(0);
}
else if(b=="Digit2")
{
changeFood(1);
}
else if(b=="Digit3")
{
changeFood(2);
}
checkCoin(); //calls checkOverlapping()
}
function checkCoin() { //checks if kebap overlaps with a coin
if(isOverlapping(kebap, coin)) {
deleteCoin(); //deletes the coin
addMoney(); //adds money
spawnCoin(); //spawn another coin
}
}
function spawnCoin() { //Spawns a coin in a random position
createCoin();
createCoinPosition();
document.body.appendChild(coin);
}
function spawnFork() {
if(!forkSpawned && money>=80) {
fork.id = "fork";
fork.src = "fork.png";
fork.style.zIndex = 1;
fork.style.position = 'absolute';
document.body.appendChild(fork);
forkSpawned = true;
}
}
function createCoin() { //Creates the coin
var x = 0;
var y = 0;
typeOfCoin = parseInt(Math.random() * 5);
if(typeOfCoin==0) {
coin.src = "res/coin1.png";
}
else {
coin.src = "res/coin.png";
}
coin.id = "coin";
coin.style.zIndex = 1;
coin.style.position = 'absolute';
}
function createCoinPosition() { //Generates the coin's random position and applies it
const rectKebap = kebap.getBoundingClientRect();
do {
y = Math.random()*(screen.height-250);
x = Math.random()*(screen.width-200);
}
while((y>rectKebap.bottom-100 && y<rectKebap.top+100) || (x>rectKebap.left-100 && x<rectKebap.right+100));
coin.style.top = y + "px";
coin.style.left = x + "px";
}
function deleteCoin() { //Deletes the coin
document.getElementById("coin").remove();
}
function addMoney() { //Adds money
if(typeOfCoin==0) {
money = money + 1;
}
else {
money = money + 0.50;
}
document.getElementById("points").innerHTML = "Money: " + money + " €";
}
function isOverlapping(element1, element2) { //Checks if two element are overlapping
const rect1 = element1.getBoundingClientRect();
const rect2 = element2.getBoundingClientRect();
return !(
rect1.right < rect2.left || // Element 1 is left of Element 2
rect1.left > rect2.right || // Element 1 is right of Element 2
rect1.bottom < rect2.top || // Element 1 is above Element 2
rect1.top > rect2.bottom // Element 1 is below Element 2
);
}
function showChangeFood() {
var hide = document.getElementById("hide");
if (hide.style.display === "none") {
hide.style.display = "block";
} else {
hide.style.display = "none";
}
}
function changeFood(g) { //changes the food by changing it's image
switch(g) {
case 0: kebap.src = "res/kebab.png"; break;
case 1: kebap.src = "res/burger.png"; break;
case 2: kebap.src = "res/curry.png"; break;
}
}