-
Notifications
You must be signed in to change notification settings - Fork 14
/
spaceInvaders_stable - 2 - Copy.html
405 lines (367 loc) · 10.5 KB
/
spaceInvaders_stable - 2 - 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
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
<!DOCTYPE html>
<html>
<head>
<title>stable-2</title>
<meta charset="utf-8">
<link type="text/css" rel="stylesheet" href="bootstrap/css/bootstrap.css" />
<script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
<style>
body{
background: #000000;
}
.container-fluid{
padding: 0;
}
.col-md-9{
height: 658px;
padding: 0;
}
.glyphicon-heart{
color:red;
font-size: 30px;
}
#tab
{
border-radius: none;
background-color:grey;
color: white;
border: none;
}
canvas{
margin-top: 5%;
background-image: url('images/space-mod.jpg');
border: 10px ridge grey;
}
.style
{
font-size: 30px;
color: white;
}
.styles
{
font-size: 25px;
color: green;
}
</style>
</head>
<body id="bhody">
<div class="container-fluid">
<nav class="navbar navbar-inverse navbar-fixed-top" style="padding:1%">
<div class="container-fluid">
<div class="navbar-header">
</div>
<table class="nav navbar-nav">
<tr>
<td style="border: none;" class="style">Player Name: </td><td style="border: none;" class="styles" id="playerName" >Anonymous </td>
<td style="border: none;" class="style" > Level:</td><td style="border: none;" id="level" class="styles">1 </td>
<td style="border: none;" class="style" >Score:</td><td style="border: none;" id="score" class="styles">0 </td>
<td style="border: none;" class="style" >High Score:<td style="border: none;" id="highScore">0 </td>
<td style="border: none;" ><span class="glyphicon glyphicon-heart"></span></td><td style="border: none;" id="live" class="style">3</td>
</tr>
</table>
</div>
</nav>
<div class="col-md-9">
<div class="canv">
<canvas id="myCanvas" width="1326" height="550" style="position: fixed;"></canvas>
</div>
</div>
<div class="col-md-3">
</div>
</div>
<div id="myModal" class="modal fade" role="dialog" style="margin-top:10%;position:absolute;max-width:50%;margin-left: 20%;padding:5%">
<div class="container modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<span class="input-group">
<span class="input-group-addon">
<span class="glyphicon glyphicon-user" style="color: green"></span></span>
<div class="modal-title" style="color:black;">
<input type="text" name="" class="form-control" placeholder="specify your identity:" disabled=""
style="font-size: 20px;font-family: verdana;">
</div>
</span>
</div>
<div class="modal-body">
<!--<p style="color:green;">NEXT LEVEL!!!!.... </p>-->
<span class="input-group">
<span class="input-group-addon">
<!--<span class="glyphicon glyphicon-user"></span>-->
<span style="font-family:'Arial Black', Gadget, sans-serif;color: green"><em>CAPTAIN</em></span>
</span>
<input type="text" name="" class="form-control" id="Name" style="font-family:'Arial Black', Gadget, sans-serif;padding: 4%;">
</span>
</div>
</div>
</div>
</div>
<script>
var canvas;
var ctx;
var width = 1326;
var height = 550;
//enemy variables
var enemy_x = 40;
var enemy_y = -130;
var enemy_width = 52;
var enemy_height = 112;
var enemyNo = 12;
var enemyArray = [];
var ppf = 1;
//shuttle variables
var shuttle_width = 86;
var shuttle_height = 148;
var shuttle_x = (width/2)-(shuttle_width/2);
var shuttle_y = height-shuttle_height-30;
var sppf = 4;
//buttons
var rightPressed = false;
var leftPressed = false;
var upPressed = false;
var downPressed = false;
var bulletArray=[];
//sound
var explode = new Audio('sound/explosion.mp3');
var laser = new Audio('sound/laserr.mp3');
var background = new Audio('sound/background.mp3');
var lose = new Audio('sound/lose.mp3');
var scoreBoard;
//var dead = false;
var livesLeft = document.getElementById('live');
var playLevel = document.getElementById("level");
var enemy_reset_x = 50;
var nameEntered = document.getElementById('playerName');
//mayo
var rectX =170;
for(var i=0; i<enemyNo; i++){
enemyArray.push([enemy_x, enemy_y, enemy_width, enemy_height, ppf]);
enemy_x +=enemy_width+65;
}
canvas = document.getElementById("myCanvas");
ctx = canvas.getContext("2d");
ctx.fillStyle='#FFF000'
ctx.rect(rectX,250,670,100);
$('#bhody').keydown(function(event)
{
if (event.which == 13) {
if(ctx.isPointInPath(rectX, 250))
{
rectX-=50;
var sss=$('#myModal').modal({backdrop:'static'});
//sss.slideToggle(800)
begin();
setInterval(home,20);
}
}
})
function message()
{
canvas = document.getElementById("myCanvas");
ctx = canvas.getContext("2d");
ctx.font="70px Tahoma, Geneva, sans-serif";
ctx.fillStyle = "white";
ctx.textAlign = "center";
ctx.fillText("Press Enter To Begin", canvas.width/2, canvas.height/2);
}
function begin(){
background.loop=true;
background.play();
//alert(document.getElementById('Name'));
$('#myModal').keydown(function(event)
{
if (event.which == 13) {
if(document.getElementById('Name').value == '')
{
//alert('your name pls...')
nameEntered.innerHTML = 'Anonymous'
canvas = document.getElementById("myCanvas");
ctx = canvas.getContext("2d");
shuttleImg = new Image();
shuttleImg.src = 'images/shuttle.png';
enemyImg = new Image();
enemyImg.src = 'images/enemy-mod.png';
scoreBoard= document.getElementById("score")
scoreBoard.innerHTML = 0;
document.addEventListener('keydown', keyDownHandler, false);
document.addEventListener('keyup', keyUpHandler, false);
$('#myModal').modal('hide');
}
else
{
nameEntered.innerHTML ='captain ' +document.getElementById('Name').value;
canvas = document.getElementById("myCanvas");
ctx = canvas.getContext("2d");
shuttleImg = new Image();
shuttleImg.src = 'images/shuttle.png';
enemyImg = new Image();
enemyImg.src = 'images/enemy-mod.png';
scoreBoard= document.getElementById("score")
scoreBoard.innerHTML = 0;
document.addEventListener('keydown', keyDownHandler, false);
document.addEventListener('keyup', keyUpHandler, false);
$('#myModal').modal('hide');
}
}
})
}
function keyDownHandler(e){
if(e.keyCode==39){
rightPressed=true;
}else if(e.keyCode==37){
leftPressed=true;
}
if(e.keyCode == 38){
upPressed = true;
}else if(e.keyCode == 40){
downPressed = true
}
if(e.keyCode == 32){
bulletArray.push([shuttle_x+(shuttle_width/2)-3, shuttle_y-34, 5,30]);
laser.play();
}
}
function keyUpHandler(e){
if(e.keyCode==39){
rightPressed=false;
}else if(e.keyCode==37){
leftPressed=false;
}
if(e.keyCode==38){
upPressed=false;
}else if(e.keyCode==40){
downPressed = false;
}
}
function moveShuttle(){
if(rightPressed && shuttle_x<(width-shuttle_width)){
shuttle_x +=sppf;
}else if(leftPressed && shuttle_x>0){
shuttle_x -=sppf;
}
if(upPressed && shuttle_y>height/4){
shuttle_y -=sppf;
}else if(downPressed && shuttle_y<(height-shuttle_height)){
shuttle_y +=sppf;
}
}
function drawShuttle(){
ctx.drawImage(shuttleImg,shuttle_x,shuttle_y);
moveShuttle();
}
function drawBullet(){
for(var i=0; i<bulletArray.length; i++){
ctx.fillStyle = "red";
ctx.fillRect(bulletArray[i][0], bulletArray[i][1], bulletArray[i][2], bulletArray[i][3]);
if(bulletArray[i][1]>-35){
bulletArray[i][1] -= 10;
}
}
}
function hitBullet(){
for(var i=0; i<bulletArray.length; i++){
for(var j=0; j<enemyArray.length; j++){
if(bulletArray[i][0]>=enemyArray[j][0] && bulletArray[i][0]<=enemyArray[j][2]+enemyArray[j][0] && bulletArray[i][1]<=enemyArray[j][1]+enemyArray[j][2] && bulletArray[i][1]>=enemyArray[j][1]){
//remove = true;
enemyArray.splice(j,1);
enemyArray.push([(Math.random()*800), -130, enemy_width, enemy_height,ppf]);
//if(remove==true){
bulletArray.splice(i,1);
//remove=false;
scoreBoard.innerHTML=parseInt(scoreBoard.innerHTML)+5;
//}
}
}
}
}
function shuttleCollide(){
for(var i=0; i<enemyArray.length; i++){
if(shuttle_x>enemyArray[i][0] && shuttle_x+22<=enemyArray[i][0]+enemy_width && shuttle_y+40<enemyArray[i][1]+enemyArray[i][3] && shuttle_y+shuttle_height>enemyArray[i][1]){
checkLife();
}else if(shuttle_x+shuttle_width-20>enemyArray[i][0]&&shuttle_x<enemyArray[i][0] && shuttle_y+40<enemyArray[i][1]+enemyArray[i][3] && shuttle_y+shuttle_height>enemyArray[i][1]){
checkLife();
}
//head-on collision
}
}
function checkLife(){
highScore = document.getElementById("highScore");
if(livesLeft.innerHTML>1){
explode.loop =false;
explode.play();
livesLeft.innerHTML -=1;
repositioning();
}else if(livesLeft.innerHTML==1){
livesLeft.innerHTML = 0;
//location.reload();
livesLeft.innerHTML=3;
lose.play();
repositioning();
if(scoreBoard.innerHTML>highScore.innerHTML){
highScore.innerHTML = scoreBoard.innerHTML;
}
scoreBoard.innerHTML=0; //bug;
}
}
function repositioning(){
shuttle_x = (width/2)-(shuttle_width/2);
shuttle_y = height-shuttle_height-30;
for (var i = 0; i < enemyArray.length; i++) {
enemyArray[i][0]+=0;
enemyArray[i][1] = -130;
enemyArray[i][2] = 52;
enemyArray[i][3] = 112;
enemyArray[i][4]=1;
}
}
function clearCanvas(){
ctx.clearRect(0,0, width, height);
}
function drawEnemy(){
for(var i=0; i<enemyArray.length; i++){
ctx.drawImage(enemyImg, enemyArray[i][0], enemyArray[i][1]);
}
}
function moveEnemy(){
for(var i=0; i<enemyArray.length; i++){
if(enemyArray[i][1] < height){
enemyArray[i][1] += enemyArray[i][4];
}else if(enemyArray[i][1] > height-1){
enemyArray[i][1] = -130;
}
}
}
function level(){
if(scoreBoard.innerHTML>=1000 && scoreBoard.innerHTML<1800){
playLevel.innerHTML = 2;
ppf=2;
sppf=5
}else if(scoreBoard.innerHTML>=1800){
playLevel.innerHTML = 3;
ppf=5;
sppf=7;
}else{
playLevel.innerHTML = 1;
ppf=1;
sppf=4;
}
}
function home(){
clearCanvas();
//if(dead==false){
drawShuttle();
drawEnemy();
moveEnemy();
drawBullet();
hitBullet();
shuttleCollide();
level();
//}
}
// setInterval(home,20);
window.onload = message();
</script>
<!--<script src="bootstrap-3.3.7-dist/bootstrap-3.3.7-dist/jquery-3.1.1.js"></script>-->
<!--<script type="text/javascript" src="bootstrap-3.3.7-dist/bootstrap-3.3.7-dist/js/bootstrap.js"></script>-->
</body>
</html>