-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
151 lines (124 loc) · 3.88 KB
/
main.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
var board;
var game_loop;
function handleKeyDown(event) {
handleKeyDown.whichOn = handleKeyDown.whichOn == undefined ? -1 : handleKeyDown.whichOn; // nothing selected initially
handleKeyDown.modelOn = handleKeyDown.modelOn == undefined ? null : handleKeyDown.modelOn; // nothing selected initially
switch (event.code) {
// model selection
case "Space":
board.hard_drop()
redraw(true)
break;
case "ArrowRight":
board.move_right()
redraw()
break;
case "ArrowLeft":
board.move_left()
redraw()
break;
case "ArrowUp":
board.rotate()
redraw()
break;
case "ArrowDown":
var do_redraw = board.drop()
redraw(do_redraw)
break;
case "KeyC":
board.hold()
draw_next_piece('hold', board.hold_piece.name)
redraw(true)
break;
case "KeyZ":
board.rotate(false)
redraw()
break;
case "KeyX":
board.rotate()
redraw()
break;
}
}
function redraw(redraw_all = false) {
document.getElementById('score').innerHTML = board.score;
if (board.game_over) {
document.getElementById('gameover').innerHTML = "Game Over! Press Retry to Continue";
clearInterval(game_loop);
} else {
board.draw()
}
if (redraw_all) {
draw_next_piece('next', board.bag[6])
draw_next_piece('next2', board.bag[5])
draw_next_piece('next3', board.bag[4])
draw_next_piece('next4', board.bag[3])
draw_next_piece('next5', board.bag[2])
}
}
function draw_next_piece(canvas_id, tetronmino_name) {
var canvasNext = document.getElementById(canvas_id);
var ctxNext = canvasNext.getContext('2d');
ctxNext.canvas.width = 5 * 25;
ctxNext.canvas.height = 5 * 25;
ctxNext.scale(25, 25);
ctxNext.fillStyle = 'black';
clear_piece(canvas_id)
var next_piece = get_tetronmino(tetronmino_name)
ctxNext.fillStyle = 'rgb(' + next_piece.color[0] * 255 + ',' + next_piece.color[1] * 255 + ',' + next_piece.color[2] * 255 + ')';
// If piece is I piece, since it's a 5x5 grid, float it up one.
if (tetronmino_name == "I") {
for (var i = 1; i < next_piece.shape.length; i++) {
for (var j = 1; j < next_piece.shape.length; j++) {
if (next_piece.shape[j][i] > 0) {
ctxNext.fillRect(i - 1, j - 1, 1, 1);
}
}
}
} else {
for (var i = 0; i < next_piece.shape.length; i++) {
for (var j = 0; j < next_piece.shape.length; j++) {
if (next_piece.shape[j][i] > 0) {
ctxNext.fillRect(i, j, 1, 1);
}
}
}
}
}
function clear_piece(canvas_id) {
var canvasNext = document.getElementById(canvas_id);
var ctxNext = canvasNext.getContext('2d');
ctxNext.canvas.width = 5 * 25;
ctxNext.canvas.height = 5 * 25;
ctxNext.scale(25, 25);
ctxNext.fillStyle = 'black';
for (var i = 0; i < 5; i++) {
for (var j = 0; j < 5; j++) {
ctxNext.fillRect(i, j, 1, 1);
}
}
}
function reset() {
document.getElementById('gameover').innerHTML = " ";
if (board.game_over) {
reset_game_loop()
}
board.reset()
clear_piece('hold')
redraw(true);
// This is necessray otherwise space will trigger retry again
document.getElementById('retry').blur();
}
function main() {
document.onkeydown = handleKeyDown; // call this when key pressed
board = new Board()
redraw(true)
reset_game_loop()
}
function reset_game_loop() {
function loop() {
var do_redraw = board.drop();
redraw(do_redraw)
}
game_loop = setInterval(loop, 800);
}