-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
205 lines (158 loc) · 4.97 KB
/
script.js
File metadata and controls
205 lines (158 loc) · 4.97 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//Run Start
var runStart = 0;
//Sounds Variables
var runSound = new Audio("run.mp3");
runSound.loop = true;
var jumpSound =new Audio("jump.mp3");
var deadSound = new Audio("dead.mp3");
var gameOverSound = new Audio("Cipher2.mp3");
//Key Check Function
function keyCheck(event){
//Enter Key
if(event.which==13){
//Start Screen Hidden
document.getElementById("startScreen").style.visibility = "hidden";
//RunWorker
if(runWorkerId==0){
runWorkerId = setInterval(run, 100);
runStart = 1;
runSound.play();
backgroundWorkerId = setInterval(moveBackground, 100);
scoreWorkerId = setInterval(updateScore, 100);
blockWorkerId = setInterval(createBlock, 1000);
moveBlockWorkerId = setInterval(moveBlocks, 100);
}
}
//Space Key
if(event.which==32){
if(runStart==1){
if(jumpWorkerId==0){
clearInterval(runWorkerId);
runSound.pause();
runWorkerId = -1;
jumpWorkerId = setInterval(jump, 100);
jumpSound.play();
}
}
}
}
//Run Function >>===> globle variables
var player = document.getElementById("player");
var runImageNumber = 1;
var runWorkerId = 0;
//Run Function
function run(){
runImageNumber++;
if(runImageNumber == 11){
runImageNumber = 1;
}
player.src = "Run/Run ("+runImageNumber+").png";
}
//Jump Function >>===> globle variables
var jumpImageNumber = 1;
var jumpWorkerId = 0;
var playerMarginTop = 550;
//Jump Function
function jump(){
jumpImageNumber++;
if(jumpImageNumber<=6){
playerMarginTop = playerMarginTop - 40;
player.style.marginTop = playerMarginTop + "px";
}
if(jumpImageNumber>=7){
playerMarginTop = playerMarginTop + 40;
player.style.marginTop = playerMarginTop + "px";
}
if(jumpImageNumber == 11){
jumpImageNumber = 1;
clearInterval(jumpWorkerId);
runWorkerId = setInterval(run, 100);
runSound.play();
jumpWorkerId = 0;
}
player.src = "Jump/Jump"+jumpImageNumber+".png";
}
//Move Background Function >>===> globle variables
var background = document.getElementById("background");
var backgroundX = 0;
var backgroundWorkerId = 0;
//Move Background Function
function moveBackground(){
backgroundX = backgroundX - 20;
background.style.backgroundPositionX = backgroundX + "px";
}
//update Score Function >>===> globle variables
var score = document.getElementById("score");
var newScore = 0;
var scoreWorkerId = 0;
//update Score Function
function updateScore(){
newScore++;
score.innerHTML = newScore;
}
//Create Block Function >>===> globle variables
var blockMarginLeft = 0;
var blockWorkerId = 0;
var blockId = 1;
//Create Block Function
function createBlock(){
var block = document.createElement("div");
block.className = "block";
block.id = "block" + blockId;
blockId++;
//Random creation of blocks
var gap = Math.random()*(1000-400) + 400;
blockMarginLeft = blockMarginLeft + gap
block.style.marginLeft = blockMarginLeft + "px";
background.appendChild(block);
}
//Move Blocks Function >>===> globle variables
var moveBlockWorkerId = 0;
//Move Blocks Function
function moveBlocks(){
for(var i = 1; i <= blockId; i++){
var currentBlock = document.getElementById("block" + i);
var currentMarginLeft = currentBlock.style.marginLeft;
var newMarginLeft = parseInt(currentMarginLeft) - 20;
currentBlock.style.marginLeft = newMarginLeft + "px";
//alert(newMarginLeft);//160 - 40
if(newMarginLeft<160){
if(newMarginLeft>40){
if(playerMarginTop<=550){
if(playerMarginTop>510){
clearInterval(runWorkerId);
clearInterval(jumpWorkerId);
runSound.pause();
jumpWorkerId = -1;
clearInterval(backgroundWorkerId);
clearInterval(blockWorkerId);
clearInterval(moveBlockWorkerId);
clearInterval(scoreWorkerId);
deadWorkerId = setInterval(dead, 100);
deadSound.play();
}
}
}
}
}
}
//Dead Image Function >>===> globle variables
var deadImageNumber = 1;
var deadWorkerId = 0;
//Dead Image Function
function dead(){
deadImageNumber++;
if(deadImageNumber == 10){
clearInterval(deadWorkerId);
runSound.pause();
player.style.marginTop = "550px";
document.getElementById("gameOver").style.visibility = "visible";
gameOverSound.play();
document.getElementById("endST").innerHTML = "Your Score - " + newScore;
}
player.src = "Dead/Dead "+deadImageNumber+".png";
}
//reload Function
function reload(){
location.reload();
}