#include <iostream>
#include <ctime>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#define Map_X 26
#define Map_Y 80
using namespace std;
string surfaceStatus = "MainSurface";
string hardMode = "简单";
int speed = 800;
int score = 0;
char gameMap[Map_X][Map_Y];
int gameover;
long start;
char color[7] = "";
struct Point{
int x;
int y;
};
struct Pre_Direction{
int x;
int y;
};
class Food{
public:
int x,y;
void createFood(){
srand(time(NULL)+1);
x = rand()%(Map_X-2 - 2 + 1) + 2;
y = rand()%(Map_Y-2 - 2 + 1) + 2;
}
};
class Snake{
private:
struct Pre_Direction *direction;
public:
int body_length;
struct Point *body;
bool isDead;
Snake(){
isDead = false;
body_length = 2;
srand(time(NULL));
body = (Point*)malloc(sizeof(Point)*Map_X*Map_Y);
direction = (Pre_Direction*)malloc(sizeof(Pre_Direction)*Map_X*Map_Y);
body[0].x = rand()%(Map_X-2 - 2 + 1) + 2;
body[0].y = rand()%(Map_Y-2 - 2 + 1) + 2;
body[1].x = body[0].x;
body[1].y = body[0].y - 1;
direction[0].x = 0;
direction[0].y = 1;
direction[1].x = 0;
direction[1].y = 1;
}
void Move(int operate){
switch(operate){
case 72:
direction[0].x = -1;
direction[0].y = 0;
break;
case 80:
direction[0].x = 1;
direction[0].y = 0;
break;
case 75:
direction[0].x = 0;
direction[0].y = -1;
break;
case 77:
direction[0].x = 0;
direction[0].y = 1;
break;
}
for (int i = 0;i < body_length;i++){
body[i].x += direction[i].x;
body[i].y += direction[i].y;
}
for (int i = body_length;i > 1;i--){
direction[i-1].x = direction[i-2].x;
direction[i-1].y = direction[i-2].y;
}
if (body[0].x + direction[0].x <= -1 || body[0].x + direction[0].x >= Map_X+2 || body[0].y + direction[0].y <= -1 || body[0].y + direction[0].y >= Map_Y+2){
isDead = true;
return;
}
else if (gameMap[body[0].x][body[0].y] != ' ' && gameMap[body[0].x][body[0].y] != '*'){
isDead = true;
return;
}
}
bool EatFood(int food_x,int food_y){
if (body[0].x == food_x && body[1].y == food_y){
body_length++;
body[body_length-1].x = body[body_length-2].x - direction[body_length-2].x;
body[body_length-1].y = body[body_length-2].y - direction[body_length-2].y;
direction[body_length-1].x = direction[body_length-2].x;
direction[body_length-1].y = direction[body_length-2].y;
score += - speed + 850;
if (hardMode == "自动"){
speed -= score / 40;
if (speed <= 20){
speed = 20;
}
}
return true;
}
else{
return false;
}
}
};
class MainSurface{
private:
int operate;
public:
void mainSurface(){
cout << "\t\t\t\t\t\t贪吃蛇\n" << endl;
cout << "\t\t\t\t\t 1.开始游戏\n\n" << endl;
cout << "\t\t\t\t\t 2.设置及帮助\n\n" << endl;
cout << "\t\t\t\t\t\t 3.退出\n\n" << endl;
cin >> operate;
switch(operate){
case 1:
surfaceStatus = "ChooseHardMode";
system("cls");
return;
case 2:
surfaceStatus = "SettingSurface";
system("cls");
break;
case 3:
exit(0);
break;
}
}
};
class ChooseHardMode{
private:
int operate;
public:
void chooseHardMode(){
cout << "选择难度:" << endl;
cout << "1.简单" << endl;
cout << "2.普通" << endl;
cout << "3.困难" << endl;
cout << "4.自动\n" << endl;
cout << "0.返回" << endl;
cin >> operate;
switch(operate){
case 1:
speed = 800;
hardMode = "简单";
surfaceStatus = "GameSurface";
break;
case 2:
speed = 600;
hardMode = "普通";
surfaceStatus = "GameSurface";
break;
case 3:
speed = 300;
hardMode = "困难";
surfaceStatus = "GameSurface";
break;
case 4:
speed = 800;
hardMode = "自动";
surfaceStatus = "GameSurface";
break;
case 0:
surfaceStatus = "MainSurface";
break;
}
system("cls");
return;
}
};
class SettingSurfasce{
private:
int operate;
char bgColor[1];
char preColor[2];
public:
void settingSurface(){
cout << "帮助:" << endl;
cout << "贪吃蛇游戏是一款经典的益智游戏,有PC和手机等多平台版本。既简单又耐玩。该游戏通过控制蛇头方向吃蛋,从而使得蛇变得越来越长。" << endl;
cout << "使用方向键控制蛇的上下左右移动,通过吃食物使身体增长,蛇头不能碰到墙也不能碰到自己的身体。" << endl;
cout << "\n\n\n1.设置颜色(输入背景色及前景色):\t\tEsc.返回\n" << endl;
cout << "0 = 黑色\t\t1 = 蓝色\t\t2 = 绿色\t\t3 = 湖蓝色" << endl;
cout << "4 = 红色\t\t5 = 紫色\t\t6 = 黄色\t\t7 = 白色" << endl;
cout << "8 = 灰色\t\t9 = 淡蓝色\t\tA = 淡绿色\t\tB = 淡浅绿色" << endl;
cout << "C = 淡红色\t\tD = 淡紫色\t\tE = 淡黄色\t\tF = 亮白色" << endl;
if (getch() == 27){
surfaceStatus = "MainSurface";
system("cls");
return;
}
else{
cin >> bgColor;
cin >> preColor;
strcat(color,"color ");
strcat(color,bgColor);
cout << color;
system(color);
surfaceStatus = "MainSurface";
color[0] = '\0';
system("cls");
return;
}
}
};
class GameSurface{
private:
int operate;
Snake snake;
Food food;
public:
GameSurface(){
food.createFood();
for (int i = 0;i < Map_X;i++){
for (int j = 0;j < Map_Y;j++){
gameMap[i][j] = ' ';
}
}
}
void update(){
system("cls");
drawMap();
start = clock();
while((gameover=(clock()-start<=speed))&&!kbhit());
if(gameover)
{
operate = getch();
if (operate == ' '){
while(getch() != ' ');
}
else if(operate == 27){
surfaceStatus = "MainSurface";
system("cls");
return;
}
}
snake.Move(operate);
if (snake.isDead){
surfaceStatus = "DeathSurface";
system("cls");
return;
}
if (snake.EatFood(food.x,food.y)){
food.createFood();
}
}
void drawMap(){
for (int i = 0;i < Map_X + 2;i++){
for (int j = 0;j < Map_Y + 2;j++){
gameMap[i][j] = ' ';
gameMap[food.x][food.y] = '*';
gameMap[snake.body[0].x][snake.body[0].y] = '@';
for (int k = 1;k < snake.body_length;k++){
gameMap[snake.body[k].x][snake.body[k].y] = '#';
}
if (i == 0 || i == Map_X + 1){
cout << "+";
}
else{
if (j == 0 || j == Map_Y + 1){
cout << "+";
}
else{
cout << gameMap[i][j];
}
}
}
if (i == 2){
cout << "\t\t难度:" << hardMode;
}
if (i == 6){
cout << "\t\t分数:" << score;
}
if (i == 10){
cout << "\t\t按空格键暂停(继续)";
}
if (i == 14){
cout << "\t\t按Esc键退出";
}
cout << endl;
}
}
};
class DeathSurface{
public:
void deathSurface(){
cout << "\n\n\n\n\t\t\t\t你失败了\n\n" << endl;
cout << "按任意键退出..." << endl;
getch();
surfaceStatus = "MainSurface";
system("cls");
}
};
int main(){
GameSurface *gS;
gS = new GameSurface();
while (1){
if (surfaceStatus == "MainSurface"){
MainSurface mS;
gS = new GameSurface();
score = 0;
mS.mainSurface();
}
if (surfaceStatus == "ChooseHardMode"){
ChooseHardMode cH;
cH.chooseHardMode();
}
if (surfaceStatus == "SettingSurface"){
SettingSurfasce sS;
sS.settingSurface();
}
if (surfaceStatus == "GameSurface"){
gS->update();
}
if (surfaceStatus == "DeathSurface"){
DeathSurface dS;
dS.deathSurface();
}
}
return 0;
}