-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalien2.html
More file actions
215 lines (185 loc) · 7.07 KB
/
Copy pathalien2.html
File metadata and controls
215 lines (185 loc) · 7.07 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
<html>
<head>
</head>
<body>
<canvas style="margin-top:30px; display:block;padding-left:0;padding-right:0;margin-left:auto;margin-right: auto;"
id ="myCanvas" width="520" height="520"></canvas>
<br>
<marquee>Mission🎖️Helix</marquee>
<marquee>Sukhdev Singh</marquee>
<script type="text/javascript">
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext('2d');
var counter = 0;
/* //old one drawObject function
function drawObject(object){
//find current index of image to be used.
var index = counter % object.images.length;
ctx.drawImage(object.images[index], object.x,object.y,object.width,object.height);
}
*/
// correcting the drawObject function and adding bullets for ships
function drawObject(object){
//find current index of image to be used.
var index = counter % object.images.length;
ctx.drawImage(object.images[index], object.x-(object.width/2),object.y-(object.height/2),object.width,object.height);
}
var HEIGHT = 520;
var bullets = [];
var bulletImage1 = new Image();
bulletImage1.src = ["https://i.imgur.com/dM81aDs.gif"];
var bulletImage2 = new Image();
bulletImage2.src = ["https://i.imgur.com/NyaUjNn.gif"];
function addBullet(x,y){
var bullet = {};
bullet.images = [bulletImage1,bulletImage2];
bullet.x = x;
bullet.y = y;
bullet.width = 50;
bullet.height = 50;
bullet.speedX = 0;
bullet.speedY = 7;
bullet.active = true;
bullet.move = function(){
this.y += this.speedY;
if( this.y >=HEIGHT ){
this.active = false;
}
}
bullets.push(bullet);
}
function drawAndMoveBullets(){
var temp = [];
for(var i=0;i<bullets.length;i++){
bullets[i].move();
drawObject(bullets[i]);
// Only add active bullets
if(bullets[i].active){
temp.push(bullets[i]);
}
}
bullets = temp;
}
// addings ships on top and giving them speed
var WIDTH = 520;
var ships = [];
var shipImages = [];
var shipUrls =["https://i.imgur.com/gLLRj2T.png",
"https://i.imgur.com/ZhshGO4.png",
"https://i.imgur.com/E0wiPJC.png",
"https://i.imgur.com/EjfY1iE.png",
"https://i.imgur.com/t3VGw8g.png",
"https://i.imgur.com/WmsDf2l.png",
"https://i.imgur.com/d6OT3qt.png"];
for (var i=0;i<shipUrls.length;i++){
var shipImage = new Image();
shipImage.src = shipUrls[i];
shipImages.push(shipImage);
}
//add five ships at a time
for (var i=0; i<5;i++){
var ship = {};
ship.x = (Math.random()*1000000)%WIDTH;
ship.y = 0; //top is zero
ship.width = 80;
ship.height = 80;
ship.speedY = 0.7;
ship.speedX = 1 + Math.random()*3; //1 to 4
ship.images = shipImages;
ship.move = function(){
if(this.x+this.width >= WIDTH && this.speedX>0){
//now move to left
this.speedX = - this.speedX;
}
if( this.x <= 0 && this.speedX <0){
// now move to right (-*- = +)
this.speedX = - this.speedX;
}
this.x += this.speedX;
this.y += this.speedY;
//re-appear
if(this.y >= 600){
this.y = -50;
}
}
//adding bullet at x,y position
ship.fireBullet = function(){
if(Math.random()<0.01)
addBullet(this.x,this.y);
}
ships.push(ship);
}
///////////////////// alien Code from alien.html file
var alienImageUrls = ["https://i.imgur.com/tvJOu59.png",
"https://i.imgur.com/e1pkJRF.png",
"https://i.imgur.com/aRumf1r.png",
"https://i.imgur.com/jjOPpWL.png",
"https://i.imgur.com/hsdEpsM.png",
"https://i.imgur.com/u5eNyl8.png",
"https://i.imgur.com/tvJOu59.png"];
// ctx.drawImage(alienImage,Object.x,Object.y,Object.width,Object.height);
var alienImages = [];
for (var i=0;i<alienImageUrls.length;i++){
var alienImage = new Image();
alienImage.src = alienImageUrls[i];
alienImages.push(alienImage);
}
var alien = {};
alien.x = 300;
alien.y = 300;
alien.width = 100;
alien.height = 100;
alien.speed = 7;
alien.images = alienImages;
//step-2 as we have already done.
var keyMap = {};
keyMap[38] = { name:'up', active:false, onactive: function(){alien.y -= alien.speed; } };
keyMap[40] = { name:'down', active:false, onactive: function(){alien.y += alien.speed; } };
keyMap[37] = { name:'left', active:false, onactive: function(){alien.x -= alien.speed; } };
keyMap[39] = { name:'right', active:false, onactive: function(){alien.x += alien.speed; } };
keyMap[32] = { name:'space', active:false, onactive: function(){ fireBullet(); } };
function handleKey(event,status){
var currentController = keyMap[event.keyCode];
console.log(status);
if(!!currentController){
currentController.active = status;
}
}
document.addEventListener('keydown', function(event){ handleKey(event,true);
});
document.addEventListener('keyup', function(event){ handleKey(event,false);
});
function update(){
counter++;
//for removing back-effect when image is moving
ctx.fillStyle = '#000000';
ctx.fillRect(0,0,520,520);
// add random x and y (-2 to +2)
//Hovering effect
var randomX = -2+ Math.random()*4;
var randomY = -2+ Math.random()*4;
alien.x += randomX;
alien.y += randomY;
for(var key in keyMap){
var currentController = keyMap[key];
if (currentController.active){
currentController.onactive();
}
console.log(JSON.stringify(keyMap))
}
drawObject(alien);
//for loop code for ships with alien
for(var i=0;i<ships.length;i++){
drawObject(ships[i]);
ships[i].move();
// calls for adding bulltets with ships
ships[i].fireBullet();
}
//calls for draw and move for bullets after firing
drawAndMoveBullets();
}
//for calling update fuctions in every 50ms
setInterval(update,50);
</script>
</body>
</html>