-
Notifications
You must be signed in to change notification settings - Fork 3
/
Tetris.pde
56 lines (41 loc) · 1.16 KB
/
Tetris.pde
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
static final int w = 10; // 4
static final int h = 30; // 60
static final int framesInSecond = 30;
static final float gameSpeed = framesInSecond / 5.0; // moving down ~5 times in second
float updatingThreshold = 0;
Game game;
float recalculateUpdatingThreshold(float threshold) {
return threshold + 1;
}
void setup() {
game = new Game(w, h);
generateRandomBlock(game);
size(w*BlockScale + 1, h*BlockScale + 1);
frameRate(framesInSecond);
}
void draw() {
background(0);
Game newGame = game;
updatingThreshold = recalculateUpdatingThreshold(updatingThreshold);
if (updatingThreshold > gameSpeed) {
newGame = updateGameState(game);
if (isGameOver(newGame)) {
// Do something on end of game
//exit();
}
updatingThreshold = 0;
}
drawGameState(newGame);
}
void keyPressed() {
if (key == CODED) {
switch (keyCode) {
case LEFT: moveBlock(game, MoveLeft); break;
case RIGHT: moveBlock(game, MoveRight); break;
case DOWN: makeBlockFall(game); break;
case UP: rotateBlock(game); break;
}
} else if (key == ' ') { // SPACE
rotateBlock(game);
}
}