forked from nearform/nceubadge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asteroids.js
164 lines (151 loc) · 3.25 KB
/
asteroids.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
var W = 128;
var H = 64;
g.drawPoly = function(p) {
g.moveTo(p[p.length-2],p[p.length-1]);
for (var i=0;i<p.length;i+=2)
g.lineTo(p[i],p[i+1]);
};
function newAst(x,y) {
var a = {
x:x,y:y,
vx:Math.random()-0.5,
vy:Math.random()-0.5,
rad:3+Math.random()*5
};
return a;
}
function onInit() {
clearInterval();
gameStart();
setInterval(onFrame, 50);
}
var running = true;
var ship = {};
var ammo = [];
var ast = [];
var score = 0;
var level = 4;
var framesSinceFired = 0;
function gameStop() {
console.log("Game over");
running = false;
g.clear();
g.drawString("Game Over!",(W-g.stringWidth("Game Over!"))/2,(H-6)/2);
g.flip();
}
function addAsteroids() {
for (var i=0;i<level;i++) {
do {
var x = Math.random()*W, y = Math.random()*H;
var dx = x-ship.x, dy = y-ship.y;
var d = Math.sqrt(dx*dx+dy*dy);
} while (d<10);
ast.push(newAst(x,y));
}
}
function gameStart() {
ammo = [];
ast = [];
score = 0;
level = 4;
ship = { x:W/2,y:H/2,r:0,v:0 };
framesSinceFired = 0;
addAsteroids();
running = true;
}
function onFrame() {
if (!running) {
if (BTNA.read()) gameStart();
return;
}
if (BTNL.read()) ship.r-=0.1;
if (BTNR.read()) ship.r+=0.1;
ship.v *= 0.9;
if (BTNU.read()) ship.v+=0.2;
ship.x += Math.cos(ship.r)*ship.v;
ship.y += Math.sin(ship.r)*ship.v;
if (ship.x<0) ship.x+=W;
if (ship.y<0) ship.y+=H;
if (ship.x>=W) ship.x-=W;
if (ship.y>=H) ship.y-=H;
framesSinceFired++;
if (BTNA.read() && framesSinceFired>4) { // fire!
framesSinceFired = 0;
ammo.push({
x:ship.x+Math.cos(ship.r)*4,
y:ship.y+Math.sin(ship.r)*4,
vx:Math.cos(ship.r)*3,
vy:Math.sin(ship.r)*3,
});
}
g.clear();
g.drawString(score,0,0);
var rs = Math.PI*0.8;
g.drawPoly([
ship.x+Math.cos(ship.r)*4, ship.y+Math.sin(ship.r)*4,
ship.x+Math.cos(ship.r+rs)*3, ship.y+Math.sin(ship.r+rs)*3,
ship.x+Math.cos(ship.r-rs)*3, ship.y+Math.sin(ship.r-rs)*3,
]);
var na = [];
ammo.forEach(function(a) {
a.x += a.vx;
a.y += a.vy;
g.fillRect(a.x-1, a.y, a.x+1, a.y);
g.fillRect(a.x, a.y-1, a.x, a.y+1);
var hit = false;
ast.forEach(function(b) {
var dx = a.x-b.x;
var dy = a.y-b.y;
var d = Math.sqrt(dx*dx+dy*dy);
if (d<b.rad) {
hit=true;
b.hit=true;
score++;
}
});
if (!hit && a.x>=0 && a.y>=0 && a.x<W && a.y<H)
na.push(a);
});
ammo=na;
na = [];
var crashed = false;
ast.forEach(function(a) {
a.x += a.vx;
a.y += a.vy;
g.drawCircle(a.x, a.y, a.rad);
if (a.x<0) a.x+=W;
if (a.y<0) a.y+=H;
if (a.x>=W) a.x-=W;
if (a.y>=H) a.y-=H;
if (!a.hit) {
na.push(a);
} else if (a.rad>4) {
a.hit = false;
var vx = 1*(Math.random()-0.5);
var vy = 1*(Math.random()-0.5);
a.rad/=2;
na.push({
x:a.x,
y:a.y,
vx:a.vx-vx,
vy:a.vy-vy,
rad:a.rad,
});
a.vx += vx;
a.vy += vy;
na.push(a);
}
var dx = a.x-ship.x;
var dy = a.y-ship.y;
var d = Math.sqrt(dx*dx+dy*dy);
if (d < a.rad) crashed = true;
});
ast=na;
if (!ast.length) {
level++;
addAsteroids();
}
g.flip();
if (crashed) gameStop();
}
onInit();