双人贪吃蛇,第二个问什么不停使唤?

键盘小王子 2020-08-05 07:40:18
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>

char qipan[20][80];
int snake = 1;

int change(int *head, int *tail, int *score, char *direction, int zuobiao[2][80])
{
int x, y;
switch(*direction)
{
case 72:
x = zuobiao[0][*head]-1;
y = zuobiao[1][*head];
break;
case 87:
x = zuobiao[0][*head]-1;
y = zuobiao[1][*head];
break;
case 80:
x = zuobiao[0][*head]+1;
y = zuobiao[1][*head];
break;
case 83:
x = zuobiao[0][*head]+1;
y = zuobiao[1][*head];
break;
case 74:
x = zuobiao[0][*head];
y = zuobiao[1][*head]-1;
break;
case 75:
x = zuobiao[0][*head];
y = zuobiao[1][*head]-1;
break;
case 76:
x = zuobiao[0][*head];
y = zuobiao[1][*head]+1;
break;
default:
x = zuobiao[0][*head];
y = zuobiao[1][*head]+1;
}
if(qipan[x][y] == '_' || qipan[x][y] == '|' || qipan[x][y] == '*')
return 1;
if(qipan[x][y] == ' ')
{
qipan[zuobiao[0][*tail]][zuobiao[1][*tail]]=' ';
*tail=(*tail+1)%80;
qipan[zuobiao[0][*head]][zuobiao[1][*head]]='*';
*head=(*head+1)%80;
zuobiao[0][*head]=x;
zuobiao[1][*head]=y;
qipan[x][y]='#';
return 0;
}
if(qipan[x][y] == '@')
{
qipan[zuobiao[0][*head]][zuobiao[1][*head]]='*';
*head=(*head+1)%80;
zuobiao[0][*head]=x;
zuobiao[1][*head]=y;
qipan[x][y]='#';
*score += 1;
return 0;
}
}
void gotoxy(int x,int y)//位置函数
{
COORD pos;

pos.X=x;

pos.Y=y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a)//颜色函数
{

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);

}
void show_start()
{

gotoxy(20,0);
color(1);
int u;
for(u = 0; u < 18; u ++)
printf("* ");
gotoxy(20, 12);
for(u = 0; u < 18; u ++)
printf("* ");
gotoxy(28,2);
color(13);
printf("S N A K E G A M E");
gotoxy(31, 5);
color(14);
printf("#");
gotoxy(34, 5);
color(11);
printf("One snake");
gotoxy(34, 7);
printf("Two snakes");
gotoxy(28,9);
printf("The speed you want: ");

gotoxy(31, 5);
char temp = 'a';
color(14);
while(1)
{
while(!kbhit());
temp = getch();
if(temp == 72 || temp == 80)
{
gotoxy(31,5+2*(snake-1));
printf(" ");
snake = snake % 2 + 1;
gotoxy(31,5+2*(snake-1));
printf("#");
gotoxy(31,5+2*(snake-1));
}
if(temp == 13)
break;
}

gotoxy(27,10);
color(10);
printf("GOOD LUCK TO YOU !");
}






