永发信息网

c++贪吃蛇程序高手来帮我补充一下

答案:2  悬赏:10  手机版
解决时间 2021-07-22 00:04

#include <windows.h>
#include <iostream>
#include <time.h>
using namespace std;
//#define GAMESPEED 100


typedef struct Point
{
int x,y;
}Site;//蛇的节点坐标

typedef struct Snake
{
Site Point[200];
int Direction;
int length;
}snake;
typedef struct food
{
int x,y;
}myfood;//食物坐标

void gotoxy(int x,int y)
{
COORD myfood;
myfood.X = x;
myfood.Y = y;
SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE),myfood);
}//设置光标 定位

snake Initsnake()
{
int num1 = 5;

snake mysnake;

mysnake.Direction = 2;

mysnake.length = num1;

for (int i = num1 - 1;i >=0;i--)
{
mysnake.Point[i].x = i;
mysnake.Point[i].y = 1;
gotoxy(mysnake.Point[i].x,mysnake.Point[i].y);
cout<<"-";
}

return mysnake;
}
void move(snake *snake)//运动函数
{

Sleep(1000);
Point snake1;

if (snake->Direction == 1)//向左运动
{
snake1.x = snake->Point[snake->length-1].x+1;
snake1.y = snake->Point[snake->length-1].y;

gotoxy(snake1.x,snake1.y);
cout<<"-";

gotoxy(snake->Point[0].x,snake->Point[0].y);
cout<<" ";

snake->Point[snake->length] = snake1;
for (int i = 0;i < snake->length;i++)
{
snake->Point[i] = snake->Point[i + 1];
}

}

if (snake->Direction == 2)//向下运动
{

snake1.x = snake->Point[snake->length - 1].x;
snake1.y = snake->Point[snake->length-1].y + 1;

gotoxy(snake1.x,snake1.y);
cout<<"-";

gotoxy(snake->Point[0].x,snake->Point[0].y);
cout<<" ";

snake->Point[snake->length] = snake1;
for (int i = 0;i <= snake->length;i++)
{

snake->Point[i] = snake->Point[i + 1];
}

}
if (snake->Direction == 3)//向右运动
{
snake1.x = snake->Point[snake->length -1].x - 1;
snake1.y = snake->Point[snake->length -1].y;

gotoxy(snake1.x,snake1.y);
cout<<"-";

gotoxy(snake->Point[0].x,snake->Point[0].y);
cout<<" ";

snake->Point[snake->length] = snake1;

for (int i = 0;i <= snake->length;i++)
{

snake->Point[i] = snake->Point[i + 1];
}

}
if (snake->Direction == 4)//向上运动
{
snake1.x = snake->Point[snake->length - 1].x;
snake1.y = snake->Point[snake->length-1].y - 1;

gotoxy(snake1.x,snake1.y);
cout<<"-";

gotoxy(snake->Point[0].x,snake->Point[0].y);
cout<<" ";
snake->Point[snake->length] = snake1;
for (int i = 0;i <= snake->length;i++)
{

snake->Point[i] = snake->Point[i + 1];
}

}
}


bool DeathSnake(snake *Snake)
{

for (int i = 0;i < Snake->length;i++)
{
if (Snake->Point[Snake->length - 1].x == Snake->Point[i].x && Snake->Point[Snake->length - 1].y == Snake->Point[i].y )
{
return false;
}
}
return true;
}

Point food()//产生随机坐标的食物
{
Point F_Point;
srand((unsigned)time(NULL));

F_Point.x = rand()%80;
F_Point.y =rand()%25;

gotoxy(F_Point.x,F_Point.y);
cout<<"-";

return F_Point;
}

int Key_Input()
{
while(1)
{

if(GetAsyncKeyState(VK_LEFT))
{
return 3;
}
if (GetAsyncKeyState(VK_DOWN))
{
return 2;
}
if (GetAsyncKeyState((VK_RIGHT)))
{
return 1;
}
if (GetAsyncKeyState(VK_UP))
{
return 4;
}

};
return 0;
}
snake AddSnake(snake *Snake,Point food)
{


if (Snake->Point[Snake->length - 1].x == food.x && Snake->Point[Snake->length -1].y == food.y)
{
Snake->Point[Snake->length].x = food.x;
Snake->Point[Snake->length].y = food.y;

gotoxy(Snake->Point[Snake->length].x,Snake->Point[Snake->length].y);

cout<<"-";

++Snake->length;
}

return *Snake;
}
int main()
{

snake mysnake;
mysnake = Initsnake();
Point myfood1 = food();
do
{
move(&mysnake);
int dir = Key_Input();
mysnake.Direction = dir;
AddSnake(&mysnake,myfood1);

程序可以运行,但贪吃蛇界面就不能玩了

按方向键就自动退出。

游戏还没有边框能不能帮我补充下

程序总的帮我完整一下

最佳答案

mysnake.Direction = dir;
这个有逻辑错误,还得考虑蛇当前的运动方向,


如果蛇正在向上运行,此时按down键是没用的。

全部回答

修改了一下,应该有些效果了,如下:

#include <windows.h> #include <iostream> #include <time.h>

using namespace std; //#define GAMESPEED 100

typedef struct Point { int x,y; }Site;//蛇的节点坐标

typedef struct Snake { Site Point[200]; int Direction; int length; }snake;

typedef struct food { int x,y; }myfood;//食物坐标

Point myfood1;

void gotoxy(int x,int y) { COORD myfood; myfood.X = x; myfood.Y = y; SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), myfood); }//设置光标 定位

