贪吃蛇双人对战源代码及详解
规则 :玩家一(左侧)通过按键W、S、A、D(大小写)四个键分别控制snake1上移、下移、左移和右移。玩家二(右侧)通过按键8、5、4、6 四个键分别控制snake2上移、下移、左移和右移。 每局游戏三分钟,死亡则直接失败,若时间结束,则分高者获胜。 每次游戏数据自动叠加保存并进行排名,若账号第一次使用,则创建新成绩。
源代码及详细解析:
#include
#include
#include
#include
#include
#include
#define SNAKESIZE 100
#define MAPWIDTH 118
#define MAPHEIGHT 29
#define M 200
void gotoxy ();
void MAP ();
void OPERATION ();
void createFood0 ();
void createFood ();
void createFood1 ();
bool check ();
bool check1 ();
void READ ();
void RANK ();
void OVER0 ();
void OVER ();
void fun ();
void fun1 ();
void START ();
void RULE ();
void MENU ();
void WRITE (char s[20],int score,char s1[20],int score1);
void PPX ();
struct game {
char name[20];
int win , lost , score;
double prob;
double jf;
}card[M+1];
struct { //保存食物坐标
int x;
int y;
}food;
struct {
int len;
int x[SNAKESIZE];
int y[SNAKESIZE];
}snake;
struct {
int len;
int x[SNAKESIZE];
int y[SNAKESIZE];
}snake1;
char key = '8' , key1 = 'w' , name[20] , name1[20];
//初始方向向上
int changeFlag = 0 , changeFlag1 = 0 , jf =0 , jf1 =0;
int speed=150 , sorce = 0 , sorce1 = 0 , sec=0 , min=3;
void gotoxy(int x, int y)//移动光标到指定位置
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void MAP()//打印边框和两条蛇的起始位置
{
for (int i = 0; i 0; i--)
{
snake.x[i] = snake.x[i - 1];
snake.y[i] = snake.y[i - 1];
}
for (int i = snake1.len - 1; i > 0; i--)
{
snake1.x[i] = snake1.x[i - 1];
snake1.y[i] = snake1.y[i - 1];
}
//蛇当前移动的方向不能和前一次的方向相反,比如蛇往左走的时候不能直接按右键往右走
//如果当前移动方向和前一次方向相反的话,把当前移动的方向改为前一次的方向
if (pre_key == '8' && key == '5')
key = '8';
if (pre_key == '5' && key == '8')
key = '5';
if (pre_key == '4' && key == '6')
key = '4';
if (pre_key == '6' && key == '4')
key = '6';
if (pre_key1 == 'w' && key1 == 's')
key1 = 'w';
if (pre_key1 == 's' && key1 == 'w')
key1 = 's';
if (pre_key1 == 'a' && key1 == 'd')
key1 = 'a';
if (pre_key1 == 'd' && key1 == 'a')
key1 = 'd';
//判断蛇头应该往哪个方向移动
switch (key)
{
case '4':
snake.x[0] -= 2;//往左
break;
case '6':
snake.x[0] += 2;//往右
break;
case '8':
snake.y[0]--;//往上
break;
case '5':
snake.y[0]++;//往下
break;
}
gotoxy(snake.x[0], snake.y[0]);
printf("■");
changeFlag = 0;
switch (key1)
{
case 'a':
case 'A':
snake1.x[0] -= 2;//往左
break;
case 'd':
case 'D':
snake1.x[0] += 2;//往右
break;
case 'w':
case 'W':
snake1.y[0]--;//往上
break;
case 's':
case 'S':
snake1.y[0]++;//往下
break;
}
gotoxy(snake1.x[0], snake1.y[0]);
printf("●");
changeFlag1 = 0;
gotoxy(MAPWIDTH,MAPHEIGHT);
return;
}
void createFood()
{
if (snake.x[0] == food.x && snake.y[0] == food.y)//蛇头碰到食物
{
//蛇头碰到食物即为要吃掉这个食物了,因此需要再次生成一个食物
createFood0();
createFood0();
snake.len++;//吃到食物,蛇身长度加1
sorce += 10;
speed -= 5;//随着吃的食物越来越多,速度会越来越快
changeFlag = 1;//很重要,因为吃到了食物,就不用再擦除蛇尾的那一节,以此来造成蛇身体增长的效果
}
return;
}
void createFood1()
{
if (snake1.x[0] == food.x && snake1.y[0] == food.y)//蛇头碰到食物
{
//蛇头碰到食物即为要吃掉这个食物了,因此需要再次生成一个食物
createFood0();
createFood0();
snake1.len++;//吃到食物,蛇身长度加1
sorce1 += 10;
speed -= 5;//随着吃的食物越来越多,速度会越来越快
changeFlag1 = 1;//很重要,因为吃到了食物,就不用再擦除蛇尾的那一节,以此来造成蛇身体增长的效果
}
return;
}
void createFood0()
{
while (1)
{
int a = 1 , b=1;
srand( (unsigned int)time(NULL) );
food.x = rand() % (MAPWIDTH - 4) + 2;
food.y = rand() % (MAPHEIGHT - 2) + 1;
//随机生成的食物不能在蛇的身体上
for (int i = 0; i < snake.len ; i++)
{
if (snake.x[i] == food.x && snake.y[i] == food.y)
{
a = 0;
break;
}
}
for (int i = 0; i < snake1.len; i++)
{
if (snake1.x[i] == food.x && snake1.y[i] == food.y)
{
b = 0;
break;
}
}
//随机生成的食物不能横坐标为奇数,也不能在蛇身,否则重新生成
if (a==1&&b==1&& food.x % 2 == 0)
break;
}
//绘制食物
gotoxy(food.x, food.y);
printf("★");
}
bool check()
{
//蛇头碰到上下边界,游戏结束
if (snake.y[0] == 0 || snake.y[0] == MAPHEIGHT)
return true;
//蛇头碰到左右边界,游戏结束
if (snake.x[0] == 0 || snake.x[0] == MAPWIDTH)
return true;
//蛇头碰到蛇身,游戏结束
for (int i = 1; i < snake.len; i++)
{
if (snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0])
return true;
}
for (int i = 0; i < snake1.len; i++)
{
if(snake1.x[i] == snake.x[0]&&snake1.y[i] == snake.y[0])
return true;
}
return false;
}
bool check1()
{
//蛇头碰到上下边界,游戏结束
if (snake1.y[0] == 0 || snake1.y[0] == MAPHEIGHT)
return true;
//蛇头碰到左右边界,游戏结束
if (snake1.x[0] == 0 || snake1.x[0] == MAPWIDTH)
return true;
//蛇头碰到蛇身,游戏结束
for (int i = 1; i < snake1.len; i++)
{
if (snake1.x[i] == snake1.x[0] && snake1.y[i] == snake1.y[0])
return true;
}
for (int i = 0; i < snake.len; i++)
{
if (snake.x[i] == snake1.x[0] && snake.y[i] == snake1.y[0])
return true;
}
return false;
}
void MENU ()//打印菜单界面
{
printf("\n\n\n\n\t\t\t\t ╔═══════════════════════════════════════════╗\n");
printf("\t\t\t\t ║ ║\n");
printf("\t\t\t\t ║ 欢迎来到贪吃蛇 ║\n");
printf("\t\t\t\t ║ ║\n");
printf("\t\t\t\t ║ ║\n");
printf("\t\t\t\t ║ ┏━━┓ ┏━━┓ ┏━━┓ ┏━━┓ ║\n");
printf("\t\t\t\t ║ 开始:┃ 1┃ 规则:┃ 2┃ 排行:┃ 3┃ 退出:┃ 4┃ ║\n");
printf("\t\t\t\t ║ ┗━━┛ ┗━━┛ ┗━━┛ ┗━━┛ ║\n");
printf("\t\t\t\t ║ ║\n");
printf("\t\t\t\t ║ ║\n");
printf("\t\t\t\t ╚═══════════════════════════════════════════╝\n");
switch(getch()){
case '1':
system("cls");
START();
break;
case '2':
system("cls");
fun(5);
RULE();
MENU();
break;
case '3':
system("cls");
fun(10);
RANK();
MENU();
break;
case '4':
exit(0);
break;
default:
system("cls");
printf("error");
MENU();
}
}
void RULE ()
{
system("cls");//清屏
printf("\t╔══════════════════════════════════════════════════════════════════════════════════════════════════╗\n");
printf("\t║本游戏玩家一(左侧)通过按键W、S、A、D(不区分大小写)四个键分别控制snake1上移、下移、左移和右移。║\n");
printf("\t║玩家二(右侧)通过按键8、5、4、6 四个键分别控制snake2上移、下移、左移和右移。 ║\n");
printf("\t║每局游戏三分钟,死亡则直接失败,若时间结束,则分高者获胜,获胜积分加十,失败积分加五。 ║\n");
printf("\t║每次游戏数据自动叠加保存并按照积分乘胜率进行排名,若账号第一次使用,则创建新账号保存新成绩。 ║\n");
printf("\t╚══════════════════════════════════════════════════════════════════════════════════════════════════╝\n");
system("pause");//暂停
system( "cls" );//清屏
}
void fun(int time)//进度条函数
{
int i = 0;
char bar[102] = { 0 };
gotoxy( 33 , 9 );
printf ( "数据载入中,请稍后\n" );
while (i sorce)
{
WRITE(name1,sorce1,name,sorce);
printf("\t\t\t\t║ %10s获胜 %10s失败 ║\n",name1,name);
}
else
printf("\t\t\t\t║ 恭喜你们平局了! ║\n");
}
printf("\t\t\t\t║ ║\n");
printf("\t\t\t\t║ ║\n");
printf("\t\t\t\t║ ┏━━┓ ┏━━┓ ┏━━┓ ║\n");
printf("\t\t\t\t║ 再来:┃ 1┃ 排名:┃ 2┃ 退出:┃ 3┃ ║\n");
printf("\t\t\t\t║ ┗━━┛ ┗━━┛ ┗━━┛ ║\n");
printf("\t\t\t\t╚═══════════════════════════════════════════╝\n");
switch(getch()){
case '1':
for(int i=0;i |