-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.wacc
238 lines (190 loc) · 6.75 KB
/
tetris.wacc
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
begin
struct PollResult {
int leftBtn,
int rightBtn,
int upBtn,
int downBtn,
int f1Btn
}
int pollBtns(struct PollResult btns) is
btns.leftBtn = 0;
btns.rightBtn = 0;
btns.upBtn = 0;
btns.downBtn = 0;
btns.f1Btn = 0;
int c = 0;
int in = 0;
in = gpio 24;
if (in == 1) then
btns.leftBtn = 1
else skip fi;
in = gpio 27;
if (in == 1) then
btns.rightBtn = 1
else skip fi;
in = gpio 22;
if (in == 1) then
btns.upBtn = 1
else skip fi;
in = gpio 23;
if (in == 1) then
btns.downBtn = 1
else skip fi;
in = gpio 25;
if (in == 1) then
btns.f1Btn = 1
else skip fi;
return 0
end
# The time to wait before rendering a new frame
int waitTime = 560000;
int pollTime = 500;
#starting location for all new blocks
int startX = 4;
int startY = 0;
#get all the colours
int currentColourScheme = 0;
int[][] colourSchemes = call initialiseColours();
int[] colours = colourSchemes[currentColourScheme];
#pass in height, width to initilise grid
int height = 18;
int width = 10;
int[][] grid = call initIntArray2D(0, width, height);
int x = startX;
int y = startY;
int colourIndex = 0;
#get the frame buffer
int[] fb = get_frame_buffer;
int[][][][] blocks = call Blocks();
#initilise first block
int blockIndex = 0;
int rotationIndex = 0;
int currentBlockRotationIndex = rotationIndex;
int[][] block = blocks[blockIndex][rotationIndex];
struct PollResult btns = new PollResult;
call drawBlock(fb, block, x, y, colours[colourIndex]);
int collision = 0;
int keepLooping = 1;
int count = 0;
int in = 1;
while (keepLooping == 1) do
in = 0;
count = 0;
while (count < 140000) do
call pollBtns(btns);
if (btns.leftBtn == 1) then
collision = call isColliding(block, grid, x - 1, y);
if (collision == 0) then # If there is no collision
#erase square, 0 means black
call drawBlock(fb, block, x, y, 0);
x -= 1;
#draw updated square
call drawBlock(fb, block, x, y, colours[colourIndex])
else skip fi
else skip fi;
if (btns.rightBtn == 1) then
collision = call isColliding(block, grid, x + 1, y );
if (collision == 0) then # If there is no collision
#erase square, 0 means black
call drawBlock(fb, block, x, y, 0);
x += 1;
#draw updated square
call drawBlock(fb, block, x, y, colours[colourIndex])
else skip fi
else skip fi;
if (btns.downBtn == 1) then
collision = call isColliding(block, grid, x, y + 1);
if (collision == 0) then # If there is no collision
#erase square, 0 means black
call drawBlock(fb, block, x, y, 0);
y += 1;
#draw updated square
call drawBlock(fb, block, x, y, colours[colourIndex])
else skip fi
else skip fi;
if (btns.upBtn == 1) then
#erase square, 0 means black
call drawBlock(fb, block, x, y, 0);
int savedRotationIndex = rotationIndex;
#rotate
rotationIndex += 1;
if (rotationIndex == 4) then
rotationIndex = 0
else
rotationIndex = rotationIndex
fi;
block = blocks[blockIndex][rotationIndex];
#if there is a collision then don't rotate, else keep rotated shape
collision = call isColliding(block, grid, x, y);
if (collision == 1) then
rotationIndex = savedRotationIndex;
block = blocks[blockIndex][rotationIndex]
else
skip
fi;
#draw updated square
call drawBlock(fb, block, x, y, colours[colourIndex])
else skip fi;
if (btns.f1Btn == 1) then
currentColourScheme += 1;
colourIndex = 0;
if (currentColourScheme >= (len colourSchemes)) then
currentColourScheme = 0
else skip fi;
colours = colourSchemes[currentColourScheme]
else skip fi;
count += 1
done;
collision = call isColliding(block, grid, x, y + 1);
if (collision == 1) then
call copyBlockToGrid(block, grid, x, y, colours[colourIndex]);
#get info on what rows need to be shifted down
int[] rowInfo = call detectRows(grid);
#while we can still shift down, keep shifting down
if (rowInfo[0] == 1) then
call shiftDown(grid, rowInfo);
skip
else
skip
fi;
call drawGrid(fb, grid);
colourIndex += 1;
if (colourIndex >= len colours) then
colourIndex = 0
else
colourIndex = colourIndex
fi;
blockIndex += 1;
if (blockIndex == len blocks) then
blockIndex = 0
else
blockIndex = blockIndex
fi;
rotationIndex += 1;
if (rotationIndex == 4) then
rotationIndex = 0
else
rotationIndex = rotationIndex
fi;
block = blocks[blockIndex][rotationIndex];
x = startX;
y = startY;
call drawBlock(fb, block, x, y, colours[colourIndex]);
collision = call isColliding(block, grid, x, y);
if (collision == 1) then keepLooping = 0 else skip fi;
# Gameover in the true branch of this if
skip
else
#erase square, 0 means black
call drawBlock(fb, block, x, y, 0);
#move block down
y += 1;
#draw updated square
call drawBlock(fb, block, x, y, colours[colourIndex])
fi
done;
while (true) do
skip
done;
skip
end