#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();
} ;