-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.html
445 lines (395 loc) · 12 KB
/
3.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<html>
<head>
<script>
/*
Work For Chukka
Do scoring -- can be done on the basis of km traveled ie pixels here.. or no of icebergs passed by the ship.. former is easier
Make collision proper.. if you can come up with a triangle mechanism to check for iceberg collision
Add boundary collision.. make set interval function in ship ant call interval every 100 miliseconds
also write start screen . and game over screen.
Work for Rash
add lines to canvas making wawes
like /\
//\\
///\\\
add one line it will aouto matically replicate
learn on phone inputs and phone porting
*/
score=0;
ship={
width:50,
height:120,
xpeed:0,
spos:360-25,
hor:860,
img:new Image(),
blank:new Image(),
calx:function(){
this.spos+=this.xpeed;
},
click:function(event){
if(event.pageX<360)
ship.left();
else ship.right();
},
left:function(){
if(ship.xpeed<1) ship.xpeed=3*(stream.speed*stream.handling/4);
else ship.xpeed+=2*(stream.speed*stream.handling/4);//*(2/stream.speed);
},
right:function(){
if(ship.xpeed>-1) ship.xpeed=-3*(stream.speed*stream.handling/4);
else ship.xpeed-=2*(stream.speed*stream.handling/4);//*(2/stream.speed);
},
edgecol:function(){
//console.log(ship.spos);
if(ship.spos<0 || ship.spos >720)
//document.write('Game Over');
gameover();
}
}
stream={
speed:10,
handling:1,
speed:1,
mode:0,
roll:function() {
var exShipPosition = ship.spos;
ship.calx();
//ctx.drawImage(c, exShipPosition, 360,25,60+stream.speed);
ctx.drawImage(c, exShipPosition, 0, ship.width, 5, exShipPosition, ship.hor, ship.width,ship.height );
ctx.drawImage(c, 0, 0, w, h-stream.speed, 0, stream.speed, w, h-stream.speed);
//ctx.fillRect(ship.spos,360,25,60+stream.speed);
ctx.drawImage(ship.img, ship.spos, ship.hor,ship.width,ship.height);
//ctx.fillRect(ship.spos, ship.hor,ship.width,ship.height );//debug!!!
},
gravity:function(){
setInterval(function(){
if(ship.spos<360-ship.width/2) ship.xpeed-=1;
else if(ship.spos>360-ship.width/2) ship.xpeed+=1;
},330*(2/stream.speed))
}
}
function wave(vert){
this.speed=0;//random;
this.vert=vert;
this.hor=20;
//this.arr=[this.vert,this.hor];
this.appear=function ()
{
//console.log(vert);
//console.log(this.vert+','+this.hor)
ctx.drawImage(ripple,this.vert,this.hor,50,50);
};
}
wave.create=function()
{
var a=new wave(Math.floor((Math.random()*(720-50))+1));
a.appear();
}
function iceberg(vert){
this.speed=Math.floor((Math.random()*10)+1);;//random;
this.vert=vert;
this.height=Math.floor(Math.random()*(41)+60);
this.width= Math.floor(Math.random()*(41)+60);
this.hor=40;
this.flag=true;
//this.arr=[this.vert,this.hor];
this.appear=function (){
//console.log(vert);
//console.log(this.vert+','+this.hor)
ctx.drawImage(iceberg.img,this.vert,this.hor,this.width,this.height);
//ctx.fillRect(this.vert,this.hor,this.width,this.height);//debug!!!
};
}
iceberg.create=function(){
var a=new iceberg(Math.floor((Math.random()*(720-50))+1));
a.appear();
setInterval(function (){ iceberg.movecheck.call(a);},33);
//a.movedown();
}
iceberg.movecheck=function(){
var ob=this;
//console.log("klklkl->",ob[0]+','+ob[1])
var flag=iceberg.collision.call(ob);
ob.hor+=stream.speed;
}
iceberg.collision=function(){
var ob=this;
var vert=ob.vert;
var hor=ob.hor;
//console.log('coll>',vert+','+hor+':'+ship.spos+','+ship.hor);
if(!ob.flag) return false;
if(hor>(ship.hor+ship.height) && ob.flag)
{
scoreboard();
ob.flag=false;
//alert();
}
if((vert>(ship.spos+ship.width)) || ((vert+ob.width)<ship.spos))
{return false;}
//alert("thi")
if( ((hor+ob.height) < ship.hor) || (hor > (ship.hor+ship.height)))
{return false;}
var m=(ob.height*2)/(ob.width);
var vd = ob.height+ob.hor-ship.height-ship.hor
var hd = ship.width + ship.spos-ob.vert;
console.log(m,vd,hd,vd/hd)
if(m<vd/hd) {return false;}
var hd = ob.vert+ob.width-ship.spos;
if(m<vd/hd) {return false;}
// var t=-(ship.hor+ship.height-ob.hor+ob.height)/ob.height;
//if((sip.vert+ship.width)<(ob.vert+t*ob.width.width/2)) return false;
//document.write('Game Over');
//alert("G");
gameover();
//window.open("gameover.html");
return true;
}
function gameover()
{
for(var i=0;i<score+20;i++)
clearInterval(i);
cotx=c.getContext("2d");
grd = cotx.createLinearGradient(100, 600, 520, 100);
// light blue
grd.addColorStop(0, '#FFE87C');
// dark blue
grd.addColorStop(1, '#EDDA74');
cotx.fillStyle = grd;
cotx.font = "40px Calibri";
cotx.fillRect(100, 600, 570, 280);
cotx.strokeText("GAME OVER",270,650);
cotx.strokeText("Score:",270,720);
cotx.strokeText(""+score,400,720);
if(localStorage.getItem('highscore')){
var highscore = localStorage.getItem('highscore');
if(highscore<=score)
{ //localStorage.highscore=score;
try {
localStorage.setItem("highscore", score);
} catch (e) {
if (e == QUOTA_EXCEEDED_ERR) {
alert('Quota exceeded!');
}
}
}
}else{
localStorage.setItem("highscore", score);
}
cotx.strokeText("High Score: "+localStorage.getItem("highscore"),130,780);
cotx.strokeText("Play Again",400,780);
cotx.strokeText("Submit Score",130,840);
cotx.strokeText("See High Scores",400,840);
c.addEventListener("mousedown",refresh,true);
}
function refresh(event)
{
//alert("tada");
if(event.pageX>400 && event.pageX<600 && event.pageY>780 && event.pageY<820)
{
window.location.reload();
}
else if(event.pageX>400 && event.pageX<670 && event.pageY>840 && event.pageY<900)
{
getHighScores();
}
else if(event.pageX>130 && event.pageX<330 && event.pageY>840 && event.pageY<900 && stream.mode==1)
{
submitScore();
}
else if(event.pageX>130 && event.pageX<330 && event.pageY>840 && event.pageY<900 && stream.mode==0)
{
//submitScore();
alert("scores can be submitted only for challenge games");
window.location.reload();
}
}
function init()
{
c = document.getElementById('myCanvas');
ctx=c.getContext("2d");
//w=screen.width;
//h=screen.height;
h=1280;
w=720;
c.height=h;
c.width=w;
exShipPosition = 120;
ship.img=new Image();
iceberg.img=new Image();
iceberg.img.src="iceberg.png";
ship.img.src='ship-up.png';
ship.blank.src='ship-up-blank.png';
ripple=new Image();
ripple.src="bg1.png";
tyre=new Image();
tyre.src="tyre.png";
if(localStorage.highscore=="undefined")
{
localStorage.setItem("highscore",0);
}
ctx.rect(0, 0, w, h);
grd = ctx.createLinearGradient(0, 0, w, h);
// light blue
grd.addColorStop(0, '#8ED6FF');
// dark blue
grd.addColorStop(1, '#004CB3');
ctx.fillStyle = grd;
ctx.fill();
ctx.drawImage(ship.img, ship.spos, ship.hor,ship.width,ship.height);
c.addEventListener("mousedown",ship.click,false);
}
function gamestart()
{
c = document.getElementById('myCanvas');
ctx=c.getContext("2d");
h=1280;
w=720;
c.height=h;
c.width=w;
exShipPosition = 120;
ship.img=new Image();
iceberg.img=new Image();
iceberg.img.src="iceberg.png";
ship.img.src='ship-up.png';
ship.blank.src='ship-up-blank.png';
iceberg.img.src="iceberg.png";
ship.img.src='ship-up.png';
ripple=new Image();
ripple.src="bg1.png";
tyre=new Image();
tyre.src="tyre.png";
ctx.rect(0, 0, w, h);
grd = ctx.createLinearGradient(0, 0, w, h);
grd.addColorStop(0, '#8ED6FF');
grd.addColorStop(1, '#004CB3');
ctx.fillStyle = grd;
ctx.fill();
ctx.drawImage(ship.img, ship.spos, ship.hor,ship.width,ship.height);
// ctx.drawImage(iceberg.img,50,50);
ctx.font = "40px Calibri";
//ctx.fillRect(100, 600, 570, 280);
ctx.strokeText("Practice: Low",270,650);
ctx.strokeText("Practice: Medium",270,700);
ctx.strokeText("Practice: High",270,750);
ctx.strokeText("CHALLENGE",270,820);
c.addEventListener("mousedown",game,false);
}
function game()
{
if(event.pageX>270 && event.pageX<520 && event.pageY>650 && event.pageY<700)
{
stream.mode=0;
stream.handling=2;
stream.speed=5;
}
else if(event.pageX>270 && event.pageX<520 && event.pageY>700 && event.pageY<750)
{
stream.mode=0;
stream.handling=1;
stream.speed=10;
}
else if(event.pageX>270 && event.pageX<520 && event.pageY>750 && event.pageY<800)
{
stream.mode=0;
stream.handling=1;
stream.speed=15;
}
else if(event.pageX>270 && event.pageX<520 && event.pageY>820 && event.pageY<850)
{
stream.mode=1;
stream.handling=1;
stream.speed=8;
}
else
{
window.location.reload();
}
init();
start();
}
function scoreboard(){
score++;
if(score%5 == 0 && stream.mode==1)
stream.speed=stream.speed+1;
sc=score + " " +stream.speed+ " "+ stream.mode;
document.getElementById('score').innerHTML =sc;
}
function keys(ev)
{
if(ev.keyCode==52||ev.keyCode==97) ship.left();
else if (ev.keyCode==54||ev.keyCode==100) ship.right();
}
function start()
{
setInterval(stream.roll,33);
stream.gravity();
setInterval(iceberg.create,((2000)*(2/stream.speed)));
setInterval(ship.edgecol,33);
setInterval(wave.create,2000);
//setInterval(tyre.create,8000);
}
function submitScore(){
var score = localStorage.getItem('highscore');
var person = prompt("Please Enter Your Name","Chukka");
var email = prompt("Please Enter Your Email ID","[email protected]");
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(xmlhttp.responseText);
}
}
xmlhttp.open("POST","backend.php",true);
xmlhttp.send("todo=post&person="+person+"&email="+email+"&score="+score);
xmlhttp.send();
}
function getHighScores(){
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(xmlhttp.responseText);
}
}
xmlhttp.open("GET","backend.php",true);
xmlhttp.send();
}
</script>
</head>
<body onkeypress="keys(event)" onload="gamestart();" >
<div><span id="score">0</span></div>
<canvas id="myCanvas" width="300" height="500" style="position:absolute;border:1px solid #000000;background-color:#008AE6">
Your browser does not support the HTML5 canvas tag.
</canvas>
<button type="button" onclick="first()">
<img src="ship-up.png" alt="Play" />
<br />PLAY!</button>
<br/>
<!--button onclick="setInterval(stream.roll,33);stream.gravity();setInterval(iceberg.create,2000);">start</button>
<!--button onclick="setInterval(iceberg.create,1000);">Ice please!</button-->
<br/>
<button onclick="ship.left()">left</button>
<button onclick="ship.right()">right</button>
<br/>
<p>Or use num pad 4,6 to play or use .. WASD .. a and d to play</p>
</body>
</html>