-
Notifications
You must be signed in to change notification settings - Fork 0
/
indecs.html
311 lines (306 loc) · 7.53 KB
/
indecs.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Gamedev Canvas Workshop</title>
<link rel = "shortcut icon" href="APCSArcadeico.ico">
<link rel = "apple-touch-icon" href="APCSArcadeFavicon.png">
<link rel = "icon" sizes="192x192" href="APCSArcadeFavicon.png">
<style>
* { padding: 0; margin: 0; }
canvas { background: #eee; display: block; margin: 0 auto; }
</style>
</head>
<body>
<canvas id="myCanvas" width="1900" height="949" style="border:10px solid"></canvas>
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var x = canvas.width/2;
var y = canvas.height/2 + 250;
var circx = canvas.width/2 ;
var circy = canvas.height/2 + 250;
var d = 3;
var upPressed = false;
var downPressed = false;
var leftPressed = false;
var rightPressed = false;
var stage = "precircle";
var rotation = 0;
var rectx = 0;
var recty = 0;
var dcirc = 2;
var hit = false;
var timer = 0;
var timer2 = 0;
var rand = 5000* Math.random();
var decided = false;
var gametime = 0;
var xproj = [];
var yproj = [];
var projstage;
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);
for(var i = 0; i < 30; i++){
xproj[i] = canvas.width * Math.random();
yproj[i] = canvas.height * Math.random();
}
function keyDownHandler(e) {
if(e.key == "Right" || e.key == "ArrowRight") {
rightPressed = true;
}
if(e.key == "Left" || e.key == "ArrowLeft") {
leftPressed = true;
}
if(e.key == "Up" || e.key == "ArrowUp"){
upPressed = true;
}
if(e.key == "Down" || e.key == "ArrowDown"){
downPressed = true;
}
}
function keyUpHandler(e) {
if(e.key == "Right" || e.key == "ArrowRight") {
rightPressed = false;
}
if(e.key == "Left" || e.key == "ArrowLeft") {
leftPressed = false;
}
if(e.key == "Up" || e.key == "ArrowUp"){
upPressed = false;
}
if(e.key == "Down" || e.key == "ArrowDown"){
downPressed = false;
}
}
function drawCharacter() {
ctx.beginPath();
ctx.arc(x, y, 8, 0, Math.PI*2);
ctx.fillStyle = "#000000";
ctx.fill();
ctx.closePath();
}
function drawProjectiles(){
ctx.beginPath();
ctx.fillStyle = "#FF0000";
for(var i = 0; i < 30; i++){
ctx.arc(xproj[i], yproj[i], 26, 0, Math.PI*2);
ctx.fill();
ctx.closePath();
}
}
function drawpreProjectiles(){
ctx.beginPath();
ctx.fillStyle = "#FBFF00";
for(var i = 0; i < 30; i++){
ctx.arc(xproj[i], yproj[i], 26, 0, Math.PI*2);
ctx.fill();
ctx.closePath();
}
}
function drawPreCircle(){
var circx = canvas.width/2 ;
var circy = canvas.height/2 + 250;
ctx.beginPath();
ctx.fillStyle = "#FBFF00";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.arc(circx, circy, 150, 0, Math.PI*2);
ctx.fillStyle = "#FFFFFF";
ctx.fill();
ctx.closePath();
}
function drawCircle(){
ctx.beginPath();
ctx.fillStyle = "#FF3C3C";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.arc(circx, circy, 150, 0, Math.PI*2);
ctx.fillStyle = "#FFFFFF";
ctx.fill();
ctx.closePath();
}
function drawPreHorBeam(){
ctx.beginPath();
ctx.fillStyle = "#FBFF00";
ctx.fillRect(rectx, recty, canvas.width, 1);
ctx.closePath();
}
function drawPreVertBeam(){
ctx.beginPath();
ctx.fillStyle = "#FBFF00";
ctx.fillRect(recty, rectx, 1, canvas.height);
ctx.closePath();
}
function drawHorBeam(){
ctx.beginPath();
ctx.fillStyle = "#FF3C3C";
ctx.fillRect(rectx, recty, canvas.width, 1);
ctx.closePath();
}
function drawVertBeam(){
ctx.beginPath();
ctx.fillStyle = "#FF3C3C";
ctx.fillRect(recty, rectx, 1, canvas.height);
ctx.closePath();
}
function draw() {
gametime+=.01;
timer+=10;
timer2 += 10;
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (stage == "precircle"){
drawPreCircle();
if(timer >= 2000){
stage = "circleCW";
timer = 0;
timer2 = 0;
}
}
if(stage == "circleCW"){
drawCircle();
diffx = circx - canvas.width/2;
diffy = circy - canvas.height/2;
circx += dcirc * ((- diffy)/(Math.sqrt(Math.pow(diffx, 2) + Math.pow(diffy, 2))));
circy += dcirc * ((diffx)/(Math.sqrt(Math.pow(diffx, 2) + Math.pow(diffy, 2))));
if(Math.sqrt(Math.pow(circx - x, 2) + Math.pow(circy - y, 2)) >= 147){
hit = true;
}
if(timer2 >= 7500){
stage = "prebeams";
timer2 = 0;
timer = 0;
decided = false;
}
if(decided == true){
if(timer >= rand){
stage = "circleCCW";
decided = false;
timer = 0;
}
}
else{
rand = 5000 * Math.random();
decided = true;
timer = 0;
}
}
if(stage == "circleCCW"){
drawCircle();
diffx = circx - canvas.width/2;
diffy = circy - canvas.height/2;
circx += dcirc * ((diffy)/(Math.sqrt(Math.pow(diffx, 2) + Math.pow(diffy, 2))));
circy += dcirc * ((- diffx)/(Math.sqrt(Math.pow(diffx, 2) + Math.pow(diffy, 2))));
if(Math.sqrt(Math.pow(circx - x, 2) + Math.pow(circy - y, 2)) >= 147){
hit = true;
}
if(timer2 >= 75000){
stage = "prebeams";
timer2 = 0;
timer = 0;
decided = false;
}
if(decided == true){
if(timer >= rand){
stage = "circleCW";
decided = false;
}
}
else{
rand = 5000 * Math.random();
decided = true;
timer = 0;
}
}
if(stage == "prebeams"){
for(var i = 0; i < 10; i++){
recty = 100 * i;
drawPreHorBeam();
}
for(var i = 0; i < 20; i++){
recty = 100 * i;
drawPreVertBeam();
}
if(timer >= 750){
stage = "beams";
timer = 0;
}
}
if(stage == "beams"){
for(var i = 0; i <= 10; i++){
recty = 100 * i;
drawHorBeam();
if(Math.abs(y - recty) <= 4){
hit = true;
}
}
for(var i = 0; i <= 20; i++){
recty = 100 * i;
drawVertBeam();
if(Math.abs(x - recty) <= 4){
hit = true;
}
}
if(timer >= 2000){
stage = "preprojectiles";
timer = 0;
}
}
if(stage == "preprojectiles"){
drawpreProjectiles();
if(timer >= 500){
timer = 0;
stage = "projectiles1";
}
}
if(stage == "projectiles1"){
drawProjectiles();
for(var i = 0; i < 30; i++){
yproj[i] +=10;
if(Math.sqrt(Math.pow(xproj[i] - x, 2) + Math.pow(yproj[i] - y, 2)) <= 16){
hit = true;
}
}
if(timer >= 1000){
timer = 0;
stage = "precircle";
}
}
drawCharacter();
if(rightPressed && upPressed){
x += d / Math.sqrt(2);
y -= d / Math.sqrt(2);
}
else if(rightPressed && downPressed){
x += d / Math.sqrt(2);
y += d / Math.sqrt(2);
}
else if(leftPressed && downPressed){
x -= d / Math.sqrt(2);
y += d / Math.sqrt(2);
}
else if(leftPressed && upPressed){
x -= d / Math.sqrt(2);
y -= d / Math.sqrt(2);
}
else{
if(rightPressed) {
x += d;
}
if(leftPressed) {
x -= d;
}
if(upPressed){
y -= d;
}
if(downPressed){
y += d;
}
}
if(hit == true){
alert("GAME OVER Time survived: " + Math.round(gametime) + " seconds");
document.location.reload();
}
}
setInterval(draw, 10);
</script>
</body>
</html>