-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
289 lines (257 loc) · 7.98 KB
/
Copy pathmain.js
File metadata and controls
289 lines (257 loc) · 7.98 KB
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
/*-----------ONLOAD INITIALIZATION-----------*/
window.onload = function(){
var c = document.querySelector("canvas");
var canvas = document.querySelector("canvas");
c.width = innerWidth;
c.height = innerHeight;
c = c.getContext("2d");
/*-----------MOUSE/TOUCH & CONTROLS-----------*/
//mouse and touch objects
function startGame(){
mouse = {
x: innerWidth/2,
y: innerHeight-33
};
touch = {
x: innerWidth/2,
y: innerHeight-33
};
//event listener for mouse object
canvas.addEventListener("mousemove", function(event){
mouse.x = event.clientX;
//mouse.y = event.clientY;
});
//eventListener for touch object
canvas.addEventListener("touchmove", function(event){
var rect = canvas.getBoundingClientRect();
var root = document.documentElement;
var touch = event.changedTouches[0];
var touchX = parseInt(touch.clientX);
var touchY = parseInt(touch.clientY) - rect.top - root.scrollTop;
event.preventDefault();
mouse.x = touchX;
//mouse.y = touchY;
});
/*-----------GAME VARIABLES-----------*/
//player
var player_width = 32;
var player_height = 32;
var playerImg = new Image();
var score = 0;
var health = 100;
function choosePlayer(){
var orangeShip = "https://image.ibb.co/n8rayp/rocket.png";
var blueShip = "https://image.ibb.co/dfbD1U/heroShip.png";
var userInput = prompt("🚀SELECT BATTLESHIP!🚀\n1 is for orange and 2 is for blue ship", 1);
if(userInput==1){
playerImg.src = orangeShip;
}
else if(userInput==2){
playerImg.src = blueShip;
}
else{
playerImg.src = orangeShip;
}
}choosePlayer();
//bullet array
var _bullets = []; //array to hold n bullets
var bullet_width = 6;
var bullet_height = 8;
var bullet_speed = 8;
//enemy array
var _enemies = []; //array to hold n enemies
var enemyImg = new Image();
enemyImg.src = "https://image.ibb.co/bX9UuU/ufo_1.png"; //"https://image.ibb.co/gi6ZpU/ufo.png";
var enemy_width = 32;
var enemy_height = 32;
//health array
var _healthkits = []; //array to hold n health kits
var healthkitImg = new Image();
healthkitImg.src = "https://image.ibb.co/iTrjuU/hospital.png"; //"https://image.ibb.co/gFvSEU/first_aid_kit.png";
var healthkit_width = 32;
var healthkit_height = 32;
//sounds
var shot = new Audio();
shot.src = "https://www.dropbox.com/s/w70c8hyryak6w40/Laser-SoundBible.com-602495617.mp3?dl=0";
/*-----------GAME OBJECTS-----------*/
//Player object
function Player(x, y, width, height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.draw = function(){
c.beginPath();
c.drawImage(playerImg, mouse.x-player_width, mouse.y-player_height); //draw player and center cursor
};
this.update = function(){
this.draw();
};
}
//Bullet object
function Bullet(x, y, width, height, speed){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.speed = speed;
this.draw = function(){
c.beginPath();
c.rect(this.x, this.y, this.width, this.height);
c.fillStyle = "#fff";
c.fill();
c.stroke();
};
this.update = function(){
this.y -= this.speed;
this.draw();
};
}
//Enemy object
function Enemy(x, y, width, height, speed){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.speed = speed;
this.draw = function(){
c.beginPath();
c.drawImage(enemyImg, this.x, this.y);
};
this.update = function(){
this.y += this.speed;
this.draw();
};
}
//Health kit object
function Healthkit(x, y, width, height, speed){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.speed = speed;
this.draw = function(){
c.beginPath();
c.drawImage(healthkitImg, this.x, this.y);
};
this.update = function(){
this.y += this.speed;
this.draw();
};
}
/*-----------_new OBJECT-----------*/
//draw Player
var __player = new Player(mouse.x, mouse.y, player_width, player_height);
//draw n enemies into enemies array
function drawEnemies(){
for (var _ = 0; _<4; _++){ //enemy with random x axis, -32 as y axis, enemy_width, enemy_height, random speed
var x = Math.random()*(innerWidth-enemy_width);
var y = -enemy_height; //-height to draw above canvas for smooth income
var width = enemy_width;
var height = enemy_height;
var speed = Math.random()*4.5;
var __enemy = new Enemy(x, y, width, height, speed);
_enemies.push(__enemy); //push enemy to my array of enemies
}
}setInterval(drawEnemies, 1234);
//draw health kits
function drawHealthkits(){
for (var _ = 0; _<1; _++){ //health with random x axis, -32 as y axis, healthkit_width, healthkit_height, random speed
var x = Math.random()*(innerWidth-enemy_width);
var y = -enemy_height; //-height to draw above canvas for smooth income
var width = healthkit_width;
var height = healthkit_height;
var speed = Math.random()*2.6;
var __healthkit = new Healthkit(x, y, width, height, speed);
_healthkits.push(__healthkit); //push healthkit to my array of healthkits
}
}setInterval(drawHealthkits, 15000);
//draw bullet
//var __bullet = new Bullet(mouse.x-bullet_width/2, mouse.y-player_height, bullet_width, bullet_height, bullet_speed);
//fire bullet function
function fire(){ //fire bullet from mouse.x on x axis, y axis, width, height, speed
for (var _ = 0; _<1; _++){
var x = mouse.x-bullet_width/2;
var y = mouse.y-player_height;
var __bullet = new Bullet(x, y, bullet_width, bullet_height, bullet_speed);
_bullets.push(__bullet); //push bullet to my array of bullets
//shot.play();
}
}setInterval(fire, 200);
//event listener for fire function
canvas.addEventListener("click", function(){
//fire();
});
/*-----------COLLISION DETECTION-----------*/
function collision(a,b){
return a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y;
}
/*-----------SCORE-----------*/
c.fillStyle = "white";
c.font = "1em Arial";
/*-----------DIRTY ERROR HANDLING-----------*/
function stoperror() {
return true;
}
window.onerror = stoperror;
/*-----------GAME LOOP-----------*/
function animate(){
requestAnimationFrame(animate); //animate
c.beginPath(); //begin
c.clearRect(0,0,innerWidth,innerHeight); //clear canvas
c.fillText("Health: " + health, 5, 20); //health
c.fillText("Score: " + score, innerWidth-100, 20); //score
/*-----------__player, __bullet, __enemy update, __healthkit update-----------*/
//update _player
__player.update();
//update bullets from bullets array
for (var i=0; i < _bullets.length; i++){
_bullets[i].update();
if (_bullets[i].y < 0){
_bullets.splice(i, 1);
}
}
//update enemies from enemies array
for (var k=0; k < _enemies.length; k++){
_enemies[k].update();
//if enemy is below canvas, delete it
if(_enemies[k].y > innerHeight){
_enemies.splice(k, 1);
health -= 10;
if(health == 0){
alert("You DIED!\nYour score was "+score);
startGame();
}
}
}
//loop over both enemies and bullets to detect collisions
for(var j = _enemies.length-1; j >= 0; j--){
for(var l = _bullets.length-1; l >= 0; l--){
if(collision(_enemies[j], _bullets[l])){
_enemies.splice(j, 1);
_bullets.splice(l, 1);
score++;
}
}
}
//draw healthkits
for(var h=0; h < _healthkits.length; h++){
_healthkits[h].update();
}
//loop over both healthkits and bullets to detect collisions
for(var hh = _healthkits.length-1; hh >= 0; hh--){
for(var hhh = _bullets.length-1; hhh >= 0; hhh--){
if(collision(_healthkits[hh], _bullets[hhh])){
_healthkits.splice(hh, 1);
_bullets.splice(hhh, 1);
health += 10;
}
}
}
} //animate func
animate();
}startGame();//startGame function starts/restarts game
}; //end of onload func