-
Notifications
You must be signed in to change notification settings - Fork 1
/
spaceinvaders.js
140 lines (119 loc) · 3.39 KB
/
spaceinvaders.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
//board
let board;
let boardWidth = 500;
let boardHeight = 600;
let context;
let gameOver = false
//ship
let shipWidth = 38;
let shipHeight = 48;
let shipX = 226;
let shipY = 500;
let shipImg;
//actual dimentions
//75x95
//21px from left edge
//24px from right edge
//middle at 45 from left edge
//middle 51 from right edge
//bullet
let bulletArray = [];
let bulletWidth = 38;
let bulletHeight = 48;
let bulletX = 218
//ship varuble
let ship = {
x : shipX,
y : shipY,
width : shipWidth,
height : shipHeight
}
//physics for ship
let Xspeed = 0;
let moveLeft = false;
let moveRight = false;
//physics for bullet
let bulletSpeed = 2
window.onload = function() { //when game starts
board = document.getElementById("board");
board.height = boardHeight;
board.width = boardWidth;
context = board.getContext("2d"); //used for drawing on the board
//draw initial dinosaur
// context.fillStyle="green";
// context.fillRect(dino.x, dino.y, dino.width, dino.height);
shipImg = new Image();
shipImg.src = "./spaceinvadersImgs/ship.png";
shipImg.onload = function() {
console.log("imgLoaded")
context.drawImage(shipImg, ship.x, ship.y, ship.width, ship.height);
}
bulletImg = new Image();
bulletImg.src = "./spaceinvadersImgs/bullet.png";
window.addEventListener("keydown", function(e){
switch(e.key){
case "ArrowLeft":
moveLeft = true
break;
case "ArrowRight":
moveRight = true
break;
case " ":
e.preventDefault()
console.log("shoot bullet")
if (e.key === " "){
//place bullet
let bullet = {
img : bulletImg,
x : ship.x,
y : 536,
width : bulletWidth,
height: bulletHeight
}
bulletArray.push(bullet); //add bullet at this moment to the array
if (bulletArray.length > 25) {
bulletArray.shift(); //remove the first element from the array so that the array doesn't constantly grow
}
}
}
}, false);
window.addEventListener("keyup", function(e){
switch(e.key){
case "ArrowLeft":
moveLeft = false
break;
case "ArrowRight":
moveRight = false
break;
}
}, false);
update()
}
function update(){
requestAnimationFrame(update);
console.log("update")
//preping for next frame
if (gameOver) {
return;
}
if(ship.x < 0 && Xspeed == -5 || ship.x > 500-shipWidth && Xspeed == 5){
Xspeed = 0
}
ship.x += Xspeed;
if(moveLeft && !moveRight){
Xspeed = -5
}
if(moveRight && !moveLeft){
Xspeed = 5
}
if(!moveLeft && !moveRight){
Xspeed = 0
}
context.clearRect(0, 0, board.width, board.height);
for (let i = 0; i < bulletArray.length; i++) {
let bullet = bulletArray[i];
bullet.y -= bulletSpeed;
context.drawImage(bullet.img, bullet.x, bullet.y, bullet.width, bullet.height);
}
context.drawImage(shipImg, ship.x, ship.y, ship.width, ship.height);
};