-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspaceboi.js
202 lines (166 loc) · 3.85 KB
/
spaceboi.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
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
var canvas = document.querySelector('canvas');
var ctx = canvas.getContext('2d');
var width = canvas.width = window.innerWidth;
var height = canvas.height = window.innerHeight;
var pos = 0;
var gameStatus = 0;
var gapWidth = 150;
var score = 0;
var hScore = 0;
var canTran = 0;
var aiEn = 0;
var logBoi = 1.2;
var laserConst =0;
var Key = {
_pressed: {},
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
SPACE: 32,
A: 65,
isDown: function(keyCode) {
return this._pressed[keyCode];
},
onKeydown: function(event) {
this._pressed[event.keyCode] = true;
},
onKeyup: function(event) {
delete this._pressed[event.keyCode];
}
};
window.addEventListener('keyup', function(event) { Key.onKeyup(event); }, false);
window.addEventListener('keydown', function(event) { Key.onKeydown(event); }, false);
function EndGame(){
if(gameStatus){
ctx.fillStyle = 'rgba(0,0,0,1)';
ctx.fillRect(-2000,-2000,4*width,4*height);
for(i=0;i<stars.length;i++){
stars[i].update();
stars[i].draw();
}
pillar1.draw();
pillar2.draw();
pillar3.draw();
box.draw();
ctx.fillStyle = 'rgba(255,0,0,0.25)';
ctx.fillRect(-2000,-2000,3*width,5*height);
ctx.save();
ctx.textAlign = "center";
ctx.fillStyle = 'blue';
ctx.font = '72px arial';
ctx.translate(-(canTran),0);
if(score >= hScore){
hScore = score;
ctx.font = '128px arial';
ctx.fillText("NEW HIGH SCORE",width/2,height/2);
ctx.font = '72px arial';
}
ctx.translate ((width/4),height/8);
ctx.fillText(score,0, 0);
ctx.translate(width/4,0);
ctx.fillText("High Score: ",100,0);
ctx.fillText(hScore,500,0);
ctx.restore();
}
}
function gameReset(){
ctx.restore();
ctx.translate(-(canTran),0);
canTran = 0;
box.x = width/2;
box.y = height*0.9;
gameStatus = 0;
pillar1.y = 0;
pillar2.y = -(height);
pillar3.y = -height*2;
pillar1.height = (Math.random() * width * 0.85);
pillar2.height = (Math.random() * width * 0.85);
pillar3.height = (Math.random() * width * 0.85);
pillar1.barrier = 5;
pillar2.barrier = 5;
pillar3.barrier = 5;
score = 0;
}
document.addEventListener('keydown', function(event) {
if(event.keyCode == 65) {
aiEn ^= 1;
}
else if(event.keyCode == 33){
speed++;
}
else if(event.keyCode == 34){
if(speed > 0){
speed--;
}
}
});
var box = new Box(width/2,height*0.9,10,0,'rgb(0,255,0)',10);
var pillar1 = new Pillar(0);
var pillar2 = new Pillar((-height));
var pillar3 = new Pillar(-(height*2));
var rotate = 0;
var rotPos =0;
var gravity = 0;
var stars = [];
var bob = new Star();
var i;
var speed = 5;
var aiKeyUp = 0;
var aiKeyLeft = 0;
var aiKeyRight = 0;
var img = document.getElementById("logo");
function loop(){
while(stars.length < 150){
var star = new Star();
stars.push(star);
}
if(!gameStatus){
ctx.save();
ctx.clearRect(-2000,-2000, 4*width, 4*height);
ctx.fillStyle = 'rgba(0,0,0,1)';
ctx.fillRect(-2000,-2000,4*width,4*height);
for(i=0;i<stars.length;i++){
stars[i].update();
stars[i].draw();
}
if(aiEn){
ai();
}
else{
aiKeyUp =0;
aiKeyLeft =0;
ayKeyRight =0;
}
pillar1.update();
pillar1.draw();
pillar1.collision();
pillar2.update();
pillar2.draw();
pillar2.collision();
pillar3.update();
pillar3.draw();
pillar3.collision();
box.update();
box.draw();
ctx.save();
ctx.textAlign = 'center';
ctx.fillStyle = 'blue';
ctx.font = '72px arial';
ctx.translate(-(canTran),0);
ctx.translate ((width/4),height/8);
ctx.fillText(score,0, 0);
ctx.translate(width/4,0);
ctx.fillText("High Score: ",100,0);
ctx.fillText(hScore,500,0);
ctx.restore();
}
else{
if (Key.isDown(Key.SPACE)){
gameReset();
}
EndGame();
}
requestAnimationFrame(loop);
}
loop();