-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
127 lines (119 loc) · 2.86 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
<!DOCTYPE html>
<html>
<head>
<title>Task 02</title>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>SPACE WAR</header>
<canvas id="canvas" ></canvas>
</body>
<script>
var aliens=[],gun=[];
var frame=0;
var w = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var h = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
var canvas = document.getElementById("canvas");
if(w>600){
canvas.width=w-100;
canvas.height=h-200;
}
else{
canvas.width=w;
canvas.height=h;
}
var ctx = canvas.getContext("2d");
var spaceship={
x:400,
y:400,
s:50,
draw: function(){
ctx.fillStyle="blue";
ctx.fillRect(this.x,this.y,this.s,this.s);
}
}
function alien(y){
this.y=y;
this.x=1700;
this.attack=function(){
ctx.fillStyle="red";
ctx.fillRect(this.x,y,20,20);
}
}
function bullet(x,y){
this.x=x;
this.y=y;
this.fire=function(){
ctx.fillStyle="purple";
ctx.fillRect(x,y,20,10);
// console.log('fire');
}
}
// var bullet={
// fire: function(x,y){
// ctx.fillStyle="purple";
// ctx.fillRect(x,y,20,10);
// console.log('fire');
// }
// }
function gameOver(enemy){
if((spaceship.x+50=== enemy.x) && (Math.abs(spaceship.y-enemy.y))<=20){
// console.log(spaceship.x,enemy.x);
// console.log(spaceship.y,enemy.y);
alert('KHELA FINISH');
}
}
function animate(){
ctx.clearRect(0,0,canvas.width,canvas.height)
spaceship.draw();
frame++;
if(frame==1 || (frame/150) % 1 == 0){
var yPos=Math.random()*800;
console.log(yPos);
aliens.push(new alien(yPos));
}
console.log('array length',aliens.length);
for (i = 0; i < aliens.length; i += 1) {
aliens[i].x += -2.5;
aliens[i].attack();
}
for (i = 0; i < aliens.length; i += 1) {
if (gameOver(aliens[i])) {
alert("KHELA KHATAM");
}
}
document.addEventListener('keyup',function(e){
if(e.code=='Space'){
console.log('space');
//console.log("coordinates",spaceship.x,spaceship.y);
gun.push(new bullet(spaceship.x+50,spaceship.y));
}
for (i = 0; i < gun.length; i += 1){
gun[i].x += 2.5;
gun[i].fire();
}
});
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
document.addEventListener('keydown',function(e){
if(e.code==='ArrowRight'){
spaceship.x += 5;
}
if(e.code==='ArrowLeft'){
spaceship.x += -5;
}
if(e.code==='ArrowUp'){
spaceship.y += -5;
}
if(e.code==='ArrowDown'){
spaceship.y += 5;
}
});
</script>
</html>