if(kbhit())//输入方向
input=getch();
if(input=='w'||input=='s'||input=='a'||input=='d')
{
map[snake_x[snakelong-1]][snake_y[snakelong-1]]=0;
for(i=snakelong-1;i>0;i--)
{
snake_x[i]=snake_x[i-1];
snake_y[i]=snake_y[i-1];
map[snake_x[i]][snake_y[i]]=1;
}
if(input=='w')
snake_x[0]--;
else if(input=='s')
snake_x[0]++;
else if(input=='a')
snake_y[0]--;
else if(input=='d')
snake_y[0]++;
map[snake_x[0]][snake_y[0]]=4;
}
else if(input=='i'||input=='k'||input=='j'||input=='l')
{
map[snake_x1[snake1long-1]][snake_y1[snake1long-1]]=0;
for(j=snake1long-1;j>0;j--)
{
snake_x1[j]=snake_x1[j-1];
snake_y1[j]=snake_y1[j-1];
map[snake_x1[j]][snake_y1[j]]=1;
}
if(input=='i')
snake_x1[0]--;
else if(input=='k')
snake_x1[0]++;
else if(input=='j')
snake_y1[0]--;
else if(input=='l')
snake_y1[0]++;
map[snake_x1[0]][snake_y1[0]]=4;
}补充:我需要做到其中一条蛇运动时,另一条蛇的运动状态不受影响补充:#include<stdio.h> #include<stdlib.h>#include<time.h> #include<conio.h> #include<windows.h>#define heigh 58#define width 26#define snakenum 200#define framex 5#define framey 5#define wide 20#define high 20int snake_x[snakenum],snake_y[snakenum];int snake_x1[snakenum],snake_y1[snakenum];int food_x ,food_y ;int snakelong;int snake1long;int map[width][heigh];int score;int score1; char input; void gotoxy1(HANDLE hout,int x,int y){ //COORD是WindowsAPI中定义的一种结构,表示一个字符在控制台屏幕上的坐标 COORD pos; pos.X=x; pos.Y=y; //SetConsoleCursorPosition是API中定位光标位置的函数。 SetConsoleCursorPosition(hout,pos);}void cover (HANDLE hout){ gotoxy1(hout,framex+wide,framey); printf("snake——贪吃蛇游戏"); gotoxy1(hout,framex+wide,framey+5); printf("开始游戏前请关闭中文输入法"); gotoxy1(hout,framex+wide,framey+15); char a; a=getchar(); system("cls");} void HideCursor()//隐藏光标 {CONSOLE_CURSOR_INFO cursor_info={1,0};SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);} void startup_1() {snake_x[0]=width/2;snake_y[0]=heigh/3;snake_x[1]=width/2;snake_y[1]=heigh/3+1;food_x=width/3;food_y=heigh/3;snakelong=2; score=0; int i,j; map[snake_x[0]][snake_y[0]]=4; map[snake_x[1]][snake_y[1]]=1; map[food_x][food_y]=2; for(i=0;i<width;i++) for(j=0;j<heigh;j++) { if(i==0||j==0||i==width-1||j==heigh-1)map[i][j]=3; } //0是空格。 //1是蛇身。 //2是食物。//3是围墙。//4是蛇头。 HideCursor(); } void startup_2() {snake_x1[0]=width/4;snake_y1[0]=heigh/5;snake_x1[1]=width/4;snake_y1[1]=heigh/5+1;food_x=width/3;food_y=heigh/3;snake1long=2; score1=0; int i,j; map[snake_x1[0]][snake_y1[0]]=4; map[snake_x1[1]][snake_y1[1]]=1; map[food_x][food_y]=2; for(i=0;i<width;i++) for(j=0;j<heigh;j++) { if(i==0||j==0||i==width-1||j==heigh-1)map[i][j]=3; } //0是空格。 //1是蛇身。 //2是食物。//3是围墙。//4是蛇头。 HideCursor(); } void gotoxy(int x,int y) { HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X=x; pos.Y=y; SetConsoleCursorPosition(handle,pos); }void show() { gotoxy(0,0);int i,j;for(i=0;i<width;i++){for(j=0;j<heigh;j++){if(map[i][j]==0)printf(" ");else if(map[i][j]==1)printf("█");else if(map[i][j]==2)printf("█");else if(map[i][j]==3)printf("█");else if(map[i][j]==4)printf("█");} printf("\n"); } printf("蛇1得分%d\n",score); printf("蛇2得分%d\n",score1);} void updatewithoutinput_1() { int i; if(snake_x[0]==food_x&&snake_y[0]==food_y) {score++; srand((unsigned)time(NULL));//食物刷新 food_x=rand()%(width-3)+1; food_y=rand()%(heigh-3)+1; map[food_x][food_y]=2; snakelong++; } } void updatewithoutinput_2() { int i; if(snake_x1[0]==food_x&&snake_y1[0]==food_y) {score1++; srand((unsigned)time(NULL));//食物刷新 food_x=rand()%(width-3)+1; food_y=rand()%(heigh-3)+1; map[food_x][food_y]=2; snake1long++; } } void updatewithinput_1() { int i=1; if(kbhit())//输入方向 input=getch(); if(input=='w'||input=='s'||input=='a'||input=='d') { map[snake_x[snakelong-1]][snake_y[snakelong-1]]=0; for(i=snakelong-1;i>0;i--) { snake_x[i]=snake_x[i-1];snake_y[i]=snake_y[i-1]; map[snake_x[i]][snake_y[i]]=1; }} if(input=='w') snake_x[0]--; else if(input=='s') snake_x[0]++; else if(input=='a')snake_y[0]--; else if(input=='d') snake_y[0]++; map[snake_x[0]][snake_y[0]]=4; } void updatewithinput_2() { int i=1; if(kbhit())//输入方向 input=getch(); if(input=='i'||input=='k'||input=='j'||input=='l') { map[snake_x1[snake1long-1]][snake_y1[snake1long-1]]=0; for(i=snake1long-1;i>0;i--) { snake_x1[i]=snake_x1[i-1];snake_y1[i]=snake_y1[i-1]; map[snake_x1[i]][snake_y1[i]]=1; }} if(input=='i') snake_x1[0]--; else if(input=='k') snake_x1[0]++; else if(input=='j')snake_y1[0]--; else if(input=='l') snake_y1[0]++; map[snake_x1[0]][snake_y1[0]]=4; } int main(){ startup_1();startup_2();HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE); cover(hout);while(1){ show();updatewithoutinput_1();updatewithoutinput_2();updatewithinput_1();updatewithinput_2(); if(snake_x[0]==0||snake_y[0]==0||snake_x[0]==width-1||snake_y[0]==heigh-1){break;} else if(snake_x1[0]==0||snake_y1[0]==0||snake_x1[0]==width-1||snake_y1[0]==heigh-1) {break;} else if(score==5||score1==5) {break;} }Sleep(2000); //清屏 system("cls"); //显示结束界面 if(snake_x[0]==0||snake_y[0]==0||snake_x[0]==width-1||snake_y[0]==heigh-1) {printf("\n\n\n\n\n\t\t\t蛇2赢了\n\n\t\t\t"); printf("\n\n\n\n\n\t\t\t蛇1的得分:%d\n\n\t\t\t",score); printf("\n\n\n\n\n\t\t\t蛇2的得分:%d\n\n\t\t\t",score1); } else if(score==5) {printf("\n\n\n\n\n\t\t\t蛇1赢了\n\n\t\t\t蛇1得到了满分:5\n\n\n"); printf("\n\n\n\n\n\t\t\t蛇2的得分:%d\n\n\t\t\t",score1); } else if(snake_x1[0]==0||snake_y1[0]==0||snake_x1[0]==width-1||snake_y1[0]==heigh-1) {printf("\n\n\n\n\n\t\t\t蛇1赢了\n\n\t\t\t"); printf("\n\n\n\n\n\t\t\t蛇1的得分:%d\n\n\t\t\t",score); printf("\n\n\n\n\n\t\t\t蛇2的得分:%d\n\n\t\t\t",score1); } else if(score1==5) {printf("\n\n\n\n\n\t\t\t蛇2赢了\n\n\t\t\t蛇2得到了满分:5\n\n\n"); printf("\n\n\n\n\n\t\t\t蛇1的得分:%d\n\n\t\t\t",score); } else printf("\n\n\n\n\n\t\t\t出错了\n\n\t\t\t"); Sleep(3000);return 0;}
推荐回答
基本思路:
蛇每吃一个食物蛇身子就增加一格,用UP, DOWN, LEFT, RIGHT控制蛇头的运动,而蛇身子跟着蛇头走,每后一格蛇身子下一步走到上一格蛇身子的位置,以此类推。
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#define BEG_X 2
#define BEG_Y 1
#define WID 20
#define HEI 20
HANDLE hout;
typedef enum {UP, DOWN, LEFT, RIGHT} DIR;
typedef struct Snake_body
{
COORD pos;//蛇身的位置
struct Snake_body *next;//下一个蛇身
struct Snake_body *prev;//前一个蛇身
}SNAKE, *PSNAKE;
PSNAKE head = NULL;//蛇头
PSNAKE tail = NULL;//蛇尾
//画游戏边框的函数
void DrawBorder()
{
int i, j;
COORD pos = {BEG_X, BEG_Y};
for(i = 0; i < HEI; ++i)
{
SetConsoleCursorPosition(hout, pos);
for(j = 0; j < WID; ++j)
{
if(i == 0)//第一行
{
if(j == 0)
printf("┏");
else if(j == WID - 1)
printf("┓");
else
printf("━");
}
else if(i == HEI - 1)//最后一行
{
if(j == 0)
printf("┗");
else if(j == WID - 1)
printf("┛");
else
printf("━");
}
else if(j == 0 || j == WID - 1)//第一列或最后一列
printf("┃");
else
printf(" ");
}
++pos.Y;
}
}
//添加蛇身的函数
void AddBody(COORD pos)
{
PSNAKE pnew = (PSNAKE)calloc(1, sizeof(SNAKE));
pnew->pos = pos;
if(!head)
{
head = tail = pnew;
}
else
{
pnew->next = head;//新创建蛇身的next指向原先的蛇头
head->prev = pnew;//原先的蛇头的prev指向新创建的蛇身
head = pnew;//把新创建的蛇身作为新的蛇头
}
SetConsoleCursorPosition(hout, head->pos);
printf("◎");
}
//蛇身移动的函数
void MoveBody(DIR dir)
{
PSNAKE ptmp;
COORD pos = head->pos;
switch(dir)
{
case UP:
if(head->pos.Y > BEG_Y + 1)
--pos.Y;
else
return;
break;
case DOWN:
if(head->pos.Y < BEG_Y + HEI - 2)
++pos.Y;
else
return;
break;
case LEFT:
if(head->pos.X > BEG_X + 2)
pos.X -= 2;
else
return;
break;
case RIGHT:
if(head->pos.X < BEG_X + (WID - 2) * 2)
pos.X += 2;
else
return;
break;
}
AddBody(pos);//添加了一个新的蛇头
ptmp = tail;//保存当前的蛇尾
tail = tail->prev;
if(tail)
tail->next = NULL;
SetConsoleCursorPosition(hout, ptmp->pos);
printf(" ");
free(ptmp);
}
int main()
{
int ctrl;
DIR dir = RIGHT;//初始蛇的方向是向右的
COORD pos = {BEG_X + 2, BEG_Y + HEI / 2};
system("color 0E");
system("mode con cols=90 lines=30");
hout = GetStdHandle(STD_OUTPUT_HANDLE);
printf(" ------------贪吃蛇的移动------------");
DrawBorder();
//自定义几个蛇的身体
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
//控制蛇的移动
while(ctrl = getch())
{
switch(ctrl)
{
case 'w':
if(dir == DOWN)
continue;
dir = UP;
break;
case 's':
if(dir == UP)
continue;
dir = DOWN;
break;
case 'a':
if(dir == RIGHT)
continue;
dir = LEFT;
break;
case 'd':
if(dir == LEFT)
continue;
dir = RIGHT;
break;
case 'q':
return 0;
}
MoveBody(dir);
}
return 0;
}