-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
224 lines (195 loc) · 6.04 KB
/
index.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
<html>
<script>
var ctx, cvs, sname="m_score_shooting";
var itv_update, fps=30;
var itv_spawn, spawn_rate, spawn_dir;
var bullet_rate, lives, score;
var enemies=[], bullets=[];
function mRandom(){ return Math.random(); }
function mFloor(i){ return Math.floor(i); }
/* Game Objects */
function Shooter(){
this.img = new Image();
this.img.src = "shooter.png";
this.deg = 0; this.speed = 10;
this.width = 60; this.height = 100;
this.x = (cvs.width-this.width)/2;
this.y = cvs.height-this.height;
}
Shooter.prototype.draw = function(){
ctx.save();
ctx.translate(this.x, this.y);
ctx.translate(this.width/2, this.height/2);
ctx.rotate(this.deg * Math.PI/180);
ctx.drawImage(this.img, -(this.width/2), -(this.height/2));
ctx.restore();
};
Shooter.prototype.shoot = function(){
if(this.locked){ return false; }
bullets.push(new Bullet());
this.locked = true;
setTimeout("shooter.unlock();", 1000/bullet_rate);
}
Shooter.prototype.unlock = function(){
this.locked = false;
}
function Enemy(){
this.radius = 20;
this.x = mFloor((mRandom()*(cvs.width-(2*this.radius)))+this.radius);
this.y = -this.radius;
this.speed = 7; this.alive = true;
diff_x = ((shooter.x+(shooter.width/2)) - this.x);
diff_y = ((spawn_dir*cvs.height) - this.y);
mul = this.speed/Math.sqrt(Math.pow(diff_x, 2) + Math.pow(diff_y, 2));
this.dx = mul * diff_x; this.dy = mul * diff_y;
}
Enemy.prototype.draw = function(){
this.x += this.dx; this.y += this.dy;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, 2*Math.PI, false);
ctx.fillStyle = "#000000";
ctx.fill();
}
Enemy.prototype.checkAlive = function(){
for(var i=0; i<bullets.length; i++){
b = bullets[i];
if(b.alive){
diff = Math.sqrt(Math.pow(this.x - b.x, 2) + Math.pow(this.y - b.y, 2));
if(diff < (this.radius+b.radius)){
this.alive = false;
b.alive = false;
score += 10;
return false;
}
}
}
if(this.y > (cvs.height-120-this.radius+5)){
lives -= 1;
this.alive = false;
}
}
function Bullet(){
this.x = shooter.x + shooter.width/2;
this.y = shooter.y + shooter.height/2;
this.speed = 10; this.radius = 5; this.alive = true;
this.dx = this.speed*Math.sin(shooter.deg * Math.PI/180);
this.dy = -this.speed*Math.cos(shooter.deg * Math.PI/180);
}
Bullet.prototype.draw = function(){
this.x += this.dx; this.y += this.dy;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, 2*Math.PI, false);
ctx.fillStyle = "#DDDDDD";
ctx.fill();
}
/* Game Functions */
function inView(obj){
if(obj.x>=0 && obj.x<cvs.width){
if(obj.y>=0 && obj.y<cvs.height){ return true; }
}
return false;
}
function spawnEnemy(){
enemies.push(new Enemy());
itv_spawn = setTimeout(spawnEnemy, 1000/spawn_rate);
}
function endGame(){
clearTimeout(itv_update);
clearTimeout(itv_spawn);
setTimeout("endGame2('Shooting Game', score);", 3000);
}
function pauseGame(){
clearTimeout(itv_update);
clearTimeout(itv_spawn);
document.onkeypress = null;
cvs.onclick = resumeGame;
}
function resumeGame(){
update(); spawnEnemy();
document.onkeypress = onKeyPress;
cvs.onclick = pauseGame;
}
/* Canvas Functions */
function draw(){
ctx.clearRect(0, 0, cvs.width, cvs.height);
cvs.onclick = pauseGame;
for(var i=0; i<enemies.length; i++){ enemies[i].draw(); }
for(var i=0; i<bullets.length; i++){ bullets[i].draw(); }
shooter.draw();
ctx.fillStyle = "#FFFFFF";
ctx.fillRect(0, cvs.height-120, cvs.width, 5);
ctx.fillStyle = "# ";
ctx.font = "17px Arial";
ctx.fillText("Score: "+score.toString(), 20, 30);
ctx.fillText("Lives: "+lives.toString(), cvs.width-70, 30);
}
function init(){
cvs = document.getElementById("canvas");
cvs.onclick = console.log();
ctx = cvs.getContext("2d");
shooter = new Shooter();
score = 0; lives = 5;
enemies = []; spawn_rate = 1; spawn_dir = 1;
bullets = []; bullet_rate = 15;
update();
spawnEnemy();
}
function update(){
draw();
for(var i=0; i<enemies.length; i++){ enemies[i].checkAlive(); }
if(lives<0){
lives = 0;
endGame();
return false;
}
dead = enemies.filter(function(i){ if(!i.alive){ delete i; } });
enemies = enemies.filter(function(i){ return i.alive; });
dead = bullets.filter(function(i){ if(!i.alive){ delete i; } });
bullets = bullets.filter(function(i){ return i.alive; });
// Difficulty
spawn_rate = Math.floor((score/250)+1);
spawn_dir = Math.floor((score/160)*0.1 + 1);
bullet_rate = (score/240)*-1 + 15;
if(bullet_rate < 10){ bullet_rate = 10; }
itv_update = setTimeout(update, 1000/fps);
}
function onKeyPress(e){
switch(e.keyCode){
case 97: shooter.deg -= shooter.speed; shooter.deg %= 360; break;
case 100: shooter.deg += shooter.speed; shooter.deg %= 360; break;
case 32: shooter.shoot(); break;
}
if( shooter.deg > 90 ){ shooter.deg = 90; }
if( shooter.deg < -90 ){ shooter.deg = -90; }
}
document.onkeypress = onKeyPress;
</script>
<script src="score.js"></script>
<style>
body{
background-color:#BBBBBB;
text-align:center;
}
#canvas{
background-color:#555555;
}
</style>
<head>
<meta title="author" content="Metta Ong" />
<title>Shooting Game</title>
</head>
<body>
<h1 style="margin:10px auto;">Shooting Game</h1>
<div style="width:300;margin:10px auto;text-align:justify;">
Do not let the balls cross the line.<br />
Use 'A' and 'D' to orientate the shooter.<br />
Use 'SPACE' to shoot at the balls.<br />
Click to pause or resume the game.<br />
<p style="margin:5px;text-align:center;font-weight:bold;">CLICK TO START</p>
</div>
<canvas id="canvas" width="300" height="400" onclick="init();"></canvas>
<div style="width:300;margin:5px auto;text-align:right;">
Created By Metta Ong
</div>
</body>
</html>