-
Notifications
You must be signed in to change notification settings - Fork 0
/
alien1.html
155 lines (131 loc) · 5.37 KB
/
alien1.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
<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;
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);
}
// 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;
}
}
ships.push(ship);
}
///////////////////// 95% 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)
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();
}
}
//for calling update fuctions in every 50ms
setInterval(update,50);
</script>
</body>
</html>