int main()
{
srand(time(0));

int head1 = 3, tail1 = 0, score1 = 0;
int head2 = 3, tail2 = 0, score2 = 0;
int zuobiao1[2][80];
int zuobiao2[2][80];

/*棋盘 20×80 初始化*/
for(int i = 0; i < 20; i++)
{
for(int j = 0; j < 80; j++)
{
qipan[i][j] = ' ';
}
}
for(int i = 0; i < 20; i++)
{
qipan[i][0] = qipan[i][79] = '|';//第一列、最后一列是墙20*1
}
for(int i = 0; i < 80; i++)
{
qipan[0][i] = qipan[19][i] = '_';//第一行、最后一行是墙1*80
}

/*蛇的坐标 2×8 初始化, 将蛇放到棋盘上*/
int x1 = 1, x2 = 18, y, temp = 0;

for(int m = tail1; m < head1; m++)
{
zuobiao1[0][m] = x1;//初始行号
y = zuobiao1[1][m] = ++temp;//初始列号
qipan[x1][y] = '*';
}
zuobiao1[0][head1] = x1;//初始行号
y = zuobiao1[1][head1] = ++temp;//初始列号
qipan[x1][y] = '#';//蛇头

show_start();

if(snake == 2)
{
temp = 0;
for(int m = tail2; m < head2; m++)
{
zuobiao2[0][m] = x2;//初始行号
y = zuobiao2[1][m] = ++temp;//初始列号
qipan[x2][y] = '*';
}
zuobiao2[0][head2] = x2;//初始行号
y = zuobiao2[1][head2] = ++temp;//初始列号
qipan[x2][y] = '#';//蛇头
}

clock_t start;
int timeover;
char direction1 = 77;//72 88 75 77
char direction2 = 68;//87 83 65 68
char new_direction;
char new_direction1 ;
char new_direction2 ;

int gamespeed;
gotoxy(48, 9);
color(14);
scanf("%d", &gamespeed);
int rand_i = rand()%18 + 1;
int rand_j = rand()%78 + 1;
qipan[rand_i][rand_j] = '@';

system("cls");
gotoxy(10, 1);
color(10);
printf("Your present score: ");
gotoxy(30, 1);
color(11);
printf("%d", score1);
gotoxy(0,4);
for(int i = 0; i < 20; i++)//打印出棋盘
for(int j = 0; j < 80; j++)
{
if(qipan[i][j] == '*' || qipan[i][j] == '#')
color(13);
else if(qipan[i][j] == '@')
color(12);
else
color(15);
printf("%c", qipan[i][j]);
}
while(1)//87 83 65 68
{
for(int i = 0; i < 20; i++)//打印出棋盘
for(int j = 0; j < 80; j++)
{
if(qipan[i][j] == '*' || qipan[i][j] == '#')
{
gotoxy(j, i+4);
color(13);
}
if(qipan[i][j] == '@')
{
gotoxy(j, i+4);
color(12);
}
if(qipan[i][j] == '_' || qipan[i][j] == '|')
{
gotoxy(j, i+4);
color(15);
}
printf("%c", qipan[i][j]);
}
for(int q = 0; q < 3; q++)
{
gotoxy(rand()%78 + 1, rand()%18 + 5 );
color(14);
printf("+");
}
start = clock();
timeover = 1;
while(!kbhit() && (timeover = clock() - start/CLOCKS_PER_SEC <= gamespeed));
if(timeover)//有按键
{
if(snake == 1)
getch();
new_direction = getch();
if(snake == 2)
{
if(new_direction == 87 || new_direction ==83 ||new_direction == 65||new_direction == 68)
{
new_direction2 = new_direction;
new_direction1 = direction1;
if(new_direction2 + direction2 == 170 || new_direction2 + direction2 == 133)
{
change(&head1, &tail1, &score1, &direction1, zuobiao1);
change(&head2, &tail2, &score2, &direction2, zuobiao2);
continue;
}
}
if(new_direction == 72 || new_direction ==80 ||new_direction == 75||new_direction == 77)
{
new_direction1 = new_direction;
new_direction2 = direction2;
if(new_direction1 + direction1 == 152)
{
change(&head1, &tail1, &score1, &direction1, zuobiao1);
change(&head2, &tail2, &score2, &direction2, zuobiao2);
continue;
}
}
}
if(snake == 1 && (new_direction == 72 || new_direction ==80 ||new_direction == 75||new_direction == 77))
{
new_direction1 = new_direction;
if(new_direction1 + direction1 == 152)
{
change(&head1, &tail1, &score1, &direction1, zuobiao1);
continue;
}
}
}
else
{
if(snake == 2)
{
new_direction2 = direction2;
new_direction1 = direction1;
}
else
{
new_direction1 = direction1;
}
}
if(snake == 1)
{
direction1 = new_direction1;
if(change(&head1, &tail1, &score1, &direction1, zuobiao1) == 1)
{
system("cls");
gotoxy(30,10);
color(7);
printf("G A M E O V E R !");
gotoxy(0,20);
color(1);
return 0;
}
}
if(snake == 2)
{
direction1 = new_direction1;
if(change(&head1, &tail1, &score1, &direction1, zuobiao1) == 1)
{
system("cls");
gotoxy(30,8);
color(7);
printf("G A M E O V E R !");
gotoxy(30,10);
printf("Snake Two is the Hero!");
gotoxy(0,20);
color(1);
return 0;
}

direction2 = new_direction2;
if(change(&head2, &tail2, &score2, &direction2, zuobiao2) == 1)
{
system("cls");
gotoxy(30,8);
color(7);
printf("G A M E O V E R !");
gotoxy(30,10);
printf("Snake One is the Hero!");
gotoxy(0,20);
color(1);
return 0;
}

}

int randnew = 0;
for(int i = 0; i < 20; i++)//打印出棋盘
for(int j = 0; j < 80; j++)
if(qipan[i][j] == '@')
{
randnew ++;
}
if(randnew == 0)
{
rand_i = rand()%18 + 1;
rand_j = rand()%78 + 1;
qipan[rand_i][rand_j] = '@';
}
if(snake == 2 && randnew == 1)
{
rand_i = rand()%18 + 1;
rand_j = rand()%78 + 1;
qipan[rand_i][rand_j] = '@';
}

gotoxy(30, 1);
printf("%d", score1);
if(snake == 2)
{
gotoxy(30,2);
printf("%d", score2);
}
}
}
各位大佬,请问第二个贪吃蛇为什么不听键盘的控制?
...全文
29 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
源代码大师 2021-05-07
  • 打赏
  • 举报
回复
希望对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10581430.html 希望对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10768339.html

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