-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.js
More file actions
76 lines (67 loc) · 1.93 KB
/
start.js
File metadata and controls
76 lines (67 loc) · 1.93 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
/*
The Kebab Game -- https://kebabgame.github.io
Made by Dennis2008
*/
var coin = new Image(100, 100);
var kebap = document.getElementById("kebap"); //gets the html element of kebap
kebap.style.left = "100px"; kebap.style.top = "100px"; //sets some style settings
spawnCoin(); //spawns the first coin
//Functions
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 checkOverlapping() //checks if kebap overlaps with a coin
{
if(isOverlapping(kebap, coin))
{
deleteCoin(); //deletes the coin
addMoney(); //adds money
spawnCoin(); //spawn another coin
}
}
function deleteCoin() //Deletes the coin
{
document.getElementById("coin").remove();
}
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 spawnCoin() //Spawns a coin in a random position
{
createCoin();
createCoinPosition();
document.body.appendChild(coin);
}