想知道在c++语言写的简单的贪吃蛇游戏中,怎么设置空格键暂停开始。利用# include
主函数
int main()
{
char map[ROW_MAX][LINE_MAX];
int snake[ROW_MAX][LINE_MAX];
srand(time(NULL));
/**************初始画面**************/
memset(map, 0, sizeof(map));
memset(snake, 0, sizeof(snake));
system("cls");
CreateMap(map);
// gotoxy(1, 1);
CreateSnake(snake);
CreateFood(map, snake);
TraverseMap(map, snake);
/**************初始画面**************/
/*********运动*****************/
run(map, snake);
/******************************/
Result();
return 0;
}
这个是主函数。
整个程序是按照二维数组构成的。其中地图是二维数组构成围墙。蛇的移动是利用上下左右或者wasd空着蛇的走位. 以及当蛇每走一步就利用 system(“cls”)清屏, 重新打印, 模拟蛇的移动.我想利用system("pause")来实现暂停开始的功能。
下面这一段是关于设置的代码,如果我要加入暂停开始 应该加在哪儿。怎么使用。谢谢各位了
设置
由于不同的处理器, 蛇的移动速度也不一样;
以及可以修改地图的大小;
int ROW = ROW_MAX;
int LINE = LINE_MAX;
int times = 1;//均为全局变量
//若增加此函数, 要达到修改的目的, 须将之前所有函数体中的 _MAX 删去(函数名中的_MAX不必删除)
//或者等后面的源代码
void SetUp(void){
char ch;
int row;
int line;
int speed;
system("cls");
printf("\tSET UP\n");
printf("1. speed\n");
printf("2. area\n");
printf("0. return\n");
ch = getch();
while(1){
switch (ch){
case '1':
printf("The less the figure the faster speed level(1~8)\n");
printf("Speed level: ");
scanf("%d", &speed);
times = speed>8? 8: speed<1? 1: speed;
printf("The speed leve: %ld", times);
break;
case '2':
printf("Update length and width(the max size is 80 x 80 and input 0 default)\n");
printf("length: ");
scanf("%d", &row);
printf("width: ");
scanf("%d", &line);
if(line != 0 && row != 0){
ROW = row+2;
LINE = line+2;
Head_x = ROW/2;
Head_y = LINE/2;
}
printf("After updating length: %d\twidth: %d\n", row, line);
break;
default:
return;
}
for(long i = 0; i < 400000000; ++i);//延时显示结果
//fflush(stdin);
system("cls");
printf("\tSET UP\n");
printf("1. speed\n");
printf("2. area\n");
printf("0. return\n");
ch = getch();
}
}
延时函数
void dely (void){
for(long i = 0; i <= pow(16, times); ++i)
;
}
将此代码插入到此位置即可.
//处理按上下左右;
if(direct == 'w'){
if(sh == 'P')
continue;
}
else if (direct == 's'){
if(sh == 'H')
continue;
}
else if (direct == 'a'){
if(sh == 'M')
continue;
}
else{
if(sh == 'K')
continue;
}
//处理按wasd;
while(1){
if(JudgeWall()){
/**********判断键盘是否敲击***********/
/************************************/
/**************蛇的运动******************/
/****************************************/
}
else
return;
dely();
}