1+ #include " iGraphics.h"
2+
3+ #define min (a, b ) a > b ? b : a
4+ #define max (a, b ) a < b ? b : a
5+
6+ int ball_1_x = 200 , ball_1_y = 200 ;
7+ int ball_2_x = 300 , ball_2_y = 300 ;
8+
9+ int ball_radius = 20 ;
10+ int dir_1_x = 0 , dir_1_y = 0 ;
11+ int dir_2_x = 0 , dir_2_y = 0 ;
12+
13+ void iDraw ()
14+ {
15+ // Clear the screen
16+ iClear ();
17+
18+ // Set drawing color Red
19+ iSetColor (255 , 0 , 0 );
20+ iFilledCircle (ball_1_x, ball_1_y, ball_radius);
21+
22+ // Set drawing color Blue
23+ iSetColor (0 , 0 , 255 );
24+ iFilledCircle (ball_2_x, ball_2_y, ball_radius);
25+ }
26+
27+ void iKeyPress (unsigned char key)
28+ {
29+ switch (key)
30+ {
31+ case ' w' : // Move up
32+ dir_1_y = 1 ;
33+ break ;
34+ case ' s' : // Move down
35+ dir_1_y = -1 ;
36+ break ;
37+ case ' a' : // Move left
38+ dir_1_x = -1 ;
39+ break ;
40+ case ' d' : // Move right
41+ dir_1_x = 1 ;
42+ break ;
43+ case ' q' : // Quit the program
44+ iExitMainLoop ();
45+ break ;
46+ default :
47+ break ;
48+ }
49+ }
50+
51+ void iKeyRelease (unsigned char key)
52+ {
53+ switch (key)
54+ {
55+ case ' w' :
56+ case ' s' :
57+ dir_1_y = 0 ;
58+ break ;
59+ case ' a' :
60+ case ' d' :
61+ dir_1_x = 0 ;
62+ break ;
63+ default :
64+ break ;
65+ }
66+ }
67+
68+ void iSpecialKeyPress (unsigned char key)
69+ {
70+ switch (key)
71+ {
72+ case GLUT_KEY_UP :
73+ dir_2_y = 1 ;
74+ break ;
75+ case GLUT_KEY_DOWN :
76+ dir_2_y = -1 ;
77+ break ;
78+ case GLUT_KEY_LEFT :
79+ dir_2_x = -1 ;
80+ break ;
81+ case GLUT_KEY_RIGHT :
82+ dir_2_x = 1 ;
83+ break ;
84+ default :
85+ break ;
86+ }
87+ }
88+
89+ void iSpecialKeyRelease (unsigned char key)
90+ {
91+ switch (key)
92+ {
93+ case GLUT_KEY_UP :
94+ case GLUT_KEY_DOWN :
95+ dir_2_y = 0 ;
96+ break ;
97+ case GLUT_KEY_LEFT :
98+ case GLUT_KEY_RIGHT :
99+ dir_2_x = 0 ;
100+ break ;
101+ default :
102+ break ;
103+ }
104+ }
105+
106+ void ballMovement ()
107+ {
108+ ball_1_x = max (min (ball_1_x + dir_1_x * 10 , iScreenWidth - ball_radius), ball_radius);
109+ ball_1_y = max (min (ball_1_y + dir_1_y * 10 , iScreenHeight - ball_radius), ball_radius);
110+
111+ ball_2_x = max (min (ball_2_x + dir_2_x * 10 , iScreenWidth - ball_radius), ball_radius);
112+ ball_2_y = max (min (ball_2_y + dir_2_y * 10 , iScreenHeight - ball_radius), ball_radius);
113+ }
114+
115+ int main (int argc, char *argv[])
116+ {
117+ iSetTimer (20 , ballMovement);
118+ iWindowedMode (400 , 400 , " iGraphics" );
119+ iStartMainLoop ();
120+ return 0 ;
121+ }
0 commit comments