-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpong_eiei_1_0_a.cpp
241 lines (197 loc) · 7.06 KB
/
pong_eiei_1_0_a.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
//
// PONG EIEI VERSION 1.0.A
// Created by Seehait Chockthanyawat
//
#include <iostream>
#include <unistd.h>
#include <thread>
#include <string>
using namespace std;
// game version
const string game_version = "1.0.a";
// input
char c = 'a';
// game status
bool game_start = false;
// screen
const int screen_refreshing_coodown = 30000;
char screen[24][80];
// toggle
const int toggle_cooldown = 100;
bool toggle = true;
int toggle_count = 0;
// ball
const int ball_default_x = 39, ball_default_y = 11;
int ball_x = ball_default_x, ball_y = ball_default_y;
bool ball_dir_x = true, ball_dir_y = true;
// player
const int player_default_y = 9, player_max_score = 3, player_paddle = 5;
int player1_score, player2_score;
int player1_y = player_default_y, player2_y = player_default_y;
// winner
int winner = 0;
// init terminal
void init() {
system("stty raw");
// hide cursor
printf("%c[?25l", 0x1B);
// init screen
for (int i = 0; i < 24; i++) {
for (int j = 0; j < 80; j++) {
if (j == 3 && i >= player1_y && i <= player1_y + 5) screen[i][j] = ']';
else if (j == 76 && i >= player2_y && i <= player2_y + 5) screen[i][j] = '[';
else if (i == ball_y && j == ball_x) screen[i][j] = '*';
else screen[i][j] = ' ';
}
}
}
// clear screen
void clear_screen() {
printf("%c[1J", 0x1B);
}
// draw game title
void draw_title() {
while (true) {
if (!game_start) {
printf("%c[1;1H", 0x1B);
printf("%c[%dm", 0x1B, 32);
printf("%c[%dm", 0x1B, 49);
printf(" ");
printf(" ");
printf(" ");
printf(" ████ ███ █ █ ███ █████ █████ █████ █████ ");
printf(" █ █ █ █ ██ █ █ █ █ █ █ ");
printf(" ████ █ █ █ █ █ █ ██ █████ █ █████ █ ");
printf(" █ █ █ █ ██ █ █ █ █ █ █ ");
printf(" █ ███ █ █ ███ █████ █████ █████ █████ ");
printf(" ");
printf("%c[%dm", 0x1B, 36);
printf(" by Seehait Chockthanyawat ");
printf(" ");
printf("%c[%dm", 0x1B, 31);
if (toggle) {
if (winner == 0) printf(" ");
else if (winner == 1) printf(" Player1 WIN! ");
else if (winner == 2) printf(" Player2 WIN! ");
} else {
printf(" ");
}
printf("%c[%dm", 0x1B, 34);
printf(" ");
printf(" ");
printf(" ");
printf(" ");
printf(" ");
printf(" ");
printf(" ");
printf(" ");
if (toggle) printf(" PRESS SPACEBAR ");
else printf(" ");
printf(" ");
printf(" ");
printf("%c[%dm", 0x1B, 33);
printf(" VERSION 1.0.a");
// if (game_start) cout << "EXIT" << endl;
// printf("PLAYER1 SCORE: %d PLAYER2 SCORE: %d", player1_score, player2_score);
if (toggle_count > toggle_cooldown) {
toggle = !toggle;
toggle_count = 0;
}
toggle_count++;
}
}
//terminate();
}
// draw current game
void draw_game() {
printf("%c[1;1H", 0x1B);
printf("%c[%dm", 0x1B, 37);
printf("%c[%dm", 0x1B, 40);
for (int i = 0; i < 23; i++) {
for (int j = 0; j < 80; j++) cout << screen[i][j];
}
printf("Player1's score: %d Player2's score: %d", player1_score, player2_score);
}
// move ball
void move_ball() {
if (ball_dir_x) ball_x += 2;
else ball_x -= 2;
if (ball_dir_y) ball_y++;
else ball_y--;
// if (ball_x == 0 || ball_x == 7player_default_y) ball_dir_x = !ball_dir_x;
if (ball_x == 5 && ball_y >= player1_y && ball_y <= player1_y + player_paddle) {
ball_dir_x = !ball_dir_x;
}
if (ball_x == 75 && ball_y >= player2_y && ball_y <= player2_y + player_paddle) {
ball_dir_x = !ball_dir_x;
}
if (ball_x >= 79) {
ball_dir_x = true;
ball_dir_y = true;
ball_x = ball_default_x;
ball_y = ball_default_y;
player1_score++;
}
if (ball_x <= 0) {
ball_dir_x = true;
ball_dir_y = true;
ball_x = ball_default_x;
ball_y = ball_default_y;
player2_score++;
}
if (ball_y == 0 || ball_y == 22) ball_dir_y = !ball_dir_y;
if (player1_score > player_max_score - 1 || player2_score > player_max_score - 1) {
winner = player1_score > player2_score ? 1 : 2;
player1_score = 0;
player2_score = 0;
game_start = false;
}
}
// input detector
void get_input() {
while (true) {
c = getchar();
if (c == 'w' && player1_y > 0) player1_y--;
if (c == 's' && player1_y < 22 - player_paddle) player1_y++;
if (c == 'i' && player2_y > 0) player2_y--;
if (c == 'k' && player2_y < 22 - player_paddle) player2_y++;
if (c == ' ') {
game_start = true;
}
}
}
// manage game screen
void screen_manager() {
while(true) {
if (game_start) {
draw_game();
usleep(screen_refreshing_coodown);
move_ball();
init();
} else {
player1_y = player_default_y;
player2_y = player_default_y;
}
}
}
// set title bar
void set_title() {
char esc_start[] = { 0x1b, ']', '0', ';', 0 };
char esc_end[] = { 0x07, 0 };
cout << esc_start << "Pong Eiei " << game_version << esc_end;
}
// main
int main()
{
// terminal init
init();
set_title();
// threads init
thread tt(draw_title);
thread sm(screen_manager);
thread ip(get_input);
// threads start
ip.join();
tt.join();
sm.join();
}