-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.js
123 lines (101 loc) · 2.79 KB
/
source.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
// constants
const BOARD_WIDTH = 12;
const BOARD_HEIGHT = 18;
const SCALE = 30;
let board = [];
let game_speed, score,
fallSpeed, gameOver,
peice;
function setup() {
// boardWidth * scale, boardHeight * scale
createCanvas(360,540);
frameRate(15);
gameSetup();
textSize(25);
}
function gameSetup() {
game_speed = 20;
score = 0;
fallSpeed = 1;
gameOver = false;
// init board
for (let y = 0; y < BOARD_HEIGHT; y++) {
for (let x = 0; x < BOARD_WIDTH; x++) {
board[y * BOARD_WIDTH + x] = x == 0 ||
y == BOARD_HEIGHT - 1 || x == BOARD_WIDTH - 1 ? 9 : 0;
}
}
peice = new Peice();
}
function draw() {
background(0);
// draw current board state
for (let y = 0; y < BOARD_HEIGHT; y++) {
for (let x = 0; x < BOARD_WIDTH; x++) {
const tile = board[y * BOARD_WIDTH + x];
switch(tile) {
case 9: fill(40); break;
case 1: fill(51,252,255); break;
case 2: fill(255,255,0); break;
case 3: fill(0,0,255); break;
case 4: fill(255,162,0); break;
case 5: fill(58,255,0); break;
case 6: fill(255,0,0); break;
case 7: fill(170,0,255); break;
case 0: fill(0); break;
default: fill(255); break;
}
rect(x * SCALE, y * SCALE, SCALE, SCALE);
}
}
peice.draw();
fill(255);
text(`Score: ${score}`, 45, 30);
if (frameCount % floor(game_speed) == 0) {
if (!peice.fall(fallSpeed)) {
peice.addToBoard();
peice = new Peice();
if (floor(game_speed - 0.25) > 0)
game_speed -= 0.25;
}
checkBoard();
}
// input
if (checkKey(37)) peice.move(-1);
if (checkKey(39)) peice.move(1);
if (checkKey(40)) {
peice.fall(fallSpeed);
score += 1;
}
if (gameOver) {
gameSetup();
fill(0);
rect(SCALE,height/2 - 50,width - SCALE*2,120);
fill(255);
text(" GAME OVER! \n Press SPACE to restart.", (width / 2) - (30 * 4.8), height / 2);
noLoop();
}
}
function checkBoard() {
let cleared = 0;
for (let y = 0; y < BOARD_HEIGHT - 1; y++) {
let line = 0;
for (let x = 1; x < BOARD_WIDTH - 1; x++) {
if (board[y * BOARD_WIDTH + x] != 0) line++;
if (line > 0 && y == 0) gameOver = true;
if (line >= BOARD_WIDTH - 2) {
clearLine(y);
cleared += 1;
}
}
}
if (cleared > 0)
score += (100 << cleared);
}
function clearLine(line) {
for (let y = line; y > 0; y--) {
for (let x = 1; x < BOARD_WIDTH; x++) {
board[y * BOARD_WIDTH + x] = board[(y - 1) * BOARD_WIDTH + x];
}
}
}