ปัญหาเกมงูกินหาง
#include<stdio.h> #include<conio.h> #include<windows.h> #include<time.h> #include <stdlib.h> void random(); void grid(); void grid2(); void check(); void delay(unsigned int mseconds); void SetColorAndBackground(int ForgC, int BackC); void gotoxy(int x,int y); COORD coord={0,0}; void game(); void game2(); int x2,y2,tail=0,x=20,y=10; char ch_rand[]="CPE";
main() { char ch; int choice=1; do { system("cls"); gotoxy(10,3); if(choice==1){SetColorAndBackground(15,2);} printf("GAME"); SetColorAndBackground(15,0);
gotoxy(10,4); if(choice==2){SetColorAndBackground(15,2);} printf("XO Game "); SetColorAndBackground(15,0);
gotoxy(10,5); if(choice==3){SetColorAndBackground(15,2);} printf("Menu 3 "); SetColorAndBackground(15,0);
gotoxy(10,6); if(choice==4){SetColorAndBackground(15,2);} printf("EXIT "); SetColorAndBackground(15,0);
ch=getch(); switch(ch) { //Up case 72: choice=choice-1; break; //down case 80: choice=choice+1; break; }
if(choice>4)choice=1; if(choice<1)choice=4;
if(ch==13) { switch(choice) { case 1: game();break; case 2: game2();break; //case 3: case 4: return 0; break; } } }while(ch!=27);
} void game() { int c,i,right; c=strlen(ch_rand); system("cls"); char ch; do { gotoxy(0,1); printf("ESC to Exit"); random(); do { grid(); gotoxy(35,2); printf("\t\t\tX %d Y : %d C : %d",x,y,c); ch=getch(); gotoxy(x,y); printf(" "); check();
switch(ch) { case 72:y--;break; //up case 80:y++;break; //down case 75:x--;break; //left case 77:x++;break; //right case 27:return 0;break; } gotoxy(x,y); printf("*");
}while((x!=x2)||(y!=y2)); gotoxy(30+tail,2); printf("%c",ch_rand[tail]);
if(tail==c-1) { system("cls"); printf("FINISH! "); for(i=0;i<25;i++) { delay(50); printf("*"); delay(50); } return 0; } tail++; }while(ch!=27); } void game2() { char ch; system("cls"); do { grid2();
gotoxy(16,5); gotoxy(16,6); printf(" # #\n"); gotoxy(16,7); printf(" # #\n"); gotoxy(16,8); printf("# #\n");
ch=getch();
}while(ch!=27); }
void grid2() { int i; for(i=0;i<18;i++) { gotoxy(15,i+4); printf("|"); gotoxy(48,i+5); printf("|"); gotoxy(26,i+5); printf("|"); gotoxy(37,i+5); printf("|"); }
for(i=0;i<34;i++) { gotoxy(15+i,4); printf("-"); gotoxy(15+i,22); printf("-"); gotoxy(15+i,10); printf("-"); gotoxy(15+i,16); printf("-"); } } void grid() { int i; for(i=0;i<17;i++) { gotoxy(5,i+4); printf("*"); gotoxy(72,i+5); printf("*"); } for(i=0;i<68;i++) { gotoxy(5+i,4); printf("*"); gotoxy(5+i,21); printf("*"); } } void random() { srand (time(NULL)); x2=rand()%55+10; y2=rand()%14+6; gotoxy(x2,y2); printf("%c",ch_rand[tail]); gotoxy(30,3); printf("POINT X: %d Y: %d",x2,y2); }
void delay(unsigned int mseconds) { clock_t goal = mseconds + clock(); while (goal > clock()); }
void SetColorAndBackground(int ForgC, int BackC) { WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor); return; } void gotoxy(int x,int y) { coord.X=x; coord.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord); } void check() {
if((x<7)||(x>70)) { if(x<7) x=7; else x=70; } if((y>20)||(y<5)) { if(y>20)y=20; else y=5; } }
เกม 1 นะครับ ผมมีปัญหาเกมงูกินหาง ขอแนวคิดกินตัวอักษรแล้วหางยาวนะครับ
|