-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
286 lines (223 loc) · 6.56 KB
/
main.c
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <termios.h>
#include <graphics.h>
static struct termios initial_settings, new_settings;
static int peek_character = -1;
void init_keyboard()
{
tcgetattr(0,&initial_settings);
new_settings = initial_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECHO;
new_settings.c_cc[VMIN] = 1;
new_settings.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &new_settings);
}
void close_keyboard()
{
tcsetattr(0, TCSANOW, &initial_settings);
}
int _kbhit()
{
unsigned char ch;
int nread;
if (peek_character != -1) return 1;
new_settings.c_cc[VMIN]=0;
tcsetattr(0, TCSANOW, &new_settings);
nread = read(0,&ch,1);
new_settings.c_cc[VMIN]=1;
tcsetattr(0, TCSANOW, &new_settings);
if(nread == 1)
{
peek_character = ch;
return 1;
}
return 0;
}
int _getch()
{
char ch;
if(peek_character != -1)
{
ch = peek_character;
peek_character = -1;
return ch;
}
read(0,&ch,1);
return ch;
}
int _putch(int c) {
putchar(c);
fflush(stdout);
return c;
}
/* manipulates the position of planets on the orbit */
void Motion(int xrad, int yrad, int mmx, int mmy, int a[1], int b[1]) {
int i, j = 0;
mmx = mmx + 290;
mmy = mmy + 50;
/* positions of ball in ellipse*/
for (i = 180; i > 0; i = i - 6) {
a[j] = mmx - (xrad * cos((i * 3.14) / 180));
b[j++] = mmy - (yrad * sin((i * 3.14) / 180));
}
return;
}
void AntiMotion(int xrad, int yrad, int mmx, int mmy, int a[1], int b[1]) {
int i, j = 0;
mmx = mmx + 290;
mmy = mmy + 50;
/* positions of ball in ellipse*/
for (i = 290; i > 0; i = i - 6) {
a[j] = mmx + (xrad * sin((i * 3.14) / 180));
b[j++] = mmy + (yrad * cos((i * 3.14) / 180));
}
return;
}
int main()
{
/* request auto detection */
int gd = DETECT, gm, err;
int radius=10;
int x, y, midy,midx;
int i=0;
int xx=0,yy=0;
int xrad, yrad, a[9][60], b[9][60];
int tmp;
int posn=30;
int xa,ya,xb,yb;
int s_xa,s_ya,s_xb,s_yb;
int mmx,mmy;
int count;
char text[] = "Score:";
int sc =0;
/* initialize graphic mode */
initgraph (&gd, &gm,NULL);
x = 40;
y = midy+100;
/*mid positions at x and y*/
midx = getmaxx() /2;
midy = getmaxy() / 2;
xrad=290;
yrad=200;
xa=x;
ya=y;
xb=x;
yb=y;
init_keyboard();
while (1) {
/* clears graphic screen */
//cleardevice();
xa=x+30;
ya=getmaxy()-100;
xb=getmaxx()-50;
yb=getmaxy()-50;
mmx = midx -260;
mmy = midy;
Motion(xrad, yrad, mmx, mmy, a[8], b[8]);
while(1){
s_xa = xa -30 - radius;
s_ya = ya -100 ;
s_xb = xb - radius;
s_yb = yb -150 ;
setlinestyle(SOLID_LINE, 1, 3);
/* road for stick man */
line(0, getmaxy() - 50, getmaxx(),getmaxy() -50);
/*net design*/
line(midx,getmaxy() - 50, midx, midy+10);
line(midx,midy+10,midx-60,midy-100);
line(midx-60,midy-100,midx-60,midy);
line(midx,getmaxy()-120,midx-60,midy-20);
line(0,midy,getmaxx(),midy);
/* image 1 -position of 1st stick man */
circle(xa-30, ya-100, radius);
line(xa-30, ya + radius-100, xa-30, ya + radius - 70);
/* leg design */
line(xa-30, ya + radius - 70, xa - 20, ya +radius -50);
line(xa-30, ya + radius - 70, xa - 40, ya +radius -50);
/* hand design */
line(xa-30, ya + radius -90, xa - 15, ya + radius -80 );
line(xa-30, ya + radius -90, xa -45, ya + radius -80 );
/*image 2 - position of 2nd stickman*/
circle(xb,yb-150,radius);
line(xb, yb + radius-150,xb,yb + radius -120);
/*leg design*/
line(xb,yb+radius-120,xb-10,yb+ radius -100);
line(xb,yb+radius-120,xb+10,yb +radius -100);
/*hand design*/
line(xb, yb + radius - 140,xb-15, yb + radius - 130);
line(xb, yb + radius - 140,xb+15, yb + radius - 130);
/*ball design*/
/* drawing orbits */
setcolor(WHITE);
outtextxy(250, 40, text);
printf("%d",sc);
/* ball */
setcolor(WHITE);
//setfillstyle(SOLID_FILL, LIGHTRED);
pieslice(a[8][posn], b[8][posn], 0, 360, radius);
/* checking for one complete rotation */
mmx = mmx -260 - xrad;
if (mmx = s_xa){
if (posn>0){
posn = posn - 1;
count++;
}
}
if (count==30){
sc++;
}
if (posn <= 0) {
while(posn!=30){
for (i=0;i<30;i++){
posn = posn + 1;
}
AntiMotion(xrad, yrad, mmx, mmy, a[8], b[8]);
count++;
}
}
if (count>=58){
Motion(xrad, yrad, mmx, mmy, a[8], b[8]);
posn = 30;
count =0;
}
if (count==56){
sc++;
}
if (mmx != s_xa){
sc = 0;
}
/* sleep for 100 milliseconds */
delay(100);
/* sleeps for 50 milliseconds */
delay(50);
if (_kbhit()) {
int ch = _getch();
_putch(ch);
//for player1
if (ch=='w') ya--; mmy--;
if (ch=='s') ya++; mmy++;
if (ch=='a') xa--; mmx--;
if (ch=='d') xa++; mmy++;
//for player2
if (ch=='i') yb--; mmy--;
if (ch=='k') yb++; mmy++;
if (ch=='j') xb--; mmx--;
if (ch=='l') xb++; mmx++;
}
cleardevice();
}
delay(50);
}
//getch();
close_keyboard();
/* deallocate memory allocated for graphic screen */
delay(50);
closegraph();
return 0;
}