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
| #include <windows.h>
#include <gl/glut.h>
#include <stdlib.h>
#include <time.h>
#include <cmath>
class game{
public:
int score_left;
int score_right;
int winwid;
int winhei;
bool done_right;
bool done_left;
game(){
score_left = 0;
score_right = 0;
winwid = 600;
winhei = 600;
done_right = false;
done_left = false;
}
void win();
void start_settings();
void floor_reflect();
void border();
void draw_border();
void draw_score_l();
void draw_score_r();
}player;
class ball{
public:
int x;
double y;
double vx;
double vy;
void move(){
x += vx;
y += vy;
}
void reflection();
void draw_ball();
}pong_ball;
class reflector{
public:
int x;
int y;
double vy;
int size;
reflector(){
x = 0;
y = 0;
vy = 0;
size = 250;
}
void draw_r();
void move();
}left,right;
void reflector::draw_r(){
glVertex2f(x + 10,y + size/2);
glVertex2f(x + 10,y - size/2);
glVertex2f(x - 10,y - size/2);
glVertex2f(x - 10,y + size/2);
}
void ball::reflection(){
if((x < left.x+10)&&(fabs(double(y - left.y)) < left.size/2)){vx = -vx;vy+=left.vy*0.2;}
if((x > right.x-10)&&(fabs(double(y - right.y)) < right.size/2)){vx = -vx;vy+=right.vy*0.2;}
}
void ball::draw_ball(){
glColor3f(1,(rand()%10)/10,(rand()%10)/10);
glVertex2f(x+10+rand()%7,y+10+rand()%7);
glVertex2f(x+10+rand()%7,y-10+rand()%7);
glVertex2f(x-10+rand()%7,y-10+rand()%7);
glVertex2f(x-10+rand()%7,y+10+rand()%7);
glColor3f(0,0,0);
}
void game::floor_reflect(){
if((pong_ball.y < -player.winhei)||(pong_ball.y > player.winhei))pong_ball.vy = -pong_ball.vy;
}
void game::start_settings(){
left.x = -510;
right.x = 510;
int i = rand()%2;
if(i == 0)pong_ball.vx = 5;
if(i == 1)pong_ball.vx = -5;
pong_ball.vy = 0;
pong_ball.x = 0;
pong_ball.y = 0;
}
void game::border(){
glColor4f(0,1,0,0);
glVertex2f(-2,600);
glVertex2f(2,600);
glVertex2f(2,-600);
glVertex2f(-2,-600);
glColor3f(1,1,1);
}
void game::draw_border(){
glColor3f(0.1,0.6,0.1);
glVertex2f(610,610);
glVertex2f(600,610);
glVertex2f(600,-610);
glVertex2f(610,-610);
glColor3f(1,1,1);
glColor4f(0.1,0.6,0.1,0);
glVertex2f(610,610);
glVertex2f(610,600);
glVertex2f(-610,600);
glVertex2f(-610,610);
glColor3f(1,1,1);
glColor4f(0.1,0.6,0.1,0);
glVertex2f(-610,-610);
glVertex2f(-610,610);
glVertex2f(-600,610);
glVertex2f(-600,-610);
glColor3f(1,1,1);
glColor4f(0.1,0.6,0.1,0);
glVertex2f(-610,-610);
glVertex2f(610,-610);
glVertex2f(610,-600);
glVertex2f(-610,-600);
glColor3f(1,1,1);
}
void reflector::move(){
if((y >= -player.winhei + size/2)&&(y <= player.winhei - size/2)){y += vy;}
if((y < -player.winhei + size/2)){y =-player.winhei + size/2;vy = 0;}
if((y > player.winhei - size/2)){y = player.winhei - size/2;vy = 0;}
}
void game::win(){
if(pong_ball.x < left.x){
player.start_settings();
score_right++;
}
if(pong_ball.x > right.x){
player.start_settings();
score_left++;
}
if((score_left==7)||(score_right==7))exit(0);
}
void game::draw_score_l(){
glColor3f(1,1,1);
for(int i = 0; i < score_left; i++){
glVertex2f(-610 + i*50,630);
glVertex2f(-606+i*50,630);
glVertex2f(-606+i*50,620);
glVertex2f(-610+i*50,620);
}
}
void game::draw_score_r(){
glColor3f(1,1,1);
for(int i = 0; i < score_right; i++){
glVertex2f(610 - i*50,630);
glVertex2f(606 - i*50,630);
glVertex2f(606 - i*50,620);
glVertex2f(610 - i*50,620);
}
}
void keyboard(unsigned char key, int x,int y){
switch(key){
case 'q' :
if((left.y <= player.winhei - left.size/2)&&(!player.done_left))left.vy=7;
player.done_left = true;
break;
case 'z' :
if((left.y >= -player.winhei + left.size/2)&&(!player.done_left))left.vy=-7;
player.done_left = true;
break;
case 'a' :
if((left.y >= -player.winhei + left.size/2)&&(!player.done_left))left.vy=0;
player.done_left = true;
break;
case 'x' : exit(0);
}
switch(key){
case 'p' :
if((right.y <= player.winhei - right.size/2)&&(!player.done_right))right.vy=7;
player.done_right = true;
break;
case ',' :
if((right.y >= -player.winhei + right.size/2)&&(!player.done_right))right.vy=-7;
player.done_right = true;
break;
case 'l' :
if((right.y >= -player.winhei + right.size/2)&&(!player.done_right))right.vy=0;
player.done_right = true;
break;
case 'x' : exit(0);
}
}
void Timer (int value){
player.done_left = false;
player.done_right = false;
left.move();
right.move();
pong_ball.move();
pong_ball.reflection();
player.floor_reflect();
player.win();
glutPostRedisplay();
glutTimerFunc(20,Timer,0);
}
void draw(){
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glBegin(GL_QUADS);
glColor3f(0,0,1);
right.draw_r();
glColor3f(1,0,1);
left.draw_r();
glColor3f(1,1,1);
pong_ball.draw_ball();
player.border();
player.draw_border();
player.draw_score_l();
player.draw_score_r();
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void Initialization(int winwid, int winhei){
glClearColor(0,0,0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glScalef(0.8,0.8,0.8);
glOrtho(-winwid,winwid,-winhei,winhei,-2,2);
}
int main (int argc, char ** argv){
srand(time(NULL));
player.start_settings();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(player.winwid, player.winhei);
glutInitWindowPosition(200, 100);
glutCreateWindow("***PONG***");
glutDisplayFunc(draw);
glutTimerFunc(100,Timer,0);
glutKeyboardFunc(keyboard);
Initialization(player.winwid, player.winhei);
glutMainLoop();
system("pause >> NULL");
return(0);
} |