-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdesertRunner.js
More file actions
96 lines (79 loc) · 2.46 KB
/
desertRunner.js
File metadata and controls
96 lines (79 loc) · 2.46 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
const Game = function() {
this.jumpHeight = 170;
this.obstacleStep = 10;
this.score = 0;
};
Game.prototype.countScore = function() {
return ++this.score;
};
Game.prototype.isGameOver = function(obstacleRight, runnerHeight) {
return obstacleRight == 1160 && runnerHeight < 170;
};
Game.prototype.moveCactus = function(previousPos) {
return previousPos + this.obstacleStep;
};
Game.prototype.jumpPlayer = function(previousHeight) {
return previousHeight + this.jumpHeight;
};
Game.prototype.stopObstacle = function () {
return 1160;
};
Game.prototype.stopRunner = function () {
return 0;
};
let game = new Game();
const moveObstacle = function(obstacle) {
let obstaclePos = +obstacle.style.right.slice(0,-2);
obstacle.style.right = game.moveCactus(obstaclePos) + "px";
if (obstaclePos >= 1350) {
obstacle.style.right = "0px";
};
};
const jumpPlayer = function(runner,obstacle) {
let playerHeight = +runner.style.bottom.slice(0, -2);
let obstaclePos = +obstacle.style.right.slice(0,-2);
if (event.keyCode == 32 && !game.isGameOver(obstaclePos,playerHeight)) {
runner.style.bottom = game.jumpPlayer(playerHeight) + "px";
setTimeout(function() {
runner.style.bottom = "0px";
}, 500);
};
};
const displayScore = function(score) {
score.innerHTML = game.countScore();
};
const stopObstacle = function(obstacle){
obstacle.style.right = game.stopObstacle(obstacle) + "px";
};
const finishGame = function(obstacle,runner) {
let restartButton = document.getElementById("restartButton");
restartButton.style.visibility = "visible";
document.getElementById("loosingNote").innerHTML = "opps! You lost";
stopObstacle(obstacle);
};
const executeGame = function() {
let obstacle = document.getElementById('obstacle');
let runner = document.getElementById('runner');
let score = document.getElementById('score');
let obstacleRight = +obstacle.style.right.slice(0,-2);
let runnerHeight = +runner.style.bottom.slice(0,-2);
moveObstacle(obstacle);
if (game.isGameOver(obstacleRight,runnerHeight)) {
finishGame(obstacle,runner);
} else {
displayScore(score);
};
};
const runGame = function() {
setInterval(executeGame, 50);
};
const restartGame = function(){
location.reload();
};
const startGame = function() {
document.getElementById("runner").src = "runnerRunning.gif";
let startButton = document.getElementById("startButton");
startButton.style.visibility = "hidden";
runGame();
setInterval(runGame, 10000);
};