-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
218 lines (209 loc) · 6.62 KB
/
game.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
var ctx, self, enemies = [], speed, enemys, score, c, ctx, paused, time, mov, tsize, revives, endgamereq, width, height, easter, instructions;
function move() {
if (endgamereq == 2) return;
else if (endgamereq) {
gameOver();
return;
}
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
ctx.font = tsize + 'px sans';
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.lineWidth = 5;
enemys = Math.floor(ctx.canvas.width * ctx.canvas.height / 38880);
for (var i = enemies.length; i < enemys; i++) {
enemies[i] = new enemy(i);
enemies[i].init();
}
for (var i = 0; i < enemys; i++) enemies[i].move();
self.move();
mov = requestAnimationFrame(move);
time--;
text();
if (time === 0) endgamereq = 1;
if (score == 5) easter = false;
}
function key(type, event) {
switch (event.keyCode) {
case 37:
if (type == 'down') self.dir = 'l';
else self.dir = null;
break;
case 39:
if (type == 'down') self.dir = 'r';
else self.dir = null;
break;
case 13:
if (paused === true && type == 'down') {
mov = requestAnimationFrame(move);
if (instructions) {
document.getElementById('instructions').close();
instructions = false;
} else {
document.getElementById('paused').close();
}
paused = false;
}
else if (type == 'down') {
cancelAnimationFrame(mov);
document.getElementById('paused').showModal();
paused = true;
}
break;
case 52:
if (type == 'down' && score == 4) easter = true;
else easter = false;
break;
case 73:
if (type == 'down') {
instructions = true;
document.getElementById('paused').close();
document.getElementById('instructions').showModal();
}
}
}
function gameOver() {
cancelAnimationFrame(mov);
var message;
function popup(beat) {
function highScore(name) {
this.score = score;
this.name = name;
}
if (beat === true) {
var name = prompt('You collected ' + score + ' coins. ' + message + ' Enter your name and press OK to play again.');
var highScore = new highScore(name);
localStorage.highScoreCisoze = JSON.stringify(highScore);
}
else {
alert('You collected ' + score + ' coins. ' + message + ' Press OK to play again.');
}
}
if (!(localStorage.highScoreCisoze)) {
message = 'Because you haven\'t played before, your score for this game is the new local high score.';
popup(true);
}
else {
var oldHighScore = JSON.parse(localStorage.highScoreCisoze);
if (oldHighScore.score > score) {
message = 'You failed to beat high score, ' + oldHighScore.name + '\'s high score of ' + oldHighScore.score + ' points!';
popup(false);
}
else if (oldHighScore.score < score) {
message = 'You beat ' + oldHighScore.name + '\'s high score of ' + oldHighScore.score + ' points!';
popup(true);
}
else {
message = 'You tied the local high score!';
popup(false);
}
}
endgamereq = 2;
location.reload();
}
function evaluate(x, y, radius, num, color) {
if (((Math.sqrt(Math.pow(x - self.x, 2) + Math.pow(y - self.y, 2)) < radius + self.radius && easter === false) || (Math.abs(x - self.x) < radius + self.radius && Math.abs(y-self.y) < radius + self.radius && easter === true)) && color != 'white' && color != 'red') {
score++;
time += 10;
if (color == 'cyan') {
if (time < 3600){
time = 3600;
if (revives === 0) {
endgamereq = true;
}
revives--;
}
score--;
return 'red';
}
else return 'white';
}
else return color;
}
function self(i) {
this.radius = 30;
this.x = ctx.canvas.width / 2;
this.y = ctx.canvas.height - 50 - this.radius;
this.dir = null;
this.move = function() {
if (this.dir == 'r' && this.x + this.radius <= ctx.canvas.width) this.x += speed * 2;
if (this.dir == 'l' && this.x - this.radius >= 0) this.x -= speed * 2;
if (this.x + this.radius > ctx.canvas.width) this.x = ctx.canvas.width - this.radius;
this.y = ctx.canvas.height - 50 - this.radius;
ctx.beginPath();
ctx.strokeStyle = 'magenta';
if (easter) ctx.rect(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2);
else ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.stroke();
};
}
function enemy(num) {
this.num = num;
this.radius = 10;
this.init = function() {
this.color = 'yellow';
this.speed = (Math.random() * 4 + 1) / 2;
this.x = Math.floor(Math.random() * (ctx.canvas.width - (this.radius * 2 - 1))) + this.radius;
this.y = -(this.radius * 2);
this.rand = Math.floor(Math.random() * 40);
if (this.rand === 0) {
this.color = 'cyan';
this.speed *= 3;
}
};
this.move = function() {
this.y += speed * this.speed;
this.color = evaluate(this.x, this.y, this.radius, this.num, this.color);
if (this.y - this.radius >= ctx.canvas.height && enemies.length <= enemys) this.init();
else if (enemies.length > enemys) enemies.splice(this.num, 1);
ctx.beginPath();
ctx.strokeStyle = this.color;
if (easter) ctx.rect(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2);
else ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.stroke();
};
}
function text() {
ctx.fillStyle = 'white';
ctx.textAlign = 'left';
ctx.fillText('Cycles remaining: ' + time, 2, 2 + tsize);
ctx.textAlign = 'center';
ctx.fillText('Coins collected: ' + score, ctx.canvas.width / 2, 2 + tsize);
ctx.textAlign = 'right';
ctx.fillText('Revives remaining: ' + revives, ctx.canvas.width - 2, 2 + tsize);
}
function init() {
paused = false;
easter = false;
instructions = false;
if (!(localStorage.playedCisoze)) {
document.getElementById('instructions').showModal();
paused = true;
instructions = true;
localStorage.playedCisoze = true;
}
endgamereq = false;
revives = 2;
tsize = 20;
c = document.getElementById('c');
ctx = c.getContext('2d');
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
enemys = Math.floor(ctx.canvas.width * ctx.canvas.height / 38880);
ctx.font = tsize + 'px sans';
ctx.lineWidth = 5;
for (var i = 0; i < enemys; i++) {
enemies[i] = new enemy(i);
enemies[i].init();
}
self = new self();
speed = 3;
score = 0;
time = 3600;
self.move();
text();
if (paused === false) mov = requestAnimationFrame(move);
}
document.onkeydown = function() {key('down', event);};
document.onkeyup = function() {key('up', event);};
window.onload = function() {init();};