C中的2个蛇程序 [英] 2 snake program in C
本文介绍了C中的2个蛇程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我试图在以下游戏中为2名玩家显示2条蛇,但终端只显示一条蛇,任何一个帮助如何显示2条蛇
我尝试过:
Hi
I'm trying to display 2 snakes in the following game for 2 Players, but the terminal showing only one snake, is any one help how to display 2 snakes
What I have tried:
// #include<stdafx.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
#include<time.h>
#include<ctype.h>
#include <time.h>
#include <windows.h>
#include <process.h>
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define UP2 119 ///W
#define DOWN2 120 ///X
#define LEFT2 97 ///A
#define RIGHT2 100 /// D
int length;
int length2;
int bend_no;
int len;
char key;
char key2;
int life;
void record();
void load();
void Delay(long double);
void Move();
void Move2();
void gotoxy(int x, int y);
void GotoXY(int x,int y);
void GotoXY2(int x,int y);
void Bend();
void Bend2();
void Boarder();
void Down();
void Down2();
void Left();
void Left2();
void Up();
void Up2();
void Print();
void Food();
int Score();
void Right();
void Right2();
void ExitGame();
int Scoreonly();
int Scoreonly2();
struct coordinate{
int x;
int y;
int direction;
};
typedef struct coordinate coordinate;
coordinate head,bend[500],food,body[30],bend2[500],head2,body2[30];
int main()
{
char key;
Print();
system("cls");
load();
length=5; //snake length
head.x=25;
head.y=20;
head.direction=RIGHT;
length2=5;
head2.x=25;
head2.y=20;
head2.direction=UP2;
Boarder();
Food(); //to generate the initial food blob coordinates
life=3; //number of lives the game player have
bend[0]=head;
bend2[0]=head2;
Move();
Move2(); //initializing initial bend coordinate
return 0;
}
void Move()
{
int a,i;
do{
Food();
// fflush(stdin);
len=0;
for(i=0;i<30;i++)
{
body[i].x=0;
body[i].y=0;
if(i==length)
break;
}
Delay(length);
Boarder();
if(head.direction==RIGHT)
Right();
else if(head.direction==LEFT)
Left();
else if(head.direction==DOWN)
Down();
else if(head.direction==UP)
Up();
ExitGame();
}while(!kbhit()); //if v entr any key othr than arrows thn game vil pause
a=getch();
if(a==27)
{
system("cls");
exit(0);
}
key=getch();
if((key==RIGHT&&head.direction!=LEFT&&head.direction!=RIGHT)||(key==LEFT&&head.direction!=RIGHT&&head.direction!=LEFT)||(key==UP&&head.direction!=DOWN&&head.direction!=UP)||(key==DOWN&&head.direction!=UP&&head.direction!=DOWN))
{
bend_no++;
bend[bend_no]=head;
head.direction=key;
if(key==UP)
head.y++; //first it was --
if(key==DOWN)
head.y--; //first it was ++
if(key==RIGHT)
head.x++;
if(key==LEFT)
head.x--;
Move();
}
/* else if(key==27)
{
system("cls");
exit(0);
}*/
else
{
printf("\a");
Move();
}
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void GotoXY(int x, int y)
{
HANDLE a;
COORD b;
// fflush(stdout);
b.X = x;
b.Y = y;
a = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(a,b);
}
void load()
{
int row,col,r,c,q;
gotoxy(36,14);
printf("HariGameloading...");
gotoxy(30,15);
for(r=1;r<=20;r++)
{
for(q=0;q<=100000000;q++);//to display the character slowly
printf("%c",177);
}
getch();
}
void Down()
{
int i;
for(i=0;i<=(head.y-bend[bend_no].y)&&len<length;i++)
{
GotoXY(head.x,head.y-i);
{
if(len==0)
printf("v");
else
printf("*");
}
body[len].x=head.x;
body[len].y=head.y-i;
len++;
}
Bend();
if(!kbhit())
head.y++;
}
void Delay(long double k) //delay can also be without any parameter
{
Score();
long double i;
for(i=0;i<=(10000000);i++);
}
void ExitGame()
{
int i,check=0;
for(i=4;i<length;i++) //starts with 4 because it needs minimum 4 element to touch its own body
{
if(body[0].x==body[i].x&&body[0].y==body[i].y)
{
check++; //checks value increases as the coordinates of head is equal to any other body coordinate
}
// if(i==length||check!=0)
// break;
}
if(head.x<=10||head.x>=70||head.y<=10||head.y>=30||check!=0)
{
life--;
if(life>=0)
{
head.x=25;
head.y=20;
bend_no=0;
head.direction=RIGHT;
Move();
}
else
{
system("cls");
printf("All lives Gone\nBetter Luck Next Time!\nPress any key to quit\n");
record();
exit(0);
}
}
}
void Food()
{
if(head.x==food.x&&head.y==food.y) //head.x=25 head.y=20 in main
{ //gotoxy(food.x,food.y) in boarder()
length++; //if food snake are at same position then length of snake increases by 1 unit
// time_t a;
// a=time(0);
// srand(a);
food.x=rand()%70; //rand() is used to generate any random no. so that food gets displayed at random positions and according to
//border x cannot be greater than 70 so %70 is used
if(food.x<=10)
food.x+=11; // position less than according to border x lies between 10 and 70 so if food is at position less than 10 then it is
//incremented by some value
food.y=rand()%30;
if(food.y<=10) //similar to x
food.y+=11;
}
else if(food.x==0) //to create food for the first time
{
food.x=rand()%70;
if(food.x<=10)
food.x+=11;
food.y=rand()%30;
if(food.y<=10)
food.y+=11;
}
}
void Left()
{
int i;
for(i=0;i<=(bend[bend_no].x-head.x)&&len<length;i++)
{
GotoXY((head.x+i),head.y);
if(len==0)
printf("<");
else
printf("*");
body[len].x=head.x+i;
body[len].y=head.y;
len++;
}
Bend();
if(!kbhit())
head.x--;
}
void Right()
{
int i; //len=0 initialized in Move()
for(i=0;i<=(head.x-bend[bend_no].x)&&len<length;i++)
{
//GotoXY((head.x-i),head.y);
body[len].x=head.x-i; //to move the snake forward in right direction
body[len].y=head.y; //x changes according to i but y remains same
GotoXY(body[len].x,body[len].y);
//{
if(len==0)
printf(">");
else
printf("*");
//}
/*body[len].x=head.x-i;
body[len].y=head.y;*/
len++;
}
Bend();
if(!kbhit())
head.x++;
}
void Bend()
{
int i,j,diff;
for(i=bend_no;i>=0&&len<length;i--) //bend_no=0;
{
// printf("%d\n",bend[i].x);
// printf("%d\n",bend[i-1].x); //bend[i-1].x=25
if(bend[i].x==bend[i-1].x)
{
diff=bend[i].y-bend[i-1].y;
if(diff<0){
for(j=1;j<=(-diff);j++)
{
body[len].x=bend[i].x;
body[len].y=bend[i].y+j;
GotoXY(body[len].x,body[len].y);
printf("*");
len++;
if(len==length)
break;
}
}
else if(diff>0)
for(j=1;j<=diff;j++)
{
/*GotoXY(bend[i].x,(bend[i].y-j));
printf("*");*/
body[len].x=bend[i].x;
body[len].y=bend[i].y-j;
GotoXY(body[len].x,body[len].y);
printf("*");
len++;
if(len==length)
break;
}
}
else if(bend[i].y==bend[i-1].y)
{
diff=bend[i].x-bend[i-1].x;
if(diff<0)
for(j=1;j<=(-diff)&&len<length;j++)
{
/*GotoXY((bend[i].x+j),bend[i].y);
printf("*");*/
body[len].x=bend[i].x+j;
body[len].y=bend[i].y;
GotoXY(body[len].x,body[len].y);
printf("*");
len++;
if(len==length)
break;
}
else if(diff>0)
for(j=1;j<=diff&&len<length;j++)
{
/*GotoXY((bend[i].x-j),bend[i].y);
printf("*");*/
body[len].x=bend[i].x-j;
body[len].y=bend[i].y;
GotoXY(body[len].x,body[len].y);
printf("*");
len++;
if(len==length)
break;
}
}
}
}
void Boarder()
{
system("cls");
int i;
GotoXY(food.x,food.y); /*displaying food*/
printf("F");
for(i=10;i<71;i++)
{
GotoXY(i,10);
printf("!");
GotoXY(i,30);
printf("!");
}
for(i=10;i<31;i++)
{
GotoXY(10,i);
printf("!");
GotoXY(70,i);
printf("!");
}
}
void Print()
{ //GotoXY(10,12);
printf("\tWelcome to the mini Snake game.(press any key to continue)\n");
getch();
// system("cls");
printf("\tGame Play Instructions:\n");
printf("\n-> Use arrow keys to move the snake.\n\n-> You will be provided food blobs at the several coordinates of the screen which you have to make your snake eat. Everytime you eat a food the length of the snake will be increased by 1 element and thus the score.\n\n-> Here you are provided with three lives. Your life will decrease as you hit the wall or snake's body.\n\n-> YOu can pause the game in its middle by pressing any key. To continue the paused game press any other key once again\n\n-> If you want to exit press esc. \n");
printf("\n\nPress any key to continue...");
if(getch()==27)
exit(0);
}
void record(){
char plname[20],nplname[20],cha,c;
int i,j,px;
FILE *info;
info=fopen("record.txt","a+");
getch();
system("cls");
printf("Enter your name\n");
scanf("%s",plname);
//************************
for(j=0;plname[j]!='\0';j++){ //to convert the first letter after space to capital
nplname[0]=toupper(plname[0]);
if(plname[j-1]==' ')
{
nplname[j]=toupper(plname[j]);
nplname[j-1]=plname[j-1];
}
else
nplname[j]=plname[j];
}
nplname[j]='\0';
//*****************************
//sdfprintf(info,"\t\t\tPlayers List\n");
fprintf(info,"Player Name :%s\n",nplname);
//for date and time
time_t mytime;
mytime = time(NULL);
fprintf(info,"Played Date:%s",ctime(&mytime));
//**************************
fprintf(info,"Player1_Score:%d\n",px=Scoreonly());//call score to display score
//fprintf(info,"\nLevel:%d\n",10);//call level to display level
// for(i=0;i<=50;i++)
fprintf(info,"%c",'_');
fprintf(info,"\n");
fclose(info);
printf("wanna see past records press 'y'\n");
cha=getch();
system("cls");
if(cha=='y')
{
info=fopen("record.txt","r");
do{
putchar(c=getc(info));
}while(c!=EOF);
}
fclose(info);
}
int Score()
{
int score;
GotoXY(20,8);
score=length-5;
printf("Player1_SCORE : %d",(length-5));
// score=length-5;
GotoXY(50,8);
printf("Life_P1 : %d",life);
return score;
}
int Scoreonly()
{
int score=Score();
system("cls");
return score;
}
void Up()
{
int i;
for(i=0;i<=(bend[bend_no].y-head.y)&&len<length;i++)
{
GotoXY(head.x,head.y+i);
{
if(len==0)
printf("^");
else
printf("*");
}
body[len].x=head.x;
body[len].y=head.y+i;
len++;
}
Bend();
if(!kbhit())
head.y--;
}
///Player2
void gotoxy2(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void GotoXY2(int x, int y)
{
HANDLE a;
COORD b;
// fflush(stdout);
b.X = x;
b.Y = y;
a = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(a,b);
}
///////////////////////////
void Move2()
{
int a,i;
do{
Food();
// fflush(stdin);
len=0;
for(i=0;i<30;i++)
{
body2[i].x=0;
body2[i].y=0;
if(i==length2)
break;
}
Delay(length2);
Boarder();
if(head2.direction==RIGHT2)
Right2();
else if(head2.direction==LEFT2)
Left2();
else if(head2.direction==DOWN2)
Down2();
else if(head2.direction==UP2)
Up2();
ExitGame();
}while(!kbhit()); //if v entr any key2 othr than arrows thn game vil pause
a=getch();
if(a==27)
{
system("cls");
exit(0);
}
key2=getch();
if((key2==RIGHT2&&head2.direction!=LEFT2&&head2.direction!=RIGHT2)||(key2==LEFT2&&head2.direction!=RIGHT2&&head2.direction!=LEFT2)||(key2==UP2&&head2.direction!=DOWN2&&head2.direction!=UP2)||(key2==DOWN2&&head2.direction!=UP2&&head2.direction!=DOWN2))
{
bend_no++;
bend2[bend_no]=head2;
head2.direction=key2;
if(key2==UP2)
head2.y++; //first it was --
if(key2==DOWN2)
head2.y--; //first it was ++
if(key2==RIGHT2)
head2.x++;
if(key2==LEFT2)
head2.x--;
Move2();
}
/* else if(key2==27)
{
system("cls");
exit(0);
}*/
else
{
printf("\a");
Move2();
}
}
void Down2()
{
int i;
for(i=0;i<=(head2.y-bend2[bend_no].y)&&len<length2;i++)
{
GotoXY2(head2.x,head2.y-i);
{
if(len==0)
printf("v");
else
printf("#");
}
body2[len].x=head2.x;
body2[len].y=head2.y-i;
len++;
}
Bend2();
if(!kbhit())
head2.y++;
}
void Left2()
{
int i;
for(i=0;i<=(bend2[bend_no].x-head2.x)&&len<length2;i++)
{
GotoXY2((head2.x+i),head2.y);
if(len==0)
printf("<");
else
printf("#");
body2[len].x=head2.x+i;
body2[len].y=head2.y;
len++;
}
Bend2();
if(!kbhit())
head2.x--;
}
void Right2()
{
int i; //len=0 initialized in Move2()
for(i=0;i<=(head2.x-bend2[bend_no].x)&&len<length2;i++)
{
//GotoXY2((head2.x-i),head2.y);
body2[len].x=head2.x-i; //to move the snake forward in right direction
body2[len].y=head2.y; //x changes according to i but y remains same
GotoXY2(body2[len].x,body2[len].y);
//{
if(len==0)
printf(">");
else
printf("#");
//}
/*body2[len].x=head2.x-i;
body2[len].y=head2.y;*/
len++;
}
Bend2();
if(!kbhit())
head2.x++;
}
void Bend2()
{
int i,j,diff;
for(i=bend_no;i>=0&&len<length2;i--) //bend_no=0;
{
// printf("%d\n",bend[i].x);
// printf("%d\n",bend[i-1].x); //bend[i-1].x=25
if(bend2[i].x==bend2[i-1].x)
{
diff=bend2[i].y-bend2[i-1].y;
if(diff<0){
for(j=1;j<=(-diff);j++)
{
body2[len].x=bend2[i].x;
body2[len].y=bend2[i].y+j;
GotoXY2(body2[len].x,body2[len].y);
printf("#");
len++;
if(len==length2)
break;
}
}
else if(diff>0)
for(j=1;j<=diff;j++)
{
/*GotoXY2(bend[i].x,(bend[i].y-j));
printf("*");*/
body2[len].x=bend2[i].x;
body2[len].y=bend2[i].y-j;
GotoXY2(body2[len].x,body2[len].y);
printf("#");
len++;
if(len==length2)
break;
}
}
else if(bend2[i].y==bend2[i-1].y)
{
diff=bend2[i].x-bend2[i-1].x;
if(diff<0)
for(j=1;j<=(-diff)&&len<length2;j++)
{
/*GotoXY2((bend[i].x+j),bend[i].y);
printf("*");*/
body2[len].x=bend2[i].x+j;
body2[len].y=bend2[i].y;
GotoXY2(body2[len].x,body2[len].y);
printf("#");
len++;
if(len==length2)
break;
}
else if(diff>0)
for(j=1;j<=diff&&len<length2;j++)
{
/*GotoXY2((bend[i].x-j),bend[i].y);
printf("*");*/
body2[len].x=bend2[i].x-j;
body2[len].y=bend2[i].y;
GotoXY2(body2[len].x,body2[len].y);
printf("#");
len++;
if(len==length2)
break;
}
}
}
}
void Up2()
{
int i;
for(i=0;i<=(bend2[bend_no].y-head2.y)&&len<length2;i++)
{
GotoXY2(head2.x,head2.y+i);
{
if(len==0)
printf("^");
else
printf("#");
}
body2[len].x=head2.x;
body2[len].y=head2.y+i;
len++;
}
Bend2();
if(!kbhit())
head2.y--;
}
推荐答案
I assume, the game has only the function Move() and you have simple added the Move2(). You call this function in your main() but it will be not executed:
I assume, the game has only the function Move() and you have simple added the Move2(). You call this function in your main() but it will be not executed:
int main()
{
/* ... */
Move();
Move2(); //initializing initial bend coordinate
return 0;
}
Take a look at Move(), it calls itself recursively:
Take a look at Move(), it calls itself recursively:
void Move()
{
/* ... */
key = getch();
if (/* ... */)
{
/* ... */
Move();
}
else
{
printf("\a");
Move();
}
}
The Move2() will never get a chance!
The Move2() will never get a chance!
这篇关于C中的2个蛇程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文