-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.cpp
More file actions
212 lines (195 loc) · 3.82 KB
/
Copy pathApp.cpp
File metadata and controls
212 lines (195 loc) · 3.82 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
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <cmath>
#define h 25
#define w 15
using namespace std;
struct hitter {
int len = 6;
int pos = w / 2 - len / 2;
};
struct pong {
int x = w / 2, y = h / 2 + 5;
int vx = 1, vy = -1;
char ball = 'o';
};
hitter player;
pong ball;
char play = 'y';
int score = 0;
int max_score = 0;
int game_over = 0;
char buffer[h][w];
char brick = (char)254;
void init_buffer();
void render();
void bind_objects();
void update_ball();
void detect_collision();
void control();
void game_over_message();
void home_screen();
void reset_game();
int win_condition();
int main() {
home_screen();
while (play == 'y') {
reset_game();
init_buffer();
while (!game_over) {
bind_objects();
render();
detect_collision();
update_ball();
control();
if (win_condition() == 1) break;
Sleep(10);
}
if(game_over) game_over_message();
}
return 0;
}
void init_buffer() {
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (i > 1 && i < h / 3 && j > 1 && j < w - 2) {
buffer[i][j] = brick;
max_score++;
}
else buffer[i][j] = ' ';
if (i == 0)buffer[i][j] = 'X';
if (j == 0 || j == w - 1)buffer[i][j] = 'X';
}
}
buffer[1][w - 2] = ' ';
}
int win_condition() {
if (score == max_score) {
render();
cout <<endl<< "\t You Win!" << endl;
cout << "\tPlay Again (y)? ";
cin >> play;
return 1;
}
return 0;
}
void game_over_message() {
cout << "\n\t Game Over!" << endl;
cout << "\tPlay Again (y)? ";
cin >> play;
}
void home_screen() {
cout << "a: left d: right" << endl;
cout << "Press any key to start the game...";
play = _getch();
play = 'y';
}
void reset_game() {
max_score = 0;
game_over = 0;
score = 0;
play = 'y';
ball.x = w / 2; ball.y = h / 2;
ball.vx = 1; ball.vy = -1;
player.pos = w / 2 - player.len / 2;
}
void control() {
if (_kbhit()) {
char c;
c = _getch();
int move = 4;
switch (c) {
case 'a':
player.pos -= move;
if (player.pos <= 0) player.pos = 1;
for (int i = 1; i < w - 1; i++) buffer[h - 2][i] = ' ';
break;
case 'd':
player.pos += move;
int right = player.pos + player.len - 1;
int excess = w - 1 - right;
if (excess <= 0) {
player.pos -= 1;
player.pos += excess;
}
for (int i = 1; i < w - 1; i++) buffer[h - 2][i] = ' ';
break;
}
}
}
void detect_collision() {
// ball falls
if (ball.y > h - 2) {
game_over = 1;
}
//player collision
for (int i = player.pos; i < player.pos + player.len; i++) {
if (ball.y == h - 3 && ball.x == i) {
ball.vy = -ball.vy;
break;
}
}
//wall collision
if (ball.x >= w - 2 || ball.x <= 1) {
ball.vx = -ball.vx;
}
if (ball.y <= 1) {
ball.vy = -ball.vy;
}
int flag1 = 0, flag2 = 0;
//brick collision
if (buffer[ball.y + ball.vy][ball.x] == brick) {
buffer[ball.y + ball.vy][ball.x] = ' ';
ball.vy = -ball.vy;
flag1 = 1;
score++;
}
if (buffer[ball.y][ball.x + ball.vx] == brick) {
buffer[ball.y][ball.x + ball.vx] = ' ';
ball.vx = -ball.vx;
score++;
flag2 = 1;
}
if (buffer[ball.y + ball.vy][ball.x + ball.vx] == brick && !flag1 && !flag2) {
buffer[ball.y + ball.vy][ball.x + ball.vx] = ' ';
ball.vx = -ball.vx;
ball.vy = -ball.vy;
score++;
}
}
void update_ball() {
if (ball.x < 1) {
ball.x = 1;
ball.vx = 1;
}
if (ball.x > w - 2) {
ball.x = w - 2;
ball.vx = -1;
}
if (ball.y < 1) {
ball.y = 1;
ball.vy = 1;
}
buffer[ball.y][ball.x] = ' ';
ball.x += ball.vx;
ball.y += ball.vy;
}
void bind_objects() {
for (int i = player.pos; i <= player.pos + player.len - 1; i++) {
buffer[h - 2][i] = '#';
}
if (ball.x > 0 && ball.x < w - 1 && ball.y>0) {
buffer[ball.y][ball.x] = ball.ball;
}
}
void render() {
system("cls");
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cout<<buffer[i][j] << " ";
}
cout << endl;
}
cout << "\t Score: " << score;
}