pp74875 2016-03-03 08:11 采纳率: 0%
浏览 1593

菜鸟求大神帮忙解答贪食蛇问题

以下的这段贪食蛇代码是我在百度上看到的,我不理解int tcsZuobiao[2][100]; //蛇的坐标数组for (i = 0; i tcsZuobiao[0][i] = 1;
tcsZuobiao[1][i] = i + 1;
}这里为什么蛇的坐标是[2][100]呢,还有循环里面的也不懂为什么要这样弄
#include
#include
#include
#include
using namespace std;
// 刷新当前屏幕
inline void Refresh(char q[][22], int grade, int gamespeed){
system("cls"); // 清屏
int i, j;
cout << endl;
for (i = 0; i cout for (j = 0; j cout if (i == 0) cout if (i == 4) cout if (i == 6) cout cout }
}
int main(){
char tcsQipan[22][22]; // 贪吃蛇棋盘是一个二维数组(如22*22,包括墙壁)
int i, j;
for (i = 1; i for (j = 1; j tcsQipan[i][j] = ' '; // 初始化贪吃蛇棋盘中间空白部分
for (i = 0; i tcsQipan[0][i] = tcsQipan[21][i] = '-'; //初始化贪吃蛇棋盘上下墙壁
for (i = 1; i tcsQipan[i][0] = tcsQipan[i][21] = '|'; //初始化贪吃蛇棋盘左右墙壁
int tcsZuobiao[2][100]; //蛇的坐标数组
for (i = 0; i tcsZuobiao[0][i] = 1;
tcsZuobiao[1][i] = i + 1;
}
int head = 3, tail = 0;
for (i = 1; i tcsQipan[1][i] = '*'; //蛇身
tcsQipan[1][4] = '#'; //蛇头
int x1, y1; // 随机出米
srand(time(0));
do{
x1 = rand() % 20 + 1;
y1 = rand() % 20 + 1;
} while (tcsQipan[x1][y1] != ' ');
tcsQipan[x1][y1] = '*';
cout long start;
int grade = 1, length = 4;
int gamespeed = 500; //前进时间间隔
for (i = 3; i >= 0; i--){
start = clock();
while (clock() - start <= 1000);
system("cls");
if (i>0)
cout << "\n\n\t\t进入倒计时:" << i << endl;
else
Refresh(tcsQipan, grade, gamespeed);
}
int timeover;
char direction = 77; // 初始情况下,向右运动
int x, y;
while (1){
timeover = 1;
start = clock();
while ((timeover = (clock() - start <= gamespeed)) && !kbhit());
//如果有键按下或时间超过自动前进时间间隔则终止循环
if (timeover){
getch(); direction = getch();
}
switch (direction){
case 72: x = tcsZuobiao[0][head] - 1; y = tcsZuobiao[1][head]; break; // 向上
case 80: x = tcsZuobiao[0][head] + 1; y = tcsZuobiao[1][head]; break; // 向下
case 75: x = tcsZuobiao[0][head]; y = tcsZuobiao[1][head] - 1; break; // 向左
case 77: x = tcsZuobiao[0][head]; y = tcsZuobiao[1][head] + 1; // 向右
}
if (!(direction == 72 || direction == 80 || direction == 75 || direction == 77)){ // 按键非方向键
cout << "\tGame over!" << endl; return 0;
}
if (x == 0 || x == 21 || y == 0 || y == 21){ // 碰到墙壁
cout << "\tGame over!" << endl; return 0;
}
if (tcsQipan[x][y] != ' '&&!(x == x1&&y == y1)){ // 蛇头碰到蛇身
cout << "\tGame over!" << endl; return 0;
}
if (x == x1 && y == y1){ // 吃米,长度加1
length++;
if (length >= 8){
length -= 8;
grade++;
if (gamespeed >= 200)
gamespeed = 550 - grade * 50; // 改变自动前进时间间隔
}
tcsQipan[x][y] = '#';
tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]] = '*';
head = (head + 1) % 100;
tcsZuobiao[0][head] = x;
tcsZuobiao[1][head] = y;
do
{
x1 = rand() % 20 + 1;
y1 = rand() % 20 + 1;
} while (tcsQipan[x1][y1] != ' ');
tcsQipan[x1][y1] = '*';
Refresh(tcsQipan, grade, gamespeed);
}
else{ // 不吃米
tcsQipan[tcsZuobiao[0][tail]][tcsZuobiao[1][tail]] = ' ';
tail = (tail + 1) % 100;
tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]] = '*';
head = (head + 1) % 100;
tcsZuobiao[0][head] = x;
tcsZuobiao[1][head] = y;
tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]] = '#';
Refresh(tcsQipan, grade, gamespeed);
}
}
return 0;
}

  • 写回答

1条回答

  • devmiao 2016-03-06 00:59
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