-
Notifications
You must be signed in to change notification settings - Fork 0
/
breakTheWall.cpp
258 lines (214 loc) · 4.91 KB
/
breakTheWall.cpp
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
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <ctime>
#include <termios.h>
#include <unistd.h>
#include <sys/select.h>
#include <math.h>
using namespace std;
#ifdef _WIN32
#elif __linux__
struct termios orig_termios;
/* Returns to original terminal */
void reset_terminal_mode()
{
tcsetattr(0, TCSANOW, &orig_termios);
}
/* Sets special terminal */
void set_conio_terminal_mode()
{
struct termios new_termios;
/* take two copies - one for now, one for later */
tcgetattr(0, &orig_termios);
memcpy(&new_termios, &orig_termios, sizeof(new_termios));
/* register cleanup handler, and set the new terminal mode */
atexit(reset_terminal_mode);
cfmakeraw(&new_termios);
tcsetattr(0, TCSANOW, &new_termios);
}
/* Ckecks if keyboard was hit */
int kbhit()
{
struct timeval tv = { 0L, 0L };
fd_set fds;
FD_ZERO(&fds);
FD_SET(0, &fds);
return select(1, &fds, NULL, NULL, &tv);
}
/* Gets char from keyboard */
int getch()
{
int r;
unsigned char c;
if ((r = read(0, &c, sizeof(c))) < 0) {
return r;
} else {
return c;
}
}
#endif
#define INIT_DELAY 0.15
#define HEIGHT 15
#define WIDTH 52
#define PI 3.1415
#include "brick.cpp"
/* Return time */
double time_now()
{
return double (clock())/CLOCKS_PER_SEC;
}
/* Prints a line of '#' */
void printLine ()
{
for (int x = 0; x <= WIDTH+1; x++) cout << '#';
cout << endl;
}
/* Checks if the pair [i, j] belongs to the array of size n */
bool belongs (int i, int j, Brick *v, int n)
{
for (int k = 0; k < n; k++)
if (v[k].x == i && v[k].y == j && v[k].on) return true;
return false;
}
/* Converts x to radians */
double rad (double x)
{
return x*PI/180.0;
}
/* Return a random number between min and max */
int rand_range (int min, int max)
{
srand(clock());
return rand()*1.0/(RAND_MAX-1) * (max+1-min) + min;
}
/* Return the amount of digits in n */
int digits (int n)
{
int ans = 0;
while (n) {
n /= 10;
ans ++;
}
return ans;
}
int score = 0;
int maxScore;
int lifes = 5;
/* Prints ending text, with correct spacing */
void printEndText ()
{
int len = 0;
if (lifes == -1) {
len = 28 + digits(score);
for (int i = 0; i < (WIDTH - len)/2; i++) cout << ' ';
cout << "YOU LOST Your Score: " << score;
for (int i = 0; i < (WIDTH - len)/2; i++) cout << ' ';
}
else if (score == maxScore) {
len = 27 + digits(score);
for (int i = 0; i < (WIDTH - len)/2; i++) cout << ' ';
cout << "YOU WON Your Score: " << score;
for (int i = 0; i < (WIDTH - len)/2; i++) cout << ' ';
}
else {
for (int i = 0; i < WIDTH; i++) cout << ' ';
}
}
#include "paddle.cpp"
#include "ball.cpp"
int main () {
bool gameOver = false;
double t1 = time_now();
double t2 = t1;
double delay = INIT_DELAY;
char input = ' ';
int n_bricks = WIDTH/4;
int n_lines = 3;
maxScore = n_bricks * n_lines * 10;
Paddle pad (WIDTH/2);
Ball ball (WIDTH/2, HEIGHT/2);
ball.setSpeed (rand_range(210, 330));
Brick *all_bricks = new Brick [n_bricks * n_lines];
/* Initializes all bricks in the correct positions */
for (int i = 0, h = -1; i < n_bricks * n_lines; i++) {
if (i%n_bricks == 0) h++;
all_bricks[i].x = (i%n_bricks);
all_bricks[i].y = h;
all_bricks[i].on = true;
}
while (!gameOver) {
/* SETUP TERMINAL */
set_conio_terminal_mode();
while (!kbhit() && t1-t2 < delay) t1 = time_now();
if (t1-t2 < delay) input = getch();
t2 = t1;
reset_terminal_mode();
system ("clear");
/*----------------*/
/* INPUT */
switch (input) {
case 'a':
case 'A':
case 'd':
case 'D':
pad.move(input);
break;
case 'q':
case 'Q':
gameOver = true;
break;
}
input = ' ';
/*-------*/
/* LOGIC */
ball.checkPaddle(pad);
ball.checkRoof();
ball.checkPass();
ball.checkBrick(all_bricks, n_lines*n_bricks);
ball.checkWall();
ball.move();
/*-------*/
/* DRAW */
printLine();
for (int j = 0; j < HEIGHT; j++) {
cout << "#";
for (int i = 0; i < WIDTH; i++) {
if (i >= pad.start && i <= pad.end && j == HEIGHT-1) cout << '=';
else if (i == floor(ball.x) && j == floor(ball.y)) cout << 'O';
else if (belongs(i/4, j, all_bricks, n_bricks*n_lines)) {
if (i%4 == 0) cout << '[';
if (i%4 == 1) cout << '+';
if (i%4 == 2) cout << '+';
if (i%4 == 3) cout << ']';
}
else cout << ' ';
}
cout << '#' << endl;
}
printLine();
cout << "score: " << score << endl;
cout << "lifes: " << lifes << endl;
if (score == n_bricks*n_lines*10 || lifes == -1) gameOver = true;
/*------*/
}
/* Ending text */
system ("clear");
printLine();
for (int j = 0; j < HEIGHT; j++) {
cout << "#";
if (j == HEIGHT/2) printEndText();
for (int i = 0; i < WIDTH; i++) {
if (belongs(i/4, j, all_bricks, n_bricks*n_lines)) {
if (i%4 == 0) cout << '[';
if (i%4 == 1) cout << '+';
if (i%4 == 2) cout << '+';
if (i%4 == 3) cout << ']';
}
else if (j != HEIGHT/2) cout << ' ';
}
cout << '#' << endl;
}
printLine();
return 0;
}