控制台贪吃蛇小游戏

控制台贪吃蛇小游戏

  由于比较懒,所以不怎么写,觉得这样不应该。我应该对自己学的做出整理,不管是高端低端,写出来是自己的。

 // 贪吃蛇.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include<iostream>
#include"Windows.h"
#include<ctime> nData g_Snake[];
nData g_Food; using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int nKeyState = ;
int nSnakeMove = ;
int nSnakeLeng = ;
bool bSnake = false; //初始化食物
srand((unsigned)time(NULL));
g_Food.X = rand() % + ;
g_Food.Y = rand() % + ; //初始化蛇头 g_Snake[].X = ;
g_Snake[].Y = ; while (true)
{
system("CLS");
//开始菜单键盘输入
if (nKeyState < )
{
if (KEY_DOWN(VK_DOWN))
{
nKeyState++;
if (nKeyState > )
{
nKeyState = ;
}
}
else if (KEY_DOWN(VK_UP))
{
nKeyState--;
if (nKeyState < )
{
nKeyState = ;
}
}
else if (KEY_DOWN(VK_RETURN))
{
if (nKeyState == e_Menu_StartGame)
{
nKeyState = e_GO_StartGame;
}
}
}
//蛇动作
else if (nKeyState == e_GO_StartGame)
{
//备份蛇头
g_Snake[].XOD = g_Snake[].X;
g_Snake[].YOD = g_Snake[].Y; if (KEY_DOWN(VK_UP))
{
if (nSnakeMove == e_Move_Down)
{
nSnakeMove = e_Move_Down;
}
else
{
nSnakeMove = e_Move_Up;
}
}
else if (KEY_DOWN(VK_DOWN))
{
if (nSnakeMove == e_Move_Up)
{
nSnakeMove = e_Move_Up;
}
else
{
nSnakeMove = e_Move_Down;
}
}
else if (KEY_DOWN(VK_LEFT))
{ if (nSnakeMove == e_Move_Right)
{
nSnakeMove = e_Move_Right;
}
else
{
nSnakeMove = e_Move_Left;
}
}
else if (KEY_DOWN(VK_RIGHT))
{
if (nSnakeMove == e_Move_Left)
{
nSnakeMove = e_Move_Left;
}
else
{
nSnakeMove = e_Move_Right;
}
}
}
switch (nSnakeMove)
{
case e_Move_Up: g_Snake[].X--; break;
case e_Move_Down: g_Snake[].X++; break;
case e_Move_Left: g_Snake[].Y--; break;
case e_Move_Right: g_Snake[].Y++; break;
} //蛇撞墙
if (nGameFrame[g_Snake[].X][g_Snake[].Y] == )
{
nKeyState = e_GO_GameOver;
}
//过关
if (nSnakeLeng == )
{
nKeyState = e_Go_WinGame;
} //蛇吃到自己`
for (int i = ; i <= nSnakeLeng; i++)
{
if (g_Snake[i].X == g_Snake[].X&&g_Snake[i].Y == g_Snake[].Y)
{
nKeyState = e_GO_GameOver;
}
} //遍历蛇身位置
for (int i = ; i < nSnakeLeng; i++)
{
g_Snake[i].XOD = g_Snake[i].X;
g_Snake[i].YOD = g_Snake[i].Y;
g_Snake[i].X = g_Snake[i - ].XOD;
g_Snake[i].Y = g_Snake[i - ].YOD;
} //吃到食物
if (g_Food.X==g_Snake[].X&&g_Food.Y==g_Snake[].Y)
{
srand((unsigned)time(NULL));
g_Food.X = rand() % + ;
g_Food.Y = rand() % + ;
nSnakeLeng++;
} //渲染.
if (nKeyState < )
{
cout << "■■■■■■■■■■■■■■■■■" << endl;
cout << "■■■■■■■■■■■■■■■■■" << endl;
cout << "■■■■ ■■■■" << endl;
if (nKeyState == e_Menu_StartGame)
{
cout << "■■■■ >-开始游戏 ■■■■" << endl;
cout << "■■■■ 游戏设置 ■■■■" << endl;
cout << "■■■■ 游戏结束 ■■■■" << endl;
}
else if (nKeyState == e_Menu_GameSet)
{
cout << "■■■■ 开始游戏 ■■■■" << endl;
cout << "■■■■ >-游戏设置 ■■■■" << endl;
cout << "■■■■ 游戏结束 ■■■■" << endl;
}
else if (nKeyState == e_Menu_EndGame)
{
cout << "■■■■ 开始游戏 ■■■■" << endl;
cout << "■■■■ 游戏设置 ■■■■" << endl;
cout << "■■■■ >-游戏结束 ■■■■" << endl;
}
cout << "■■■■ ■■■■" << endl;
cout << "■■■■■■■■■■■■■■■■■" << endl;
cout << "■■■■■■■■■■■■■■■■■" << endl;
}
else if (nKeyState==e_GO_StartGame)
{
for (int i = ; i < ; i++)
{
for (int a = ; a < ; a++)
{
for (int n = ; n < nSnakeLeng; n++)
{
if (i == g_Snake[n].X&&a == g_Snake[n].Y)
{
bSnake = true;
break;
}
else
{
bSnake = false;
}
}
if (nGameFrame[i][a] == )
{
cout << "■";
}
else if (i == g_Snake[].X&&a == g_Snake[].Y)
{
cout << "◎";
}
else if (bSnake == true)
{
cout << "●";
}
else if (i == g_Food.X&&a == g_Food.Y)
{
cout << "◆";
}
else
{
cout << " ";
}
}
cout << endl;
}
}
else if (nKeyState == e_GO_GameOver)
{
cout << "GAME OVER"<<endl;
}
else if (nKeyState==e_Go_WinGame)
{
cout << "老铁666!"<<endl;
} }
return ;
}

控制台贪吃蛇小游戏

05-11 03:53