-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
206 lines (183 loc) · 4.88 KB
/
main.js
File metadata and controls
206 lines (183 loc) · 4.88 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
//Canvas Setting
let canvas = document.createElement("canvas");
let ctx = canvas.getContext("2d");
canvas.width = 400;
canvas.height = 650;
document.body.appendChild(canvas);
//global variable
let backGroundImg, bulletImg, GameOverImg, monsterImg, spaceshipImg;
let gameOver = false;
let stage = 1;
let score = 0;
let speed = 1500
//Coordinations of SpaceShip
let SpaceShipX = canvas.width / 2 - 32;
let SpaceShipY = canvas.height - 64;
let bulletArray = [];
let monsterArray = [];
function Bullet() {
this.x = 0;
this.y = 0;
this.alive = true;
//method
this.fire = function () {
this.x = SpaceShipX + 20;
this.y = SpaceShipY;
bulletArray.push(this);
};
this.update = function () {
this.y -= 5;
};
this.hitMonster = function () {
for (let i = 0; i < monsterArray.length; i++) {
if (
this.y <= monsterArray[i].y &&
this.x >= monsterArray[i].x &&
this.x <= monsterArray[i].x + 50
) {
score+=1;
this.alive = false;
monsterArray.splice(i, 1);
}
}
};
}
function generateRandomNumver(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function Monster() {
this.x = 0;
this.y = 0;
//method
this.init = function () {
this.x = generateRandomNumver(0, canvas.width - 50);
this.y = 0;
monsterArray.push(this);
};
this.update = function () {
this.y += 0.9;
//gameover logic
if (this.y > canvas.height - 50) {
gameOver = true;
console.log("GAME OVER");
}
};
}
//Load Images
function loadImgs() {
backGroundImg = new Image();
backGroundImg.src = "images/SpaceImg.png";
bulletImg = new Image();
bulletImg.src = "images/Bullet.png";
GameOverImg = new Image();
GameOverImg.src = "images/GameOver.jpeg";
monsterImg = new Image();
monsterImg.src = "images/Monster.png";
spaceshipImg = new Image();
spaceshipImg.src = "images/SpaceShip.png";
}
//Keyboard controlls
let keysdown = {};
function setupKeyboardListener() {
document.addEventListener("keydown", (event) => {
keysdown[event.key] = true;
console.log("keypressed:", event.key);
console.log(keysdown);
});
document.addEventListener("keyup", (event) => {
delete keysdown[event.key];
console.log(keysdown);
if (event.key === " ") {
createBullet();
}
});
}
function createBullet() {
console.log("bullet making");
let b = new Bullet();
b.fire();
}
function createMonster() {
setInterval(() => {
let m = new Monster();
m.init();
}, speed);
}
function updateCoordination() {
if (keysdown.ArrowRight) {
SpaceShipX += 5;
}
if (keysdown.ArrowLeft) {
SpaceShipX -= 5;
}
if (keysdown.ArrowUp) {
SpaceShipY -= 3;
}
if (keysdown.ArrowDown) SpaceShipY += 3;
if (SpaceShipX <= 0) {
SpaceShipX = 0;
}
if (SpaceShipX >= canvas.width - 64) {
SpaceShipX = canvas.width - 64;
}
if (SpaceShipY >= canvas.height - 64) {
SpaceShipY = canvas.height - 64;
}
if (SpaceShipY <= 0) {
SpaceShipY = 0;
}
for (let i = 0; i < bulletArray.length; i++) {
if (bulletArray[i].alive) {
bulletArray[i].update();
bulletArray[i].hitMonster();
}
}
for (let i = 0; i < monsterArray.length; i++) {
monsterArray[i].update();
}
}
//Rendering Imgs
function renderImgs() {
ctx.drawImage(backGroundImg, 0, 0, canvas.width, canvas.height);
ctx.drawImage(spaceshipImg, SpaceShipX, SpaceShipY);
ctx.fillText(`Score:${score}`, 20, 20);
ctx.fillText(`Stage:${stage}`, 20, 50);
ctx.fillStyle = "white";
ctx.font = "20px Airal";
for (let i = 0; i < bulletArray.length; i++) {
if (bulletArray[i].alive) {
ctx.drawImage(bulletImg, bulletArray[i].x, bulletArray[i].y);
}
}
for (let i = 0; i < monsterArray.length; i++) {
ctx.drawImage(monsterImg, monsterArray[i].x, monsterArray[i].y);
}
}
//Rendering Imgs continuously
function main() {
if (!gameOver) {
updateCoordination();
renderImgs();
requestAnimationFrame(main);
} else {
ctx.drawImage(GameOverImg, 10, 100, 380, 300);
}
}
//Calling functions
createMonster();
loadImgs();
main();
setupKeyboardListener();
// bullet logic
// 1.if you press spacebar, bullet will generate
// 2.the coordinations of bullet is gonna be coordinations of spaceship when spacebar is pressed
// 3.if the bullets is generated , the y coordination will keep decreasing and x coordination will remain same
// 4.the bullets data will store in an array
// 5.render bullets with the array
//Monster Logic
//1.Monster has init, update and stageUp method
//2.Monster comes down=> y coordination keep increasing
//3.One monster will generate every 1.5s =>use setInterval()
//4.When monster touches the ground, game is over => when monster y coordination is greater than canvas.height-monseter size game over
//score Logic
//1.when bullet hits monster, score ++=> bullet.y < monster.y && monster.x <= bullet.x && bullet.x <= monster.size+monster.x