-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
218 lines (210 loc) · 6.17 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 canvas, height, width, ctx, evt, factor, gridSize, marginY, timeStamp;
var pipes;
function onpress(evt){
var rect = canvas.getBoundingClientRect();
if((evt.x > rect.left && evt.x < rect.right) && (evt.y > rect.top) && (evt.y < rect.bottom)){
var X = evt.x-(Math.floor(rect.left));
var Y = evt.y-(Math.floor(rect.top));
var x = Math.floor(X/factor); var y = Math.floor((Y-marginY)/factor);
if(Y>marginY&&Y<(Math.floor(rect.bottom))-marginY*2){
pipes[x][y].rotate();
}
}
}
function main() {
canvas = document.createElement("canvas");
gridSize = 5;
width = window.innerWidth;
height = window.innerHeight;
var evt = "touchstart";
if(width >= 500){
width = 320;
height = 480;
canvas.style.border = "1px solid #000";
evt = "mousedown";
}
factor = width/gridSize;
marginY = (height - factor*gridSize)/2;
document.addEventListener(evt, onpress);
canvas.width = width;
canvas.height = height;
ctx = canvas.getContext("2d");
document.body.appendChild(canvas);
var img = new Image();
img.onload = function () {
initSprites(this);
ctx.fillStyle = s_pipe.color;
pipes = generatePipes(gridSize,gridSize,1,1);
run();
}
img.src = "resources/sheet.png";
}
function generatePipes(sizex,sizey,startPipeNum,endPipeNum){
var array2d = [];
for(x = 0; x < sizex; x++){
var innerArray = [];
for(y = 0; y < sizey; y++){
var pipe = new Pipe(x,y,Math.floor((Math.random()*4)),Math.floor((Math.random()*3)));
innerArray.push(pipe);
}
array2d.push(innerArray);
}
var i = 0;
while(i < startPipeNum){
var randomX = Math.floor((Math.random()*array2d.length));
var randomY = Math.floor((Math.random()*array2d[randomX].length));
if(array2d[randomX][randomY].spriteNum!=4){
var direction = checkSides(randomX,randomY,array2d);
var pipe = new Pipe(randomX,randomY,4,direction,true);
array2d[randomX][randomY] = pipe;
i++;
}
}
i = 0;
while(i < endPipeNum){
var randomX = Math.floor((Math.random()*array2d.length));
var randomY = Math.floor((Math.random()*array2d[randomX].length));
if(array2d[randomX][randomY].spriteNum!=4){
var direction = checkSides(randomX,randomY,array2d);
var pipe = new Pipe(randomX,randomY,4,direction,false);
array2d[randomX][randomY] = pipe;
i++;
}
}
return array2d;
}
function checkSides(randomX,randomY,array2d){
var direction;
if(randomX==0){
if(randomY==0){
direction = Math.floor((Math.random()*2));
} else if (randomY==(array2d[randomX].length-1)){
switch (Math.floor((Math.random()*2))){
case 0:{
direction = 3;
break;
}
case 1:{
direction = 0;
break;
}
}
} else {
switch (Math.floor((Math.random()*3))){
case 0:{
direction = 3;
break;
}
case 1:{
direction = 0;
break;
}
case 2:{
direction = 1;
break;
}
}
}
} else if (randomX == array2d.length-1){
if(randomY==0){
switch (Math.floor((Math.random()*2))){
case 0:{
direction = 2;
break;
}
case 1:{
direction = 1;
break;
}
}
} else if (randomY==(array2d[randomX].length-1)){
switch (Math.floor((Math.random()*2))){
case 0:{
direction = 3;
break;
}
case 1:{
direction = 2;
break;
}
}
} else {
switch (Math.floor((Math.random()*3))){
case 0:{
direction = 3;
break;
}
case 1:{
direction = 2;
break;
}
case 2:{
direction = 1;
break;
}
}
}
} else if(randomY==0){
direction = (Math.floor((Math.random()*3)));
} else if (randomY==(array2d[randomX].length-1)){
switch (Math.floor((Math.random()*3))){
case 0:{
direction = 0;
break;
}
case 1:{
direction = 2;
break;
}
case 2:{
direction = 3;
break;
}
}
} else {
direction = (Math.floor((Math.random()*4)));
}
return direction;
}
function run(){
var loop = function() {
update();
render();
window.requestAnimationFrame(loop,canvas);
}
window.requestAnimationFrame(loop,canvas);
}
function update(){
var filledArray = [];
for(x = 0; x < pipes.length; x++){
for(y = 0; y < pipes[x].length; y++){
if(pipes[x][y].update()){
filledArray.push({x:x,y:y})
}
}
}
var d = new Date();
var secs = d.getSeconds();
var incrSecs = 1;
if(typeof timeStamp == "undefined"){
timeStamp = secs+incrSecs;
}
if(secs >= timeStamp && (!((secs+incrSecs)>=60)||((timeStamp+incrSecs)>=60))){
timeStamp = (secs+incrSecs)%60;
if(secs%5==0){
for(var i in filledArray){
pipes[filledArray[i].x][filledArray[i].y].fillArround();
console.log("splash!!");
}
}
}
}
function render(){
ctx.clearRect(0, 0, width, height);
for(x = 0; x < pipes.length; x++){
for(y = 0; y < pipes[x].length; y++){
pipes[x][y].draw(ctx,factor/100,marginY);
}
}
}
main();