-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
296 lines (250 loc) · 7.79 KB
/
main.cpp
File metadata and controls
296 lines (250 loc) · 7.79 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <allegro.h>
#include <string>
#include <fstream>
#include <vector>
#include <ctime>
#include <cmath>
using namespace std;
bool game_over();
BITMAP* buffer;
BITMAP* background;
BITMAP* square[7];
BITMAP* bla;
SAMPLE* song;
int highscore_nums[3][10];
string highscore_names[3][10];
bool board [17][29]; // Actually 15*25 but with 3 hidden rows for the top of pieces
int colour [17][29]; // for drawing graphics purposes
int score = 0, lines = 0, level = 1; // score related variables
#include "piece.h"
vector <Piece> pieces; // contains a record of all pieces
#include "Allegro functions.h"
#include "Highscores.h"
bool collide (int active)
{
int x,y;
pieces[active].Get_Loc(x,y);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if ((board[x+i][y+j-1]|| y+j==0) && pieces[active].In_Spot(i,j))
{
for (int k = 0; k < 4; k++)
for (int h = 0; h < 4; h++)
if (pieces[active].In_Spot(k,h))
{
board[x+k][y+h] = true;
colour[x+k][y+h] = pieces[active].Get_Type();
}
score += 10;
return true;
}
return false;
}
void complete_row ()
{
int c = 1;
bool complete;
for (int i = 0; i < 25; i++)
{
complete = true;
for (int j = 1; j < 16; j++)
if (!board[j][i])
{
complete = false;
break;
}
if (complete)
{
score += 50*c;
c++;
lines++;
for (int j = i+1; j < 27; j++)
for (int k = 1; k < 16; k++)
{
colour[k][j-1] = colour[k][j];
board[k][j-1] = board [k][j];
}
i--;
}
}
}
bool game_over ()
{
for (int i = 1; i < 16; i++)
if (board[i][24])
return true;
return false;
}
void instructions ()
{
draw_sprite (buffer, background, 0, 0);
textprintf_centre_ex(buffer, font, 220, 140, makecol(255,255,255), -1, "Instructions");
textprintf_centre_ex(buffer, font, 220, 160, makecol(255,255,255), -1, "A piece in the darker blue area will end the game");
textprintf_centre_ex(buffer, font, 220, 180, makecol(255,255,255), -1, "Rotate Clockwise - Down Arrow Key");
textprintf_centre_ex(buffer, font, 220, 200, makecol(255,255,255), -1, "Rotate Counter Clockwise - Up Arrow Key");
textprintf_centre_ex(buffer, font, 220, 220, makecol(255,255,255), -1, "Move Left - Left Arrow Key");
textprintf_centre_ex(buffer, font, 220, 240, makecol(255,255,255), -1, "Move Right - Right Arrow Key");
textprintf_centre_ex(buffer, font, 220, 260, makecol(255,255,255), -1, "Hard Drop - Space Bar");
textprintf_centre_ex(buffer, font, 220, 280, makecol(255,255,255), -1, "Press Any Key to Continue");
textprintf_centre_ex(buffer, font, 220, 300, makecol(255,255,255), -1, "M to play/stop music");
textprintf_centre_ex(buffer, font, 220, 320, makecol(255,255,255), -1, "Escape to Quit");
acquire_screen();
draw_sprite (screen, buffer,0,0);
release_screen();
readkey();
clear_keybuf();
}
int main(int argc, char *argv[])
{
srand(time(0));
start_allegro();
for (int i = 0; i < 17; i++)
for (int j = 0; j < 29; j++)
board[i][j] = false;
for (int i = 0; i < 17; i++)
for (int j = 0; j < 29; j++)
colour[i][j] = -1;
int movement, active, x, y;
active = 0;
bool playing = false; // if music is playing
// Initialize the first 2 pieces
for (int i = 0; i < 2; i++)
{
Piece temp;
pieces.push_back(temp);
}
pieces[active].Set_Active();
music(playing);
instructions();
if (key[KEY_ESC])
{
close_allegro();
return 0;
}
install_int (ticker, 400); // initialize timer
int oldticks, oldticks2,oldlevel;
bool dropped = false, move[4] = {false,false,false,false};
while (!close_button_pressed)
{
oldlevel = level;
level = (lines*25+score)/500+1;
if (level > oldlevel && level < 31) // speeding game up over time
{
install_int (ticker, 408-level*8); // initialize timer
}
// If piece is on another piece
/* if (collide(active))
{
Piece temp;p
pieces.push_back(temp);
active++;
complete_row();
if (game_over())
break;
pieces[active].Set_Active();
}*/
// timers
while (ticks == 0)
{ rest (1); }
while (ticks > 0)
{
oldticks = ticks;
// Movement of the piece. Rotate or horizontal
movement = 0;
if (key[KEY_UP] && !move[0])
{
movement = 2;
move[0] = true;
}
else if (!key[KEY_UP] && move[0])
move[0] = false;
if (key[KEY_DOWN] && !move[1])
{
movement = 1;
move[1] = true;
}
else if (!key[KEY_DOWN] && move[1])
move[1] = false;
if (movement == 1 || movement == 2)
pieces[active].Rotate(movement);
if (key[KEY_LEFT] && !move[2])
{
movement = 3;
move[2] = true;
}
else if (!key[KEY_LEFT] && move[2])
move[2] = false;
if (key[KEY_RIGHT] && !move[3])
{
movement = 4;
move[3] = true;
}
else if (!key[KEY_RIGHT] && move[3])
move[3] = false;
if (movement == 3 || movement == 4)
pieces[active].Move_Over(movement-2);
if (key[KEY_SPACE] && !dropped)
{
dropped = true;
pieces[active].Get_Loc(x,y);
score+=y/4;
for(;;)
if (!collide(active))
pieces[active].Move_Down();
else
{
Piece temp;
pieces.push_back(temp);
active++;
complete_row();
if (game_over())
break;
pieces[active].Set_Active();
break;
}
break;
}
else if (!key[KEY_SPACE])
dropped = false;
if (key[KEY_M])
music(playing);
if (game_over())
break;
if (key[KEY_ESC])
{
close_allegro();
return 0;
}
updateScreen();
if (oldticks+1 <= ticks)
break;
}
if (game_over())
break;
if (!collide(active))
pieces[active].Move_Down();
else
{
Piece temp;
pieces.push_back(temp);
active++;
complete_row();
if (game_over())
break;
pieces[active].Set_Active();
}
updateScreen();
// rest(180);
}
clear_keybuf();
updateScreen();
if (!close_button_pressed)
highscore_update();
while (!close_button_pressed && !readkey())
{
readkey();
}
close_allegro();
return 0;
}
END_OF_MAIN();