snake Initsnake() { int num1 = 5;

snake mysnake;

mysnake.Direction = 2;

mysnake.length = num1;

for (int i = num1 - 1;i >=0;i--) { mysnake.Point[i].x = i; mysnake.Point[i].y = 1; gotoxy(mysnake.Point[i].x,mysnake.Point[i].y); cout<<"-"; }

return mysnake; }

void move(snake *snake)//运动函数 { Sleep(100); Point snake1;

if (snake->Direction == 1)//向左运动 { snake1.x = snake->Point[snake->length-1].x+1; snake1.y = snake->Point[snake->length-1].y;

gotoxy(snake1.x,snake1.y); cout<<"-";

gotoxy(snake->Point[0].x,snake->Point[0].y); cout<<" ";

snake->Point[snake->length] = snake1; for (int i = 0;i < snake->length;i++) { snake->Point[i] = snake->Point[i + 1]; } } else if (snake->Direction == 2)//向下运动 { snake1.x = snake->Point[snake->length - 1].x; snake1.y = snake->Point[snake->length-1].y + 1;

gotoxy(snake1.x,snake1.y); cout<<"-";

gotoxy(snake->Point[0].x,snake->Point[0].y); cout<<" ";

snake->Point[snake->length] = snake1; for (int i = 0;i <= snake->length;i++) { snake->Point[i] = snake->Point[i + 1]; } } else if (snake->Direction == 3)//向右运动 { snake1.x = snake->Point[snake->length -1].x - 1; snake1.y = snake->Point[snake->length -1].y;

gotoxy(snake1.x,snake1.y); cout<<"-";

gotoxy(snake->Point[0].x,snake->Point[0].y); cout<<" ";

snake->Point[snake->length] = snake1;

for (int i = 0;i <= snake->length;i++) { snake->Point[i] = snake->Point[i + 1]; } } else if (snake->Direction == 4)//向上运动 { snake1.x = snake->Point[snake->length - 1].x; snake1.y = snake->Point[snake->length-1].y - 1;

gotoxy(snake1.x,snake1.y); cout<<"-";

gotoxy(snake->Point[0].x,snake->Point[0].y); cout<<" "; snake->Point[snake->length] = snake1; for (int i = 0;i <= snake->length;i++) { snake->Point[i] = snake->Point[i + 1]; } } }

bool DeathSnake(snake *Snake) { for (int i = 0;i < Snake->length;i++) { if (Snake->Point[Snake->length - 1].x == Snake->Point[i].x && Snake->Point[Snake->length - 1].y == Snake->Point[i].y ) { return false; } }

return true; }

Point NewFood()//产生随机坐标的食物 { Point F_Point; srand((unsigned)time(NULL));

F_Point.x = rand()%80; F_Point.y =rand()%25;

gotoxy(F_Point.x,F_Point.y); cout<<"-";

return F_Point; }

int Key_Input() { while(1) { if(GetAsyncKeyState(VK_LEFT)) { return 3; }

if (GetAsyncKeyState(VK_DOWN)) { return 2; }

if (GetAsyncKeyState((VK_RIGHT))) { return 1; }

if (GetAsyncKeyState(VK_UP)) { return 4; } };

return 0; }

snake AddSnake(snake *Snake, Point food) { if (Snake->Point[Snake->length - 1].x == food.x && Snake->Point[Snake->length -1].y == food.y) { Snake->Point[Snake->length].x = food.x; Snake->Point[Snake->length].y = food.y;

gotoxy(Snake->Point[Snake->length].x,Snake->Point[Snake->length].y);

cout<<"-";

++Snake->length; myfood1 = NewFood(); }

return *Snake; }

int main(void) { system("cls");

snake mysnake; mysnake = Initsnake(); //Point myfood1 = food(); myfood1 = NewFood();

do { move(&mysnake); int dir = Key_Input(); mysnake.Direction = dir; AddSnake(&mysnake, myfood1); }while(1);

return 0; }

我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
崇阳县咸宁崇阳县港口乡油榨村村民委员会我想
今年河北唐山有女兵吗
为什么QQ宠物的饥饿,清洁,心情值都是零而健
向阳区佳木斯盛达烧烤大全地址在什么地方,想
为什么日本人养猫的非常多?
长恨歌王安忆经典名句,长恨歌里的经典名句是
谁能给我《放生》的繁体歌词
利川市恩施利川市公安局交通警察大队谋道中队
农历10月25号是什么星座?
新概念第二册第11课课后练习难点部分练习全部
鼎城区常德益生堂美容养生汗蒸馆怎么去啊,谁
cq61为什么分不了区????
求救啊,AMD双核变单核啦
通达信和同花顺怎么看出一支股票是否被股权质
咸安区咸宁廖记家常菜(香城桂苑西南)地址在什
推荐资讯
求《美国往事》的主题曲下载地址
高考作文开头名人名言,高考作文常用的名言警
常言道实践出真知谚语,说明多实践才能出真知
开封火车站附近有没有单间出租
北方这几天的天气情况
QQ宠物改版后,没有了玩具,那用什么方法提高
皮球有那些玩法?
金花村到龙泉园区的公交线路(急……)
化学性质及用途
现在都11点45了咋没维护完 ?
台式机可以装无线网卡吗?
DNF55级狂战,10级崩山,落地那一下,没有范围攻
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?