Programa em Console

1 #include <stdio.h>
2 #include <time.h>
3 #include <stdlib.h>
4 #include <conio.h>
5 #include <ctype.h>
6 #include <windows.h>
7 #include <process.h>
8
9 #define UP 72
10 #define DOWN 80
11 #define LEFT 75
12 #define RIGHT 77
13
14 int length;
15 int bend_no;
16 int len;
17 char key;
18
19 void record();
20 void load();
21
22 int life;
23
24 void Delay(long double);
25 void Move();
26 void Alimento();
27
28 int Pontos();
29
30 void Print();
31 void gotoxy(int x, int y);
32 void GotoXY(int x,int y);
33 void Bend();
34 void Boarder();
35 void Down();
36 void Left();
37 void ParaCima();
38 void Right();
39 void ExitGame();
40 int Scoreonly();
41
42 struct coordinate {
43 int x;
44 int y;
45 int direction;
46 };
47
48 typedef struct coordinate coordinate;
49
50 coordinate head, bend[500],food,body[30];
51
52 int main() {
53 system("title Snake Game");
54 system("color 03");
55 char key;
56 Print();
57 system("cls");
58 load();
59 length=5;
60 head.x=25;
61 head.y=20;
62 head.direction = RIGHT;
63 Boarder();
64 Alimento();
65 life=3;
66 bend[0] = head;
67 Move();
68 return 0;
69 }
70
71 void Move() {
72 int a,i;
73 do {
74 Alimento();
75 fflush(stdin);
76 len=0;
77 for(i=0; i<30; i++) {
78 body[i].x=0;
79 body[i].y=0;
80 if(i==length)
81 break;
82 }
83
84 Delay(length);
85
86 Boarder();
87
88 if(head.direction==RIGHT)
89 Right();
90
91 else if(head.direction==LEFT)
92 Left();
93
94 else if(head.direction==DOWN)
95
96 Down();
97
98 else if(head.direction==UP)
99
100 ParaCima();
101
102 ExitGame();
103
104 }while(!kbhit());
105
106 a=getch();
107
108 if(a==27) {
109 system("cls");
110 exit(0);
111 }
112 key=getch();
113
114 if((key==RIGHT&&head.direction!=
115 LEFT&&head.direction!=RIGHT)||
116 (key==LEFT&&head.direction!=
117 RIGHT&&head.direction!=LEFT)||
118 (key==UP&&head.direction!=
119 DOWN&&head.direction!=UP)||
120 (key==DOWN&&head.direction!=
121 UP&&head.direction!=DOWN)) {
122
123 bend_no++;
124 bend[bend_no]=head;
125 head.direction=key;
126 if(key==UP)
127 head.y--;
128
129 if(key==DOWN)
130 head.y++;
131
132 if(key==RIGHT)
133 head.x++;
134
135 if(key==LEFT)
136 head.x--;
137
138 Move();
139 }
140 else if(key==27) {
141 system("cls");
142 exit(0);
143 }
144 else {
145 printf("\a");
146 Move();
147 }
148 }
149
150 void gotoxy(int x, int y) {
151 COORD coord;
152 coord.X = x;
153 coord.Y = y;
154 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
155 }
156 void GotoXY(int x, int y) {
157 HANDLE a;
158 COORD b;
159 fflush(stdout);
160 b.X = x;
161 b.Y = y;
162 a = GetStdHandle(STD_OUTPUT_HANDLE);
163 SetConsoleCursorPosition(a,b);
164 }
165
166 void load(){
167 int row,col,r,c,q;
168
169 gotoxy(36,14);
170 printf("Carregando...");
171 gotoxy(30,15);
172
173 for(r=1; r<=20; r++){
174 for(q=0;q<=100000000;q++);
175 printf("%c",177);
176 }
177 getch();
178 }
179
180 void Down() {
181 int i;
182 for(i=0;i<=(head.y-bend[bend_no].y)&&len<length;i++) {
183 GotoXY(head.x,head.y-i);
184 {
185 if(len==0)
186 printf("\xdb");
187 else
188 printf("\xdb");
189 }
190 body[len].x=head.x;
191 body[len].y=head.y-i;
192 len++;
193 }
194 Bend();
195 if(!kbhit())
196 head.y++;
197 }
198 void Delay(long double k) {
199 Pontos();
200 long double i;
201 for(i=0; i<=(10000000); i++);
202 }
203
204 void ExitGame() {
205 int i,check=0;
206 for(i=4;i<length;i++) {
207 if(body[0].x==body[i].x&&body[0].y==body[i].y) {
208 check++;
209 }
210 if(i==length||check!=0)
211 break;
212 }
213 if(head.x<=10||head.x>=70||head.y<=10||head.y>=30||check!=0) {
214 life--;
215 if(life>=0) {
216 head.x=25;
217 head.y=20;
218 bend_no=0;
219 head.direction=RIGHT;
220 Move();
221 }
222 else {
223 system("cls");
224 printf("Vidas Completadas\nBoa sorte no proximo jogo!!!");
225 printf("\nAperte qualquer tecla para sair do jogo\n");
226 record();
227 exit(0);
228 }
229 }
230 }
231 void Alimento() {
232 if(head.x==food.x && head.y==food.y) {
233 length++;
234 time_t a;
235 a=time(0);
236 srand(a);
237 food.x = rand() % 70;
238
239 if(food.x<=10)
240 food.x+=11;
241
242 food.y=rand()%30;
243
244 if(food.y<=10)
245 food.y+=11;
246 }
247 else if(food.x==0) {
248 food.x=rand()%70;
249 if(food.x<=10)
250 food.x+=11;
251 food.y=rand()%30;
252 if(food.y<=10)
253 food.y+=11;
254 }
255 }
256 void Left() {
257 int i;
258 for(i=0; i<=(bend[bend_no].x-head.x) && len<length; i++) {
259 GotoXY((head.x+i),head.y);
260 {
261 if(len == 0)
262 printf("\xdb");
263 else
264 printf("\xdb");
265 }
266 body[len].x = head.x+i;
267 body[len].y = head.y;
268 len++;
269 }
270 Bend();
271 if(!kbhit())
272 head.x--;
273
274 }
275 void Right() {
276 int i;
277 for(i=0; i<=(head.x-bend[bend_no].x) && len<length; i++) {
278 body[len].x = head.x-i;
279 body[len].y = head.y;
280
281 GotoXY(body[len].x, body[len].y);
282
283 {
284 if(len==0)
285 printf("\xdb");
286 else
287 printf("\xdb");
288 }
289 len++;
290 }
291 Bend();
292 if(!kbhit())
293 head.x++;
294 }
295 void Bend() {
296 int i,j,diff;
297
298 for(i=bend_no; i>=0 && len<length; i--) {
299 if(bend[i].x==bend[i-1].x) {
300 diff=bend[i].y-bend[i-1].y;
301 if(diff<0)
302 for(j=1;j<=(-diff);j++) {
303 body[len].x=bend[i].x;
304 body[len].y=bend[i].y+j;
305 GotoXY(body[len].x,body[len].y);
306 printf("\xdb");
307 len++;
308 if(len==length)
309 break;
310 }
311 else if(diff>0)
312 for(j=1;j<=diff;j++) {
313 body[len].x=bend[i].x;
314 body[len].y=bend[i].y-j;
315 GotoXY(body[len].x,body[len].y);
316 printf("\xdb");
317 len++;
318 if(len==length)
319 break;
320 }
321 }
322 else if(bend[i].y==bend[i-1].y) {
323 diff=bend[i].x-bend[i-1].x;
324 if(diff<0)
325 for(j=1;j<=(-diff)&&len<length;j++) {
326 body[len].x=bend[i].x+j;
327 body[len].y=bend[i].y;
328 GotoXY(body[len].x,body[len].y);
329 printf("\xdb");
330 len++;
331
332 if(len==length)
333 break;
334 }
335 else if(diff>0)
336 for(j=1; j<=diff && len<length; j++) {
337 body[len].x=bend[i].x-j;
338 body[len].y=bend[i].y;
339 GotoXY(body[len].x,body[len].y);
340 printf("\xdb");
341 len++;
342
343 if(len==length)
344 break;
345 }
346 }
347 }
348 }
349
350 void Boarder() {
351 system("cls");
352 int i;
353 GotoXY(food.x,food.y);
354 printf("\xBB1");
355 for(i=10;i<71;i++) {
356 GotoXY(i,10);
357 printf("!");
358 GotoXY(i,30);
359 printf("!");
360 }
361 for(i=10;i<31;i++) {
362 GotoXY(10,i);
363 printf("!");
364 GotoXY(70,i);
365 printf("!");
366 }
367 }
368
369 void Print() {
370 printf("\tBem Vindo ao mini Snake game.(Aperte qualquer tecla..)\n");
371 getch();
372 system("cls");
373 printf("\tInstrucoes do Jogo:\n");
374 printf("\n-> Use arrow keys to move the snake.\n\n");
375 printf("-> Para cima, para baixo, para direita e para esquerda");
376 printf("\n\n-> Voce tem 3 vidas disponiveis\n\n");
377 printf("-> Para pausar o jogo, aperte qualquer TC no meio do jogo");
378 printf("\n\n-> Para sair aperte ESC\n");
379 printf("\n\nAperte qualquer tecla para Iniciar o Jogo...");
380 if(getch()==27)
381 exit(0);
382 }
383 void record(){ /**Gravar*/
384 char plname[20],nplname[20],cha,c;
385 int i,j,px;
386 FILE *info;
387
388 info = fopen("record.txt","a+");
389 getch();
390 system("cls");
391
392 printf("Digite seu nome\n");
393 scanf("%[^\n]",plname);
394
395 for(j=0; plname[j]!='\0'; j++){
396 nplname[0]=toupper(plname[0]);
397 if(plname[j-1]==' ') {
398 nplname[j]=toupper(plname[j]);
399 nplname[j-1]=plname[j-1];
400 }
401 else nplname[j]=plname[j];
402 }
403 nplname[j]='\0';
404 fprintf(info,"Nome do Jogador :%s\n",nplname);
405 time_t mytime;
406 mytime = time(NULL);
407
408 fprintf(info,"Played Date:%s",ctime(&mytime));
409 fprintf(info,"Pontos:%d\n",px=Scoreonly());
410
411 for(i=0;i<=50;i++)
412 fprintf(info,"%c",'_');
413 fprintf(info,"\n");
414 fclose(info);
415
416 printf("wanna see past records press 'y'\n");
417 cha=getch();
418
419 system("cls");
420 if(cha=='y'){
421 info=fopen("record.txt","r");
422 do {
423 putchar(c=getc(info));
424 } while(c!=EOF);
425 }
426 fclose(info);
427 }
428 int Pontos() {
429 int score;
430 GotoXY(20,8);
431 score=length-5;
432 printf("Pontos : %d",(length-5));
433 score=length-5;
434 GotoXY(50,8);
435 printf("Vida : %d",life);
436 return score;
437 }
438
439 int Scoreonly() { /**Pontos*/
440 int score=Pontos();
441 system("cls");
442 return score;
443 }
444
445 void ParaCima() {
446 int i;
447 for(i=0;i<=(bend[bend_no].y-head.y)&&len<length;i++) {
448 GotoXY(head.x,head.y+i);
449 {
450 if(len==0)
451 printf("\xdb");
452 else
453 printf("\xdb");
454 }
455 body[len].x=head.x;
456 body[len].y=head.y+i;
457 len++;
458 }
459 Bend();
460 if(!kbhit())
461 head.y--;
462 }
463
Comentários
Postar um comentário