-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
185 lines (165 loc) · 5.36 KB
/
Copy pathapp.js
File metadata and controls
185 lines (165 loc) · 5.36 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const grid = document.querySelector('.grid');
const doodler = document.createElement('div');
const scoreCard = document.getElementById('score');
const gameEndMessage = document.getElementById('gameEndMessage');
let doodlerLeftSpace = 50;
let doodlerBottomSpace = 300;
let platformCount = 5;
let platformArray = [];
let isGameOver = false;
let upTimer, downTimer, leftTimer, rightTimer;
let isJumping = true;
let startPoint = 150;
let isRight = false;
let isLeft = false;
let score = 0;
const gridStyle = window.getComputedStyle(grid);
const gridWidth = parseInt(gridStyle.width, 10); //getting grid width from style.css and parsing only the number part (e.g. "400" instead of "400px")
var gridHeight = parseInt(gridStyle.height, 10);
class Platform {
constructor(platformBottom) {
this.bottom = platformBottom;
this.left = Math.random() * (gridWidth - 80); //placing it somewhere between the grid width - platform width
this.platform = document.createElement('div');
const platform = this.platform;
platform.classList.add('platform');
platform.style.bottom = this.bottom + 'px';
platform.style.left = this.left + 'px';
grid.appendChild(platform);
}
}
function createDoodler() {
grid.appendChild(doodler);
doodler.classList.add('doodler');
doodlerLeftSpace = platformArray[0].left + 10; //make doodler start on the first platform
doodlerBottomSpace = platformArray[0].bottom + 10;
doodler.style.left = doodlerLeftSpace + 'px';
doodler.style.bottom = doodlerBottomSpace + 'px';
}
function createPlatform () {
for(let i = 0; i < platformCount; i++) {
let platformGap = gridHeight / platformCount; //height of grid divided by number of platforms
let platformBottom = 100 + i * platformGap; //varying vertical gap between platforms
let newPlatform = new Platform(platformBottom);
platformArray.push(newPlatform);
}
}
function movePlatforms() {
if(doodlerBottomSpace > 50) {
platformArray.forEach(platformElement => {
platformElement.bottom -= 4;
let platform = platformElement.platform;
platform.style.bottom = platformElement.bottom + 'px';
if(platformElement.bottom < 1) { //making platforms disappear as they reach the bottom of the screen
let firstPlatform = platformArray[0].platform;
firstPlatform.classList.remove('platform');
platformArray.shift();
let newPlatform = new Platform(gridHeight); //add new platforms coming in from the top
platformArray.push(newPlatform);
score+=10;
scoreCard.innerHTML= "Score:" + score;
}
})
}
}
function gameOver() {
console.log('game over');
isGameOver = true;
clearInterval(upTimer);
clearInterval(downTimer);
clearInterval(leftTimer);
clearInterval(rightTimer);
while(grid.firstChild) {
grid.firstChild.remove();
}
gameEndMessage.innerHTML = "Game Over <br/> Score: " + score;
}
function fall() {
isJumping = false;
clearInterval(upTimer);
downTimer = setInterval(function () {
doodlerBottomSpace -= 5;
doodler.style.bottom = doodlerBottomSpace + 'px';
if(doodlerBottomSpace == 0) {
gameOver();
}
platformArray.forEach(platform => {
if((doodlerBottomSpace >= platform.bottom &&
doodlerBottomSpace <= platform.bottom + 10) && ((doodlerLeftSpace + 60) >= platform.left &&
doodlerLeftSpace <= (platform.left + 80)) && !isJumping) {
startPoint = doodlerBottomSpace;
jump();
}
})
}, 30);
}
function control(e) {
if(e.key == "ArrowLeft") {
moveLeft();
}
if(e.key == "ArrowRight") {
moveRight();
}
if(e.key == "ArrowUp") {
moveStraight();
}
}
function jump() {
isJumping = true;
clearInterval(downTimer);
upTimer = setInterval(function () {
doodlerBottomSpace += 20;
doodler.style.bottom = doodlerBottomSpace + 'px';
if(doodlerBottomSpace > startPoint + 300 || doodlerBottomSpace > gridHeight) {
fall();
}
}, 20);
}
function moveLeft() {
clearInterval(rightTimer);
isRight = false;
isLeft = true;
leftTimer = setInterval(function () {
if (doodlerLeftSpace >= 0) {
doodlerLeftSpace -= 5;
doodler.style.left = doodlerLeftSpace + 'px';
}
else {
moveRight();
}
}, 20)
}
function moveRight() {
clearInterval(leftTimer);
isLeft = false;
isRight = true;
rightTimer = setInterval(function() {
if(doodlerLeftSpace <= gridWidth - 60) { //grid width minus doodler width, to keep the doodler within the grid
doodlerLeftSpace += 5;
doodler.style.left = doodlerLeftSpace + 'px';
}
else {
moveLeft();
}
}, 20)
}
function moveStraight() {
if(doodlerBottomSpace > 20) {
doodlerBottomSpace += 10;
doodler.style.bottom = doodlerBottomSpace + 'px';
}
clearInterval(rightTimer);
clearInterval(leftTimer);
isLeft = false;
isRight = false;
}
function start() {
if(!isGameOver) {
createPlatform();
createDoodler();
setInterval(movePlatforms, 20);
jump();
document.addEventListener("keyup", control);
}
}
start();