-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
157 lines (135 loc) · 2.92 KB
/
sketch.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
var C_WIDTH = 640;
var C_HEIGHT = 500;
var X_ORIGIN = 195;
var Y_ORIGIN = 0;
var X_COUNT = 10;
var Y_COUNT = 20;
var G_SIZE = 25;
var MAX_STILL = 3; // maximum runs of tetris shape standing still
var field = new Field(X_ORIGIN, Y_ORIGIN, X_COUNT, Y_COUNT, G_SIZE);
var blocks = new Blocks();
var actualBlock = null;
var clearBlock = false;
var timeStart;
var timeEnd;
var timeDiff = 0;
var speedMillis = 300;
function setup()
{
$('#container').css('width', C_WIDTH + 'px');
$('#container').css('height', C_HEIGHT + 'px');
frameRate(60);
var canvas = createCanvas(C_WIDTH, C_HEIGHT);
canvas.parent('container');
timeStart = timestamp();
}
function draw()
{
background(50);
field.draw();
timeEnd = timestamp();
timeDiff = timeEnd - timeStart;
if (timeDiff > (speedMillis))
{
checkActualBlock();
timeStart = timeEnd;
}
if (actualBlock != null)
{
actualBlock.draw();
}
}
function checkActualBlock()
{
if (clearBlock)
{
// set Field as used
field.setUsed(actualBlock);
field.logField();
var linesCleared = field.checkLinesCleared();
if (linesCleared > 0)
{
field.logField();
console.log(field.array);
}
actualBlock = null;
clearBlock = false;
}
if (actualBlock === null)
{
// generiere neuen Block
actualBlock = blocks.getRandBlock();
}
moveBlock('down');
//TODO: check if cannont move further: generate new block
actualBlock.checkIfStoodStill();
if (actualBlock.stoodStill == MAX_STILL)
{
clearBlock = true;
}
actualBlock.updateLastCoordinates();
}
function moveBlock(move)
{
if (actualBlock === null)
{
return;
}
else if (move == 'rotate')
{
if (actualBlock.checkRotationMove())
{
actualBlock.changeVariation();
}
}
else if (move == 'left')
{
if (actualBlock.checkLeftMove())
{
actualBlock.rx--;
}
}
else if (move == 'right')
{
if (actualBlock.checkRightMove())
{
actualBlock.rx++;
}
}
else if (move == 'down')
{
if (actualBlock.checkDownMove())
{
actualBlock.ry++;
}
}
}
function keyPressed()
{
if (keyCode == 32) // space pressed
{
moveBlock('rotate');
}
else if (keyCode == LEFT_ARROW)
{
moveBlock('left');
}
else if (keyCode == RIGHT_ARROW)
{
moveBlock('right');
}
else if (keyCode == DOWN_ARROW)
{
moveBlock('down');
}
else if (keyCode == UP_ARROW)
{
field.setUsed(actualBlock); // for testing:
actualBlock = null; // generate new block, fix actual block
}
return false;
}
function timestamp()
{
return Date.now();
}