-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
106 lines (90 loc) · 3.45 KB
/
script.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
// elements
let character = document.getElementById("character");
let ground = document.getElementById("ground");
let scoreSpan = document.querySelector(".score span");
// parameters
let score = 0;
let characterBottom = parseInt(window.getComputedStyle(character).getPropertyValue('bottom'));
let characterRight = parseInt(window.getComputedStyle(character).getPropertyValue('right'));
let characterWidth = parseInt(window.getComputedStyle(character).getPropertyValue('width'));
let characterHeight = parseInt(window.getComputedStyle(character).getPropertyValue('height'));
let groundBottom = parseInt(window.getComputedStyle(ground).getPropertyValue('bottom'));
let groundRight = parseInt(window.getComputedStyle(ground).getPropertyValue('right'));
let groundWidth = parseInt(window.getComputedStyle(ground).getPropertyValue('width'));
let groundHeight = parseInt(window.getComputedStyle(ground).getPropertyValue('height'));
// const variables
const supportsTouch = ('ontouchstart' in window) || (navigator.maxTouchPoints > 0) || (navigator.msMaxTouchPoints > 0);
// variables
let isJumping = false;
// timers
let upTime;
let downTime;
// functions
function jump()
{
if (isJumping) return;
upTime = setInterval(() => {
if (characterBottom >= groundHeight + 250) {
clearInterval(upTime);
downTime = setInterval(() => {
if (characterBottom <= groundHeight + 10) {
clearInterval(downTime);
isJumping = false;
}
characterBottom -= 10;
character.style.bottom = characterBottom + "px";
}, 20);
}
characterBottom += 10;
character.style.bottom = characterBottom + "px";
isJumping = true;
}, 20);
}
function control(e)
{
if (e.key === "ArrowUp" || e.key === ' ') {
jump();
}
}
function generateObstacles()
{
let obstacles = document.querySelector(".obstacles");
let obstacle = document.createElement("div");
obstacle.classList.add("obstacle");
obstacles.appendChild(obstacle);
let randomTimeout = Math.floor(Math.random() * 1000) + 1000;
let obstacleRight = -30;
let obstacleBottom = 100;
let obstacleWidth = 30;
let obstacleHeight = Math.floor(Math.random() * 50) + 50;
obstacle.style.backgroundColor = "rgb(" + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + ")";
function moveObject()
{
obstacleRight += 5;
obstacle.style.right = obstacleRight + "px";
obstacle.style.bottom = obstacleBottom + "px";
obstacle.style.width = obstacleWidth + "px";
obstacle.style.height = obstacleHeight + "px";
if (characterRight >= obstacleRight - characterWidth &&
characterRight <= obstacleRight + obstacleWidth &&
characterBottom <= obstacleBottom + obstacleHeight) {
alert("Game Over, your score is: " + score);
clearInterval(obstacleInterval);
clearTimeout(obstacleTimeout);
location.reload();
}
}
let obstacleInterval = setInterval(moveObject, 10*2);
let obstacleTimeout = setTimeout(generateObstacles, randomTimeout);
}
function showScore()
{
score++;
scoreSpan.innerHTML = score;
}
// init
generateObstacles();
setInterval(showScore, 1000);
// events
window.addEventListener("keydown", control);
if (supportsTouch) window.addEventListener("touchstart", jump);