message (2)456
message (2)456
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#include <queue>
#define SNAKE 1
#define EGG 2
#define WALL 3
void HideCursor();
void gotoxy(int, int);
void HideCursor(){
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x, int y){
COORD loc;
HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
loc.X = x;
loc.Y = y;
SetConsoleCursorPosition(hOuput,loc);
}
class GM;
class Snake;
class Object
{
private:
static char allmap[HEIGHT][WIDTH];
public:
void change(int a,int b,int number)
{
allmap[a][b]=number;
}
int call(int a,int b)
{
return allmap[a][b];
}
};
char Object::allmap[HEIGHT][WIDTH]={0};
void Move(){
unsigned char key;
if(kbhit()){
key = getch();
switch(key)
{
case DIRECTION:
switch(key = getch())
{
case UP:
direction = UP;
break;
case DOWN:
direction = DOWN;
break;
case LEFT:
direction = LEFT;
break;
case RIGHT:
direction = RIGHT;
break;
}
}
}
Forward();
}
void Forward(){
switch(direction){
case UP:
if(y>0) y--;
break;
case DOWN:
if(y<HEIGHT-1) y++;
break;
case LEFT:
if(x>0) x--;
break;
case RIGHT:
if(x<WIDTH-1) x++;
break;
}
}
int check()
{
if(call(y,x)==SNAKE)
{
return 0;
}
else if(call(y,x)==EGG)
{
len++;
return 1;
}
else if(call(y,x)==WALL)
{
return 0;
}
return 2;
}
void bodymove()
{
if(call(y,x)!= EGG)
{
gotoxy(bodyx.front(), bodyy.front());
change(bodyy.front(), bodyx.front(), 0);
cout<<" ";
bodyx.pop();//刪除 queue 最早的元素。
bodyy.pop();
}
for (int i = 0; i < bodyx.size(); i++)
{
int tx = bodyx.front();
int ty = bodyy.front();
bodyx.pop();
bodyy.pop();
gotoxy(tx, ty);
cout << "O";
change(ty, tx, SNAKE);
bodyx.push(tx);
bodyy.push(ty);
}
bodyx.push(x);//新增元素至 queue 最後。
bodyy.push(y);
gotoxy(x,y);
cout<<shape;
}
int getscore()
{
return len-1;
}
~Snake()
{
for (int i = len; i >0; i--)
{
gotoxy(bodyx.front(), bodyy.front());
change(bodyy.front(), bodyx.front(), 0);
cout<<" ";
bodyx.pop();
bodyy.pop();
}
}
};
class Egg:public Object
{
private:
char shape;
int x,y;
public:
Egg()
{
do
{
x=rand() % (WIDTH - 2) + 1;
y=rand() % (HEIGHT - 2) + 1;
}
while(call(y,x) != 0);
shape='E';
gotoxy(x,y);
change(y,x,2);
cout<<shape;
}
};
class GM
{
private:
int HP ;
int best_score ;
public:
GM() : HP(3), best_score(0)
{
Wall wall;
wall.printmap();
Snake *snake = new Snake;
Egg *egg = new Egg;
gotoxy(WIDTH + 3, 2);
cout << "HP : * * * ";
gotoxy(WIDTH + 3, 5);
cout << "Score :";
while (1)
{
gotoxy(WIDTH + 3 + 8, 5);
cout << snake->getscore();
snake->Move();
int status = snake->check();
if (status == 0)
{
gotoxy(WIDTH + 5 + HP * 2, 2);
cout << " ";
HP--;
delete snake;
if (HP == 0)
{
gotoxy(WIDTH / 3, HEIGHT / 2);
cout << "Game Over!";
Sleep(SLEEP_TIME);
gotoxy(WIDTH / 3, HEIGHT / 2);
cout << " ";
Sleep(SLEEP_TIME);
gotoxy(WIDTH / 3, HEIGHT / 2 - 2);
cout << "Game Over!";
gotoxy(WIDTH / 3, HEIGHT / 2 + 2);
cout << "Best Score : " << best_score;
}
else
{
if (snake->getscore() > best_score)
{
best_score = snake->getscore();
}
snake = new Snake;
}
}
else if (status == 1)
{
delete egg;
egg = new Egg;
}
snake->bodymove();
Sleep(SLEEP_TIME);
}
}
};
int main()
{
HideCursor();
srand((unsigned)time(NULL));
GM *gm=new GM;
return 0;
}