我写了个贪吃蛇,有单人版,也有双人版
这是单人版:
#include
#include
#include
#include
#include
#include
int food[2];
int feng;
int dengji=3;
int speed=150;
struct snake
{
int x,y,life;
struct snake *next;
};
void welcome()
{
system("MODE con: COLS=79 LINES=24");
system("color 8F");
printf("欢迎来到贪吃蛇的世界\n");
printf(" By HB\n");
printf("↑ ↓ ← →\n");
printf("上 下 左 右\n");
printf("食物为:★\n");
printf("按空格暂停\n");
printf("按Z(大写)切换等级\n");
printf("等级从5级到1级是从高到低\n");
printf("注意:每次增加的分数=当前等级*10\n");
printf("按任意键开始游戏!\n");
getch();
system("cls");
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void makefood(struct snake*p)
{
int fx;
struct snake*p1=p;
srand(time(NULL));
fx=rand()%77+2;
if(fx%2==0)
{
fx--;
}
food[0]=fx;
food[1]=rand()%22+1;
while(p!=NULL)
{
if(p->x==food[0]&&p->y==food[1])
{
makefood(p1);
}
p=p->next;
}
}
void drawmap(struct snake* p)
{
system("cls");
printf("你的分数为:%d分 当前等级为:%d级",feng,dengji);
gotoxy(p->x,p->y);
printf("◇");
p=p->next;
while(p!=NULL)
{
gotoxy(p->x,p->y);
printf("※");
p=p->next;
}
gotoxy(food[0],food[1]);
printf("★");
}
struct snake *mvsnake(struct snake *p,int z)
{
struct snake *p2,*p3;
int x,y;
x=p->x;
y=p->y;
switch(z)
{
case 1:
{
x-=2;
break;
}
case 2:
{
x+=2;
break;
}
case 3:
{
y--;
break;
}
case 4:
{
y++;
break;
}
}
p2=(struct snake*)malloc(sizeof(struct snake));
p2->x=x;
p2->y=y;
p2->life=1;
p2->next=p;
if(p2->x>=79||p2->y>=24||p2->x<0||p2->y<1)
{
p2->life=0;
}
p3=p;
while(p3!=NULL)
{
if(p2->x==p3->x&&p2->y==p3->y)
{
p2->life=0;
}
p3=p3->next;
}
if(p2->x==food[0]&&p2->y==food[1])
{
feng+=dengji*10;
makefood(p2);
}
else
{
while(p->next->next!=NULL)
{
p=p->next;
}
p3=p->next;
p->next=NULL;
free(p3);
}
return p2;
}
int scan(int z)
{
char ch;
if(kbhit()){
ch=getch();
switch(ch)
{
case 75:if(z!=2)z=1;break;
case 77:if(z!=1)z=2;break;
case 72:if(z!=4)z=3;break;
case 80:if(z!=3)z=4;break;
case 32:z=5;break;
case 'Z':speed-=50;
dengji++;
if(speed==0){speed=250;dengji=1;}
break;
default :break;
}
if(ch==27)z=27;
fflush(stdin);
}
return z;
}
int main(int argc, char *argv[])
{
welcome();
loop:system("cls");
struct snake *snakenext;
struct snake *snk=(struct snake*)malloc(sizeof(struct snake));
snk->x=15;
snk->y=15;
snk->life=1;
snk->next=(struct snake*)malloc(sizeof(struct snake));
snk->next->x=14;
snk->next->y=snk->y;
snk->next->next=(struct snake*)malloc(sizeof(struct snake));
snk->next->next->x=13;
snk->next->next->y=snk->y;
snk->next->next->next=NULL;
int z=2;
makefood(snk);
while(1)
{
int temp_z=z;
z=scan(z);
if(z==27)exit(0);
if(z==5)
{
gotoxy(30,15);
printf("暂停");
getch();z=temp_z;
}
snk=mvsnake(snk,z);
if(snk->life==0)break;
drawmap(snk);
Sleep(speed);
}
system("cls");
gotoxy(25,11);
printf("GAME OVER,你的最终分数为:%d分\n",feng);
gotoxy(25,12);
printf("按空格重新开始,按其他键结束!\n");
while(snk!=NULL)
{
snakenext=snk->next;
free(snk);
snk=snakenext;
}
Sleep(500);
char cha;
cha=getch();
if(cha==32)
{
goto loop;
}
return 0;
}
这是双人版:
#include
#include
#include
#include
#include
#include
int food[2];
int feng1,feng2;
int speed=150;
int dengji=3;
int chi;
struct snake
{
int x,y,life;
struct snake *next;
};
void welcome()
{
system("MODE con: COLS=79 LINES=24");
system("color 8F");
printf("欢迎来到双人贪吃蛇游戏\n");
printf(" By HB\n");
printf("玩家1:□□◇\n");
printf("W S A D(大写)\n");
printf("上 下 左 右\n");
printf("玩家2:■■◆\n");
printf("↑ ↓ ← →\n");
printf("上 下 左 右\n");
printf("食物为:★\n");
printf("按空格暂停\n");
printf("按Z(大写)切换等级\n");
printf("等级从5级到1级是从高到低\n");
printf("注意:每次增加的分数=当前等级*10\n");
printf("按任意键开始游戏!\n");
getch();
system("cls");
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void makefood(struct snake *p,struct snake *p2)
{
int fx;
struct snake*p1=p;
struct snake*p3=p2;
srand(time(NULL));
fx=rand()%77+2;
if(fx%2==0)
{
fx--;
}
food[0]=fx;
food[1]=rand()%22+1;
while(p!=NULL&&p2!=NULL)
{
if((p->x==food[0]&&p->y==food[1])||(p2->x==food[0]&&p2->y==food[1]))
{
makefood(p1,p3);
}
p=p->next;
p2=p2->next;
}
}
void drawmap(struct snake *p,struct snake *p2)
{
system("cls");
printf("玩家1的分数为:%d分 当前等级为:%d级 玩家2的分数为:%d分",feng1,dengji,feng2);
gotoxy(p->x,p->y);
printf("◇");
p=p->next;
gotoxy(p2->x,p2->y);
printf("◆");
p2=p2->next;
while(p!=NULL)
{
gotoxy(p->x,p->y);
printf("□");
p=p->next;
}
while(p2!=NULL)
{
gotoxy(p2->x,p2->y);
printf("■");
p2=p2->next;
}
gotoxy(food[0],food[1]);
printf("★");
}
struct snake *mvsnake(struct snake *p,struct snake *p4,int z,int i)
{
struct snake *p2,*p3;
int x,y;
x=p->x;
y=p->y;
switch(z)
{
case 1:
{
x-=2;
break;
}
case 2:
{
x+=2;
break;
}
case 3:
{
y--;
break;
}
case 4:
{
y++;
break;
}
}
p2=(struct snake*)malloc(sizeof(struct snake));
p2->x=x;
p2->y=y;
p2->life=1;
p2->next=p;
if(p2->x>=79||p2->y>=24||p2->x<0||p2->y<1)
{
p2->life=0;
}
p3=p;
while(p3!=NULL)
{
if(p2->x==p3->x&&p2->y==p3->y)
{
p2->life=0;
}
p3=p3->next;
}
if(p2->x==food[0]&&p2->y==food[1])
{
if(i==1)
{
feng1+=dengji*10;
}
else if(i==2)
{
feng2+=dengji*10;
}
makefood(p2,p4);
}
else
{
while(p->next->next!=NULL)
{
p=p->next;
}
p3=p->next;
p->next=NULL;
free(p3);
}
return p2;
}
int scan(int z)
{
char ch;
if(kbhit()){
ch=getch();
switch(ch)
{
case 75:
case 'A':if(z!=2)z=1;break;
case 77:
case 'D':if(z!=1)z=2;break;
case 72:
case 'W':if(z!=4)z=3;break;
case 80:
case 'S':if(z!=3)z=4;break;
case 32:z=5;break;
case 'Z':speed-=50;
dengji++;
if(speed==0){speed=250;dengji=1;}
break;
default :break;
}
if(ch==27)z=27;
}
return z;
}
int main(int argc, char *argv[])
{
welcome();
loop:system("cls");
struct snake *snakenext;
struct snake *snk=(struct snake*)malloc(sizeof(struct snake));
snk->x=15;
snk->y=15;
snk->life=1;
snk->next=(struct snake*)malloc(sizeof(struct snake));
snk->next->x=14;
snk->next->y=snk->y;
snk->next->next=(struct snake*)malloc(sizeof(struct snake));
snk->next->next->x=13;
snk->next->next->y=snk->y;
snk->next->next->next=NULL;
struct snake *snake2next;
struct snake *snk2=(struct snake*)malloc(sizeof(struct snake));
snk2->x=17;
snk2->y=10;
snk2->life=1;
snk2->next=(struct snake*)malloc(sizeof(struct snake));
snk2->next->x=16;
snk2->next->y=snk2->y;
snk2->next->next=(struct snake*)malloc(sizeof(struct snake));
snk2->next->next->x=15;
snk2->next->next->y=snk2->y;
snk2->next->next->next=NULL;
int z=2;
int k=2;
makefood(snk,snk2);
while(1)
{
int temp_z=z;
z=scan(z);
k=scan(k);
if(z==27)exit(0);
if(z==5)
{
gotoxy(30,15);
printf("暂停");
getch();z=temp_z;
}
snk=mvsnake(snk,snk2,z,1);
snk2=mvsnake(snk2,snk,k,2);
if(snk->life==0||snk2->life==0)break;
drawmap(snk,snk2);
Sleep(speed);
}
system("cls");
gotoxy(15,11);
printf("GAME OVER,玩家1的分数为:%d分,玩家2的分数为:%d分\n",feng1,feng2);
gotoxy(33,12);
if(feng1>feng2)
{
printf("玩家1赢了!\n");
}
else if(feng2>feng1)
{
printf("玩家2赢了!\n");
}
else
{
printf("你们打成了平手!\n");
}
gotoxy(25,13);
printf("按空格重新开始,按其他键结束!\n");
while(snk!=NULL)
{
snakenext=snk->next;
free(snk);
snk=snakenext;
}
while(snk2!=NULL)
{
snake2next=snk2->next;
free(snk2);
snk2=snake2next;
}
Sleep(500);
char cha;
cha=getch();
if(cha==32)
{
goto loop;
}
return 0;
}
怎么发给你,游戏虽然小,只是几kb就几千行了
百度一下有的事呢