#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
#define R 1;
#define L -1;
#define U 2;
#define D -2;
//方向,开始默认向右
char direct = R;
//界面长宽
int sideL = 51;
int sideS = 17;
//蛇的长度
short length = 3;
//蛇头坐标
COORD head = {sideL / 2, sideS / 2};
//蛇尾坐标
COORD tail = {0, 0};
//食物坐标
COORD f = {5, 5};
COORD snake[1000];
//光标移动
void gotoxy(int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
//标题文字
void title()
{
gotoxy(0,sideS+2);
cout << "欢迎您来到史上最low逼的贪吃蛇游戏!" << endl;
cout << "说明:\n1,本游戏没有计分,没有排行,没有模式,没有关卡\n2,按方向键移动\n3,碰到墙壁和身体,游戏结束。" << '\n';
}
//绘制墙壁(长度)
void wall(int i)
{
while (i+2 > 0)
{
cout << "█";
i--;
}
cout << endl;
}
//绘制游戏场地(长度)
void site(short i,short j)
{
while (j> 0)
{
cout << "█" << setw(i + 3) << "█" << '\n';
j--;
}
}
//蛇初始绘制
void begin()
{
for (short i = 0; i < 3; i++)
{
snake[i].X = head.X - i;
snake[i].Y = head.Y;
}
}
//食物绘制
void drawFood(COORD p){
gotoxy(p.X,p.Y);
cout<<"●";
}
//食物
void food()
{
while(1){
srand(time(0));
short x = rand() % sideL+1;
short y = rand() % sideS+1;
for(short i=0;i<length;i++){
if(x==snake[i].X&&y==snake[i].Y)
continue;
}
f = {x, y};
drawFood(f);
break;
}
}
void drawHead(COORD p)
{
gotoxy(p.X, p.Y);
cout << '0';
}
void drawBody(COORD p)
{
gotoxy(p.X, p.Y);
cout << 'O';
}
void erasure(COORD p)
{
gotoxy(p.X, p.Y);
cout << ' ';
}
//蛇身绘制
void move()
{
short x, y;
drawHead(head);
drawBody(snake[1]);
/* short i=1;
while(i<length){
x=snake[i].X;
y=snake[i].Y;
gotoxy(x,y);
cout<<"■";
i++;
}*/
erasure(tail);
}
//暂停函数
void pause()
{
getch();
}
//转向
void turn()
{
char oldDirect = direct;
//GetAsyncKeyState函数用来判断调用时指定虚假键的状态
if (GetAsyncKeyState(VK_UP))
direct = U;
if (GetAsyncKeyState(VK_DOWN))
direct = D;
if (GetAsyncKeyState(VK_LEFT))
direct = L;
if (GetAsyncKeyState(VK_RIGHT))
direct = R;
if (GetAsyncKeyState(VK_SPACE)) //按空格键暂停
{
while (1)
{
Sleep(300);
if (GetAsyncKeyState(VK_SPACE)) //再按一次空格键继续
break;
}
}
if (oldDirect + direct)
return;
else
direct = oldDirect;
}
//移动蛇头
void preMove()
{
switch (direct)
{
case 2:
head.Y--;
break;
case -2:
head.Y++;
break;
case -1:
head.X--;
break;
case 1:
head.X++;
break;
default:
break;
}
}
//判断是否吃到食物
bool ate()
{
if (head.X == f.X && head.Y == f.Y)
return 1;
else
return 0;
}
//蛇身坐标改变
void bodyMove()
{
tail = snake[length - 1];
for (int i = length; i > 1; i--)
{
snake[i - 1].X = snake[i - 2].X;
snake[i - 1].Y = snake[i - 2].Y;
}
snake[0] = head;
if (ate())
{
snake[length] = tail;
length++;
food();
}
}
//检查
bool check()
{
short x = head.X;
short y = head.Y;
if (x == 0 || x == sideL+1 || y == 0 || y == sideS+1)
return 0;
for (int i = 1; i < length; i++)
{
if (x == snake[i].X && y == snake[i].Y)
return 0;
}
return 1;
}
//结束
void endGame()
{
gotoxy(sideL + 2, sideS/2);
cout << "你死了!!!";
Sleep(3000);
exit(0);
}
// 键盘控制
void play()
{
pause();
while (1)
{
if (kbhit())
turn();
preMove();
bodyMove();
check() ? move() : endGame();
Sleep(500);
}
}
void drawInterface(){
wall(sideL);
site(sideL,sideS);
wall(sideL);
}
//初始化界面
void draw()
{
drawInterface();
title();
begin();
food();
}
int main()
{
draw();
play();
return 0;
}
这是在手机上找到的,去年写的。现在手头没有电脑,更新不了,这个源码应该可以在Windows控制台运行。