-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
234 lines (202 loc) · 5.65 KB
/
app.js
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
window.onload = function(){
var c = document.querySelector("canvas");
var canvas = document.querySelector("canvas");
c.width = innerWidth;
c.height = innerHeight;
c = c.getContext("2d");
function startGame(){
mouse = {
x: innerWidth/2,
y: innerHeight-33
};
touch = {
x: innerWidth/2,
y: innerHeight-33
};
canvas.addEventListener("mousemove", function(event){
mouse.x = event.clientX;
});
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;
});
var player_width = 32;
var player_height = 32;
var playerImg = new Image();
var score = 0;
var health = 100;
playerImg.src = "https://image.ibb.co/dfbD1U/heroShip.png";
var _bullets = [];
var bullet_width = 6;
var bullet_height = 8;
var bullet_speed = 10;
var _enemies = [];
var enemyImg = new Image();
enemyImg.src = "https://i.ibb.co/0YgHvmx/enemy-fotor-20230927153748.png"
var enemy_width = 32;
var enemy_height = 32;
var _healthkits = [];
var healthkitImg = new Image();
healthkitImg.src = "https://image.ibb.co/gFvSEU/first_aid_kit.png";
var healthkit_width = 32;
var healthkit_height = 32;
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);
};
this.update = function(){
this.draw();
};
}
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 = "white";
c.fill();
};
this.update = function(){
this.y -= this.speed;
this.draw();
};
}
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();
};
}
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();
};
}
var __player = new Player(mouse.x, mouse.y, player_width, player_height);
function drawEnemies(){
for (var _ = 0; _<4; _++){
var x = Math.random()*(innerWidth-enemy_width);
var y = -enemy_height;
var width = enemy_width;
var height = enemy_height;
var speed = Math.random()*2;
var __enemy = new Enemy(x, y, width, height, speed);
_enemies.push(__enemy);
}
}setInterval(drawEnemies, 1234);
function drawHealthkits(){
for (var _ = 0; _<1; _++){
var x = Math.random()*(innerWidth-enemy_width);
var y = -enemy_height;
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);
}
}setInterval(drawHealthkits, 15000);
function fire(){
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);
}
}setInterval(fire, 200);
canvas.addEventListener("click", function(){
});
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;
}
c.font = "1em Arial";
function stoperror() {
return true;
}
window.onerror = stoperror;
function animate(){
requestAnimationFrame(animate);
c.beginPath();
c.clearRect(0,0,innerWidth,innerHeight);
c.fillStyle = 'white';
c.fillText("Health: " + health, 5, 20);
c.fillText("Score: " + score, innerWidth-100, 20);
__player.update();
for (var i=0; i < _bullets.length; i++){
_bullets[i].update();
if (_bullets[i].y < 0){
_bullets.splice(i, 1);
}
}
for (var k=0; k < _enemies.length; k++){
_enemies[k].update();
if(_enemies[k].y > innerHeight){
_enemies.splice(k, 1);
health -= 10;
if(health == 0){
alert("You DIED!\nYour score was "+score);
startGame();
}
}
}
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++;
}
}
}
for(var h=0; h < _healthkits.length; h++){
_healthkits[h].update();
}
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();
}startGame();
};