-
Notifications
You must be signed in to change notification settings - Fork 0
/
dinasourrunner.html
232 lines (191 loc) · 5.14 KB
/
dinasourrunner.html
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<html> <body>
<canvas style="margin-top:30px;display: block ;padding-left: 0; padding-right: 0; margin-left: auto; margin-right: auto;" id="myCanvas" width="700" height="320"></canvas>
<br>
<div style="text-align: center;"><b ><h2>Made wih love by KRISHNA KANT</h2></b></div>
<br>
<br>
<br>
<br>
<br>
<p style="text-align: center;"><b>#LearnInspireGrow #DinosaurJumpGame. </b></p>
<br>
<br>
<br>
<br>
<br>
<br>
<script>
// Made with love to inspire others by Coding Club India team !
// Step 6
// Add hurdles and sound effects.
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var width = 700;
var height = 320;
var audio = new Audio('https://freesound.org/data/previews/173/173326_2370190-lq.mp3');
function playJumpAudio(){
audio.play();
}
function drawObject(object){
//Find current index of image to be used.
var currentImageIndex = counter%object.images.length;
//Access image from array of images
var currentImage = object.images[currentImageIndex];
//Draw of canvas
try{
ctx.drawImage(currentImage,object.x-(object.width/2),object.y-(object.height/2),object.width,object.height);
} catch(er){}
if(1===object.repeatX){
ctx.drawImage(currentImage,object.width + object.x-(object.width/2),object.y-(object.height/2),object.width,object.height);
}
}
var dragonImages = [];
var dragonImageUrls = ["https://i.imgur.com/dyPMw7K.png","https://i.imgur.com/cO9ZZMC.png","https://i.imgur.com/AJUkc9s.png","https://i.imgur.com/6xIWhPr.png"];
//,"https://i.imgur.com/SCqOXcT.png"];
for( var i=0; i<dragonImageUrls.length ; i++ ){
var image = new Image();
image.src = dragonImageUrls[i];
dragonImages.push(image);
}
var dragon = {};
dragon.images = dragonImages ;
dragon.width = 50;
dragon.height = 50;
dragon.x=100;
dragon.y=280;
dragon.speedX = 0;
dragon.speedY = 0;
var gravity = 2.08;
dragon.move = function(){
//
console.log(this.speedY);
this.speedY = this.speedY + gravity;
this.y = this.y + this.speedY;
if(this.y > 280){
this.y = 280;
this.speedY =0;
}
}
var clouds = [];
var cloudImage = new Image();
cloudImage.src = "https://i.imgur.com/H84RIEb.png";
function getCloud(){
var cloud = {};
cloud.images = [cloudImage] ;
cloud.width = 50;
cloud.height = 30;
cloud.x=width;
cloud.y=30+Math.random()*75;
cloud.isActive = true;
cloud.speedX = -4 -( Math.random()*4);
cloud.speedY = 0;
cloud.move = function(){
this.x = this.x + this.speedX;
if(this.x < -100 ){
this.isActive = false;
}
}
return cloud;
}
var hurdles = [];
var hurdleImage = new Image();
hurdleImage.src = "https://i.imgur.com/9pEv72P.png";
var score = 0;
function getHurdle(){
var hurdle = {};
hurdle.images = [hurdleImage] ;
hurdle.width = 30;
hurdle.height = 70;
hurdle.x=width;
hurdle.y=height - hurdle.height/2 - 15;
hurdle.isActive = true;
hurdle.speedX = -9;
hurdle.speedY = 0;
hurdle.move = function(){
this.x = this.x + this.speedX;
if(this.x < -100 ){
this.isActive = false;
}
}
return hurdle;
}
var landImage = new Image();
landImage.src = "https://i.imgur.com/gL6wPcx.png";
var land = {};
land.width = 1200;
land.height = 12;
land.images = [landImage];
land.x= land.width/2;
land.y= height -25;
land.speedX = -10;
land.speedY = 0;
land.repeatX = 1;
land.move = function(){
this.x = this.x + this.speedX;
if( this.x < -this.width/2 ){
this.x += this.width ;
}
}
var counter = 0;
function update(){
counter++;
score++;
if(counter%30==0){
// Create new cloud and store it in array named clouds
var cloud = getCloud();
clouds.push(cloud);
}
if(counter%100==0){
// Create new hurdle and store it in array named hurdles
var hurdle = getHurdle();
hurdles.push(hurdle);
}
//Clear all backgroud to white
ctx.fillStyle = "#ffffff";
ctx.fillRect(0, 0, width, height);
ctx.font = "30px Arial";
ctx.fillStyle = "#000000";
ctx.fillText("Score: " + parseInt(score/10) , width-180, 50);
dragon.move();
drawObject(dragon);
var cloudsFinal = [];
for(var i = 0; i < clouds.length ; i++){
var cloud = clouds[i];
cloud.move();
drawObject(cloud);
if( cloud.isActive == true){
cloudsFinal.push(cloud);
}
}
clouds = cloudsFinal;
var hurdlesFinal = [];
for(var i = 0; i < hurdles.length ; i++){
var hurdle = hurdles[i];
hurdle.move();
drawObject(hurdle);
if( hurdle.isActive == true){
hurdlesFinal.push(hurdle);
}
}
hurdles = hurdlesFinal;
land.move();
drawObject(land);
}
var isMusicPlaying = false;
document.addEventListener("keydown", function(event) {
if(event.keyCode == 32){
if(dragon.y == 280){
playJumpAudio();
dragon.speedY = -20;
}
}
if(!isMusicPlaying){
isMusicPlaying=true;
var audio = new Audio('https://d1490khl9dq1ow.cloudfront.net/audio/music/mp3preview/BsTwCwBHBjzwub4i4/isotope_fkjfprHu_WM.mp3');
audio.play();
}
});
setInterval(update,50);
</script>
</body>
</html>