-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path21. DXBall2.cpp
executable file
·219 lines (164 loc) · 3.86 KB
/
21. DXBall2.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
#define _CRT_SECURE_NO_WARNINGS
#include <GL/glut.h>///#include <glut.h>
#include <stdio.h>
#include <GL/gl.h>///#include <glut.h>
#include <GL/glu.h>///#include <glut.h>
//#include<bits/stdc++.h>
#include<string.h>
#include<stdio.h>
#define FROM_RIGHT 1
#define FROM_LEFT 2
#define FROM_TOP 3
#define FROM_BOTTOM 4
static int WINDOW_WIDTH, WINDOW_HEIGHT;
int playerResult = 0;
static float Xspeed = 2, Yspeed = 2;
static float delta = 2;
int flag_pg = 0;
char string[100];
struct RECTA
{
float left, top, right, bottom;
};
RECTA ball = { 100, 100, 120, 120 };
RECTA wall;
RECTA player_1 = { 0, 790, 80, 800 };
void DrawRectangle(RECTA rect)
{
glBegin(GL_QUADS);
glVertex2f(rect.left, rect.bottom); //Left - Bottom
glVertex2f(rect.right, rect.bottom);
glVertex2f(rect.right, rect.top);
glVertex2f(rect.left, rect.top);
glEnd();
}
void Timer(int v)
{
/* ??? ???? ????? ?? ?? 5 ?? ????? ?? ??????? */
ball.left += Xspeed;
ball.right += Xspeed;
ball.top += Yspeed;
ball.bottom += Yspeed;
glutTimerFunc(1, Timer, 1); // ??? ????? ???? ??? ?? ??? ?????? ?????? ??????
}
void drawText(char*string, int x, int y)
{
char *c;
glPushMatrix();
glTranslatef(x, y, 0);
glScalef(0.1, -0.1, 1);
for (c = string; *c != '\0'; c++)
{
glutStrokeCharacter(GLUT_STROKE_ROMAN, *c);
}
glPopMatrix();
}
int Test_Ball_Wall(RECTA ball, RECTA wall)
{
if (ball.right >= wall.right)
return FROM_RIGHT;
if (ball.left <= wall.left)
return FROM_LEFT;
if (ball.top <= wall.top)
return FROM_TOP;
if (ball.bottom >= wall.bottom)
return FROM_BOTTOM;
else return 0;
}
bool Test_Ball_Player(RECTA ball, RECTA player)
{
if (ball.bottom >= player.top && ball.left >= player.left && ball.right <= player.right)
{
playerResult++;
if ((playerResult + 1) % 2 == 0 )
delta += 1;
return true;
}
else if (ball.bottom >= player.bottom && (ball.left < player.left || ball.right > player.right))
{
flag_pg = 1;
glutPostRedisplay();
}
return false;
}
static int mouse_x = 0;
void MouseMotion(int x, int y)
{
mouse_x = x;
}
// OpenGL Setting
void Setting(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
// OnWindowResize
void reshape(int w, int h)
{
WINDOW_WIDTH = w;
WINDOW_HEIGHT = h;
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, h, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int pcResult = 0;
void Render()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
if (flag_pg == 0)
{
//sprintf(string, "PC : %d ", pcResult);
//drawText(string, 10, 80);
sprintf(string, "Player : %d ", playerResult);
drawText(string, 10, 120);
wall.left = wall.top = 0;
wall.right = WINDOW_WIDTH;
wall.bottom = WINDOW_HEIGHT;
DrawRectangle(ball);
if (Test_Ball_Wall(ball, wall) == FROM_RIGHT)
Xspeed = -delta;
if (Test_Ball_Wall(ball, wall) == FROM_LEFT)
Xspeed = delta;
if (Test_Ball_Wall(ball, wall) == FROM_TOP)
Yspeed = delta;
if (Test_Ball_Wall(ball, wall) == FROM_BOTTOM)
{
Yspeed = -delta;
pcResult += 1;
}
DrawRectangle(player_1);
player_1.left = mouse_x - 60;
player_1.right = mouse_x + 60;
if (Test_Ball_Player(ball, player_1) == true)
Yspeed = -delta;
}
else if (flag_pg == 1)
{
drawText("GAME OVER", 450, 200);
sprintf(string, "SCORE IS %d ", playerResult);
drawText(string, 450, 250);
}
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(1400, 1000);
glutInitWindowPosition(200, 200);
glutCreateWindow("DX BALL OpenGL game");
Setting();
glutDisplayFunc(Render);
glutIdleFunc(Render);
glutTimerFunc(1, Timer, 1);
glutReshapeFunc(reshape);
//glutKeyboardFunc(keyboard);
//glutSpecialFunc(inputKey);
glutPassiveMotionFunc(MouseMotion);
glutMainLoop();
return 0;
}