while(1)
{
LoadKeyboardLayout("0x409", KLF_ACTIVATE | KLF_SETFORPROCESS); //切换为英文输入法
char s1[10], s2[10]; //保存成绩
settextcolor(GREEN);
sprintf(s1, "玩家1分数:%d", score); //将成绩格式化输出到字符串s1中
outtextxy(0, 0, s1);
sprintf(s2, "玩家2分数:%d", score2); //将成绩格式化输出到字符串s2中
outtextxy(0, 20, s2);
outtextxy(1050,200,_T("按空格键暂停游戏,按p退出游戏!"));
// 获取按键
t2 = GetTickCount();
if (kbhit())
{
c = getch();
t2 = GetTickCount();
t1=t2;
if(c==' ') //按空格键游戏暂停
{
outtextxy(520, 320, "游戏暂停,按回车键继续游戏");
getchar();
settextcolor(WHITE);
outtextxy(520, 320, "游戏暂停,按回车键继续游戏");
}
if(c=='p') //按p退出游戏
{
exit(0);
}
// 根据按键设置蛇的运动方向
switch(c)
{
case 'a': //向左,原方向为上下才可以变
if(snake->headDirection == 'w' || snake->headDirection == 's' )snake->headDirection = c ; break;
case 'd': //向右,原方向为上下才可以变
if(snake->headDirection == 'w' || snake->headDirection == 's' )snake->headDirection = c ; break;
case 'w': //向上,原来为左右才变化
if(snake->headDirection == 'a' || snake->headDirection == 'd' )snake->headDirection = c ; break;
case 's': //向下,原来为左右才变化
if(snake->headDirection == 'a' || snake->headDirection == 'd' )snake->headDirection = c ; break;
case 75: //向左,原方向为上下才可以变
if(snake2->headDirection == 'w' || snake2->headDirection == 's' )snake2->headDirection = 'a' ; break;
case 77: //向右,原方向为上下才可以变
if(snake2->headDirection == 'w' || snake2->headDirection == 's' )snake2->headDirection = 'd' ; break;
case 72: //向上,原来为左右才变化
if(snake2->headDirection == 'a' || snake2->headDirection == 'd' )snake2->headDirection = 'w' ; break;
case 80: //向下,原来为左右才变化
if(snake2->headDirection == 'a' || snake2->headDirection == 'd' )snake2->headDirection = 's' ; break;
}
}
judge=moveSnake(snake,1,snake2); //按照蛇的运动方向移动
if(judge==5)
{
settextcolor(GREEN);
outtextxy(455, 320, "玩家1撞到了自己的身体,恭喜玩家2获得了胜利,游戏结束!");
outtextxy(455, 340, "按回车键继续游戏,按Esc键返回菜单选择模式!");
break;
}
if(judge==6)
{
settextcolor(GREEN);
outtextxy(455, 320, "玩家2撞到了自己的身体,恭喜玩家1获得了胜利,游戏结束!");
outtextxy(455, 340, "按回车键继续游戏,按Esc键返回菜单选择模式!");
break;
}
if(judge==0)
{
settextcolor(GREEN);
outtextxy(455, 320, "玩家1撞墙了,恭喜玩家2获得了胜利,游戏结束!");
outtextxy(455, 340, "按回车键继续游戏,按Esc键返回菜单选择模式!");
score=0;
score2=0;
break;
}
else if(judge==2)
{
settextcolor(GREEN);
outtextxy(455, 320, "玩家1撞到了玩家2的身体,恭喜玩家2获得了胜利,游戏结束!");
outtextxy(455, 340, "按回车键继续游戏,按Esc键返回菜单选择模式!");
score=0;
score2=0;
break;
}
judge=moveSnake(snake2,2,snake);
if(judge==0)
{
settextcolor(GREEN);
outtextxy(455, 320, "玩家2撞墙了,恭喜玩家1获得了胜利,游戏结束!");
outtextxy(455, 340, "按回车键继续游戏,按Esc键返回菜单选择模式!");
score=0;
score2=0;
break;
}
else if(judge==2)
{
settextcolor(GREEN);
outtextxy(455, 320, "玩家2撞到了玩家1的身体,恭喜玩家1获得了胜利,游戏结束!");
outtextxy(455, 340, "按回车键继续游戏,按Esc键返回菜单选择模式!");
score=0;
score2=0;
break;
}
// 延时
Sleep(v-70);
if(t2 - t1 > 50)
{
v=50;
t1=t2;
}
}