#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);
程序可以运行,但贪吃蛇界面就不能玩了
按方向键就自动退出。
游戏还没有边框能不能帮我补充下
程序总的帮我完整一下