-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3 - Copy.html
321 lines (275 loc) · 8.45 KB
/
3 - Copy.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
<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,
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 tyre(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(tyre,this.vert,this.hor,50,50);
};
}
tyre.create=function()
{
var a=new tyre(Math.floor((Math.random()*300)+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, 520, 200);
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);
c.addEventListener("mousedown",refresh,true);
}
function refresh(event)
{
//alert("tada");
if(event.pageX>380 && event.pageX<560)// && event.PageY>780)
{
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 scoreboard(){
score++;
document.getElementById('score').innerHTML =score;
}
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,1);
setInterval(wave.create,2000);
setInterval(tyre.create,8000);
}
</script>
</head>
<body onkeypress="keys(event)" onload="init();start();" >
<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>
<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>