#ifndef __COLORCONSOLE__H__
#define __COLORCONSOLE__H__
#include <windows.h>
#include <iostream>
#include <conio.h>
#include <time.h>
using namespace std;
HANDLE initiate();
BOOL textout(HANDLE hOutput,int x,int y,WORD wColors[],int nColors,LPTSTR lpszString);
#endif
#ifndef __FOOD__H__ //防止重复引用该头文件
#define __FOOD__H__
#include "colorConsole.h"
class Food
{
public:
void initfood(int); //出始化食物函数
protected:
WORD goodcolor[1]; //定义正常食物的颜色
WORD badcolor[1]; //定义变质食物的颜色
int numgood,goodx[50],goody[50]; //定义正常食物的个数、坐标
int numbad,badx[30],bady[30]; //定义变质食物的个数、坐标
};
#endif
#ifndef __FOOD__H__ //防止重复引用该头文件
#define __FOOD__H__
#include "colorConsole.h"
class Food
{
public:
void initfood(int); //出始化食物函数
protected:
WORD goodcolor[1]; //定义正常食物的颜色
WORD badcolor[1]; //定义变质食物的颜色
int numgood,goodx[50],goody[50]; //定义正常食物的个数、坐标
int numbad,badx[30],bady[30]; //定义变质食物的个数、坐标
};
#endif
#ifndef __SNAKE__H__ //防止重复引用该头文件
#define __SNAKE__H__
#include "colorConsole.h"
class Snake
{
public:
void initsnake(); //初始化蛇的函数
protected:
WORD bColor[1]; //定义蛇身的颜色
WORD hColor[1]; //定义蛇头的颜色
int headx,heady,bodyx[100],bodyy[100]; //定义蛇头和蛇身的坐标
int node; //定义蛇身的节数
};
#endif
#include "colorConsole.h"
HANDLE initiate()
{
HANDLE hOutput;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
return hOutput;
}
BOOL textout(HANDLE hOutput,int x,int y,WORD wColors[],int nColors,LPTSTR lpszString)
{
DWORD cWritten;
BOOL fSuccess;
COORD coord;
coord.X = x; // start at first cell
coord.Y = y; // of first row
fSuccess = WriteConsoleOutputCharacter(
hOutput, // screen buffer handle
lpszString, // pointer to source string
lstrlen(lpszString), // length of string
coord, // first cell to write to
&cWritten); // actual number written
if (! fSuccess)
cout<<"error:WriteConsoleOutputCharacter"<<endl;
for (;fSuccess && coord.X < lstrlen(lpszString)+x; coord.X += nColors)
{
fSuccess = WriteConsoleOutputAttribute(
hOutput, // screen buffer handle
wColors, // pointer to source string
nColors, // length of string
coord, // first cell to write to
&cWritten); // actual number written
}
if (! fSuccess)
cout<<"error:WriteConsoleOutputAttribute"<<endl;
return 0;
}
#include "Food.h"
HANDLE handle;
void Food::initfood(int lev) //初始化食物
{
numgood=lev*5;
numbad=lev*2;
goodcolor[0]=FOREGROUND_RED|FOREGROUND_BLUE; //正常食物的颜色
badcolor[0]=FOREGROUND_RED|FOREGROUND_GREEN; //变质食物的颜色
srand((unsigned) time(NULL));
textout(handle,64,6,goodcolor,1,"正常食物为★"); //输出图例
textout(handle,64,8,badcolor,1,"变质食物为★");
for(int fgood=0;fgood<numgood;fgood++) //输出正常食物
{
goodx[fgood]=((rand()%55+4)/2)*2;
goody[fgood]=rand()%26+4;
textout(handle,goodx[fgood],goody[fgood],goodcolor,1,"★");
}
for(int fbad=0;fbad<numbad;fbad++) //输出变质食物
{
badx[fbad]=((rand()%56+4)/2)*2;
bady[fbad]=rand()%26+4;
textout(handle,badx[fbad],bady[fbad],badcolor,1,"★");
}
}
#include "Game.h"
HANDLE handle;
Game::Game()
{
node=3; //蛇的出始默认节数
headx=44,heady=4; //蛇头的出始默认坐标
controlx=-2,controly=0; //蛇的出始默认方向为向左
start=0;
scores=0;
self=0,wall=0,died=0; //游戏结束条件默认为否
sColor[0]=FOREGROUND_GREEN|FOREGROUND_INTENSITY; //围墙颜色
head[0]=FOREGROUND_RED|FOREGROUND_INTENSITY; //开始界面文字颜色
}
int Game::starting()
{
while(1)
{
textout(handle,22,16,head,1,"控制键: W-上 S-下 A-左 D-右"); //开始界面
textout(handle,16,18,head,1,"吃完所有正常食物升级 千万不要吃到变质的食物哦!");
textout(handle,30,20,head,1, "选择级数 1-9 ");
if(_kbhit())
{
int v=_getch(); //选择级数
switch(v)
{
case 49:
level=1;
break;
case 50:
level=2;
break;
case 51:
level=3;
break;
case 52:
level=4;
break;
case 53:
level=5;
break;
case 54:
level=6;
break;
case 55:
level=7;
break;
case 56:
level=8;
break;
default:
level=9;
break;
}
textout(handle,16,18,head,1," "); //清屏
textout(handle,30,20,head,1, " ");
textout(handle,22,16,head,1," ");
break;
}
}
//设置四周围墙
for(int wideup=2;wideup<60;wideup+=2) //上边
{
textout(handle,wideup,2,sColor,1,"■");
}
for(int widedown=2;widedown<60;widedown+=2) //下边
{
textout(handle,widedown,30,sColor,1,"■");
}
for(int longr=2;longr<30;longr++) //左边
{
textout(handle,2,longr,sColor,1,"■");
}
for(int longl=2;longl<31;longl++) //右边
{
textout(handle,60,longl,sColor,1,"■");
}
levelup=5*level;
return level;
}
void Game::ifstart() //由用户判断是否开始游戏
{
textout(handle,24,0,sColor,1,"按 y 开始游戏");
int v=_getch();
switch(v)
{
case 'y': start=1;break;
default : start=0;break;
}
textout(handle,24,0,sColor,1," "); //清屏
}
bool Game::getstart() //获得start
{
return start;
}
void Game::moventurn() //关于移动和转向的函数
{
int len1=0; //设置一个消除原有图形的计数器
for(int len=0;len<node;len++) //消除原有图形
{
textout(handle,bodyx[len],bodyy[len],bColor,1," ");
}
for(int len6=node-1;len6>0;len6--) //显示移动以后的图形
{
len1=len6-1;
bodyx[len6]=bodyx[len1]; //获得前一节body的坐标
bodyy[len6]=bodyy[len1];
textout(handle,bodyx[len6],bodyy[len6],bColor,1,"●"); //移动后的图形
}
bodyx[0]=headx;
bodyy[0]=heady;
textout(handle,bodyx[0],bodyy[0],bColor,1,"●"); //显示移动后的第一节
headx+=controlx;
heady+=controly;
textout(handle,headx,heady,hColor,1,"◎"); //显示移动后的头部
Sleep(165-level*17); //速度
if(kbhit()) //转向
{
char direction=getch(); //获取方向
if(direction=='w'&&direc!='s')
{
textout(handle,headx,heady,hColor,1," ");
textout(handle,headx,heady,hColor,1,"◎");
controlx=0;
controly=-1;
direc='w';
}
if(direction=='s'&&direc!='w')
{
textout(handle,headx,heady,hColor,1," ");
textout(handle,headx,heady,hColor,1,"◎");
controlx=0;
controly=1;
direc='s';
}
if(direction=='a'&&direc!='d')
{
textout(handle,headx,heady,hColor,1," ");
textout(handle,headx,heady,hColor,1,"◎");
controlx=-2;
controly=0;
direc='a';
}
if(direction=='d'&&direc!='a')
{
textout(handle,headx,heady,hColor,1," ");
textout(handle,headx,heady,hColor,1,"◎");
controlx=2;
controly=0;
direc='d';
}
}
}
bool Game::ifgameover() //判断是否结束游戏
{
if(headx==2||headx==60||heady==2||heady==30) //撞墙
{
wall=1;
return 1;
}
for(int sbody=1;sbody<node;sbody++) //咬到自己
{
if(headx==bodyx[sbody]&&heady==bodyy[sbody])
{
self=1;
return 1;
}
}
for(int countbad=0;countbad<numbad;countbad++) //吃到变质食物
{
if(headx==badx[countbad]&&heady==bady[countbad])
{
died=1;
return 1;
}
}
return 0;
}
void Game::gameover() //输出游戏结束界面
{
//sndPlaySound("DOWN.wav",SND_ASYNC);
if(wall==1)
textout(handle,20,14,sColor,1,"撞到墙了 游戏结束");
if(self==1)
textout(handle,20,14,sColor,1,"咬到自己了 游戏结束");
if(died==1)
textout(handle,19,14,sColor,1,"吃到变质食物了 游戏结束");
}
void Game::scorenlevelup()
{
char marks[5],Level[5]; //准备输出级数和分数
for(int eatgood=0;eatgood<numgood;eatgood++) //判断是否咬到正常食物以加分
{
if(headx==goodx[eatgood]&&heady==goody[eatgood])
{
//sndPlaySound("LASER.wav",SND_ASYNC);
goodx[eatgood]=74; //防止以后走到已吃过食物点的时候会判断为吃到而加分以影响升级
goody[eatgood]=6;
node++; //长一截
scores+=10; //加十分
itoa(scores,marks,10); //转换数据类型,准备输出
itoa(level,Level,10);
textout(handle,64,11,hColor,1,"分数:"); //输出分数
textout(handle,70,11,hColor,1,marks);
textout(handle,64,13,hColor,1,"级数:"); //输出级数
textout(handle,70,13,hColor,1,Level);
levelup--;
if(levelup==4)
{
for(int fgood=0;fgood<numgood;fgood++) //再次输出正常食物
{
textout(handle,goodx[fgood],goody[fgood],goodcolor,1,"★");
}
}
}
}
if(levelup==0) //升级
{
level++;
levelup=5*level;
for(int i=4;i<58;i+=2) //清屏
{
for(int j=4;j<30;j++)
{
textout(handle,i,j,sColor,1," ");
}
}
initfood(level); //输出食物
textout(handle,headx,heady,hColor,1,"◎"); //打印蛇头
for(int j=0;j<node;j++) //打印蛇身
{
textout(handle,bodyx[j],bodyy[j],bColor,1,"●");
}
textout(handle,18,0,hColor,1,"按任意键开始更高的等级");
itoa(level,Level,10);
textout(handle,70,13,hColor,1,Level); //输出新的级数
while(1) //由用户控制是否开始更高等级
{
if (_kbhit())
{
textout(handle,18,0,sColor,1, " ");
break;
}
}
}
}
#include "Snake.h"
extern HANDLE handle;
void Snake::initsnake() //初始化蛇
{
hColor[0]=FOREGROUND_RED|FOREGROUND_INTENSITY; //蛇头颜色
bColor[0]=FOREGROUND_BLUE|FOREGROUND_INTENSITY; //蛇身颜色
for(int i=0;i<node;i++) //蛇身各节的坐标定义
{
bodyx[i]=(i+1)*2+headx;
bodyy[i]=heady;
}
textout(handle,headx,heady,hColor,1,"◎"); //打印蛇头
for(int j=0;j<node;j++) //打印蛇身
{
textout(handle,bodyx[j],bodyy[j],bColor,1,"●");
}
}
#include "Snake.h"
#include "Food.h"
#include "Game.h"
Game game;
HANDLE handle; //窗口句柄
void main()
{
//sndPlaySound("BACKGROUND.wav",SND_LOOP|SND_ASYNC);
handle=initiate(); //初始化窗口句柄
game.initfood(game.starting()); //初始化食物
game.initsnake(); //初始化蛇
game.ifstart(); //玩家是否开始游戏
if(game.getstart())
{ while(1)
{
game.moventurn(); //蛇的移动和转向
game.scorenlevelup(); //得分和升级
if(game.ifgameover()) //判断是否中止游戏
break;
}
game.gameover(); //输出游戏结束原因
}
}
这是一个控制台的贪吃蛇 比较简单
只要楼主了解几个API函数就可以 至于算法 有好多 我这个不怎么样
我建议用链表来表示蛇体 下面是我的一个snake 类 函数体没给 楼主自己想吧
struct node
{node* pre;
Cpoint point;
node* next;
};
node::node()
{ point(0,0);
pre=NULL;
next=NULL;
};
class snake
{ private:
node* snakehead;
node snakebody[100];
int length
int direction;
public:
void go();
bool fail();
void textout();
} ;
温馨提示:内容为网友见解,仅供参考