-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacker.js
More file actions
155 lines (121 loc) · 3.92 KB
/
stacker.js
File metadata and controls
155 lines (121 loc) · 3.92 KB
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
let canvas;
let ctx;
let bHeight = 12;
let bWidth = 7;
let board = Array.from(Array(bHeight), () => new Array(bWidth)) //the board has 0's where there are no pieces and 1 where there are
let squareSize = 50;
let cWidth = 800;
let cHeight = 800;
let sx = (cWidth - (bWidth * squareSize)) / 2; //starting x value for board (from left)
let sy = 25; //starting y value for board (from top)
let starts = [2, 4, 1, 0, 3, 2, 3, 0, 1, 2, 1, 3] //positions where left most piece starts from on each line going from top to bottom
let curY = bHeight - 1;
let curX = starts[curY];
let curSize = 3; //how many pieces to display on row
let velocity = 1; //1 if piece is moving right, -1 if left
let paused = false;
document.addEventListener('DOMContentLoaded', setupCanvas);
function setupCanvas(){
canvas = document.getElementById('my-canvas');
ctx = canvas.getContext('2d');
canvas.width = cWidth;
canvas.height = cHeight;
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, cWidth, cHeight);
ctx.fillStyle = 'gray';
//fill board background
ctx.fillRect(sx, sy, bWidth*squareSize, bHeight*squareSize);
ctx.strokeStyle = 'black';
//draw board border depending on size of board grid
ctx.strokeRect(sx, sy, bWidth*squareSize, bHeight*squareSize);
//fill up the board with a grid
for(let i = 0; i < bHeight; i++){
for(let j = 0; j < bWidth; j++){
ctx.strokeRect(sx + j*squareSize, sy + i*squareSize, squareSize, squareSize);
}
}
//set starting values for board
for(let i = 0; i < bHeight; i++){
for(let j = 0; j < bWidth; j++){
board[i][j] = 0;
}
}
board[curY][curX] = 1;
board[curY][curX+1] = 1;
board[curY][curX+2] = 1;
//handle keyboard presses
document.addEventListener('keydown', handleKeyPress);
}
window.setInterval(function(){
move();
}, 250);
function stopPiece(){
//make sure we start with only as many pieces landed on the board
pieceCount = 0;
for(let i = 0; i < bWidth; i++){
if(board[curY][i] === 1){
pieceCount++;
}
}
curSize = pieceCount;
//check for pieces that are off the stack
if(curY < bHeight - 1){
for(let i = 0; i < bWidth; i++){
if(board[curY][i] === 1 && board[curY+1][i] === 0){
board[curY][i] = 0;
drawSquare('gray', i, curY);
curSize--;
console.log(curSize);
}
}
}
velocity = 1;
curY--;
if(curY < 0)
endGame('You have won!');
curX = starts[curY];
}
function endGame(condition){
paused = true;
console.log(condition)
}
function drawSquare(color, x, y){
ctx.fillStyle = color;
ctx.fillRect(sx + x * squareSize + 1, sy + y * squareSize + 1, squareSize - 2, squareSize - 2);
}
function drawRow(row){
for(let i = 0; i < bWidth; i++){
if(board[row][i] === 0)
drawSquare('gray', i, row);
else
drawSquare('red', i, row);
}
}
function move(){
if(paused === false){
if(curSize === 0){
endGame('You have lost!');
}
//wipe current row
for(let i = 0; i < bWidth; i++){
board[curY][i] = 0;
}
let pieceCount = 0;
if(curX < bWidth && curX >= 0 && curSize > 0)
board[curY][curX] = 1;
if(bHeight - curY <= 6 && curSize > 1 && curX+1 < bWidth && curX+1 >= 0) //there should be at least 2 pieces on this row
board[curY][curX+1] = 1;
if(bHeight - curY <= 3 && curSize > 2 && curX+2 < bWidth && curX+2 >= 0) //there should be 3 pieces on this row
board[curY][curX+2] = 1;
drawRow(curY);
if(curX == bWidth-1)
velocity = -1;
else if(curX+curSize-1 <= 0)
velocity = 1;
curX += velocity;
}
}
function handleKeyPress(key){
if (key.keyCode === 32 && paused === false)
stopPiece();
}