头文件<Snake.h>
#include<iostream>
#include<windows.h>
#include<cstdbool>
#include<cstdlib>
#include<conio.h>
#include<ctime>
using namespace std;
//#include<mmsyscom.h>
//#include<mciapi.h>
//#include<windef.h>
#define _CRT_SECURE_NO_WARNINGS 1
//宏定义按键函数
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
//地图边长
#define length 40
//游戏模式s
#define RULE 2
//刷新速率 0-2
#define Speed 20
//0:NULL ; 1:WALL ; 2:head ; 3:body ; 4:FOOD
struct Body
{int x;int y;Body* Next;
};
class Snake
{public:bool Move();bool Die();Body* Dataget(void);int LDataget(void);Snake(){head = new Body;head->x = length / 2;head->y = length / 2;head->Next = NULL;Length = 1;}~Snake(void){ delete(head);}private:Body* head;unsigned int Length;
};
//枚举方向
void Deplay(Snake* TheSnake);
void initialize(Body* head);
bool Action(Snake* TheSnake);
bool Auto_Action(Snake* TheSnake);
bool FoodCheck(void);
bool Auto_FoodCreate(bool check);
void Refresh(Body* head);enum class DIRECTION
{UP ,DOWN,LEFT,RIGHT
};
//设置初始蛇方向为上
enum class DIRECTION Direction = DIRECTION::UP;
//地图
int map[length][length];
//食物坐标结构初始化
struct food
{int x;int y;
}food;
//蛇移动
bool Snake::Move()
{Body* p , *q , *end;p = head;end = p->Next;switch (Direction){case DIRECTION::UP:if (map[head->x - 1][head->y] == 0 )//2 :空地{q = new Body;q->x = head->x - 1;q->y = head->y;q->Next = head;head = q;if (end != NULL){while (end->Next != NULL){p = end;end = end->Next;}p->Next = NULL;}elsehead->Next = NULL;}else if (map[head->x - 1][head->y] == 1 || map[head->x - 1][head->y] == 3)//1 :墙壁 3 :身体{return (Snake::Die());}else if (map[head->x - 1][head->y] == 4)//4 :食物{Length += 1;q = new Body;q->x = head->x - 1;q->y = head->y;q->Next = head;head = q;}break;case DIRECTION::DOWN:if (map[head->x + 1][head->y] == 0)//2 :空地{q = new Body;q->x = head->x + 1;q->y = head->y;q->Next = head;head = q;if (end != NULL){while (end->Next != NULL){p = end;end = end->Next;}p->Next = NULL;}elsehead->Next = NULL;}else if (map[head->x + 1][head->y] == 1 || map[head->x + 1][head->y] == 3)//1 :墙壁 3 :身体{return (Snake::Die());}else if (map[head->x + 1][head->y] == 4)//4 :食物{Length += 1;q = new Body;q->x = head->x + 1;q->y = head->y;q->Next = head;head = q;}break;case DIRECTION::LEFT:if (map[head->x ][head->y - 1] == 0)//2 :空地{q = new Body;q->x = head->x;q->y = head->y - 1;q->Next = head;head = q;if (end != NULL){while (end->Next != NULL){p = end;end = end->Next;}p->Next = NULL;}elsehead->Next = NULL;}else if (map[head->x][head->y - 1] == 1 || map[head->x][head->y - 1] == 3)//1 :墙壁 3 :身体{return (Snake::Die());}else if (map[head->x][head->y - 1] == 4)//4 :食物{Length += 1;q = new Body;q->x = head->x;q->y = head->y - 1;q->Next = head;head = q;}break;case DIRECTION::RIGHT:if (map[head->x][head->y + 1] == 0)//2 :空地{q = new Body;q->x = head->x;q->y = head->y + 1;q->Next = head;head = q;if (end != NULL){while (end->Next != NULL){p = end;end = end->Next;}p->Next = NULL;}elsehead->Next = NULL;}else if (map[head->x][head->y + 1] == 1 || map[head->x][head->y + 1] == 3)//1 :墙壁 3 :身体{return (Snake::Die());}else if (map[head->x][head->y + 1] == 4)//4 :食物{Length += 1;q = new Body;q->x = head->x;q->y = head->y + 1;q->Next = head;head = q;}break;default:break;}Refresh(head);return TRUE;
}
//蛇死亡
bool Snake::Die()
{return FALSE;
}
//获取蛇头结构指针
Body* Snake::Dataget(void)
{return head;
}
//获取蛇长
int Snake::LDataget()
{return (int)Length;
}
//展示
void Deplay(Snake* TheSnake)
{int sum = 0;Body* p = (TheSnake->Dataget());for (int i = 0; i < length; i++){for (int j = 0; j < length; j++){if (map[i][j] == 0)cout << " ";else if (map[i][j] == 1)cout << "//";else if (map[i][j] == 2)cout << "■";else if (map[i][j] == 3)cout << "□";else if (map[i][j] == 4)cout << "☆";//0:NULL ; 1:WALL ; 2:head ; 3:body//4:FOOD}printf("n");}cout << "Q键终止程序---当前蛇长 " << (TheSnake->LDataget()) << endl;return;
}
//地图初始化
void initialize(Body* head)
{int i, j;for (i = 0; i < length; i++){for (j = 0; j < length; j++){if (i == 0 || j == 0 || i == length - 1 || j == length - 1)map[i][j] = 1;else if (i == head->x && j == head->y)map[i][j] = 2;elsemap[i][j] = 0;//0:NULL ; 1:WALL ; 2:head ; 3:body//4:FOOD}}return;
}
//读取指令行动
bool Action(Snake *TheSnake)
{char ch = 0;ch = _getch();switch (ch){case 'W':case 'w':case 72 :case 50 :Direction = DIRECTION::UP;break;//UPcase 'S':case 's':case 80 :case 56 :Direction = DIRECTION::DOWN;break;//DOWNcase 'A':case 'a':case 75 :case 52 :Direction = DIRECTION::LEFT;break;//LEFTcase 'D':case 'd':case 77 :case 54 :Direction = DIRECTION::RIGHT;break;//RIGHTcase 'q':case 'Q':return FALSE;break;//终止程序default:break;}return (TheSnake->Move());
}
//检查是否存在食物
bool FoodCheck(void)
{int i, j;for (i = 0; i < length; i++){for (j = 0; j < length; j++){if (map[i][j] == 4)return FALSE;}}return TRUE;
}
//食物生成
bool Auto_FoodCreate(bool check)
{int x,y;srand((unsigned int)time(0));if (check== FALSE)return FALSE;x = rand() % length;y = rand() % length;if (map[x][y] == 0 ){map[x][y] = 4;food.x = x;food.y = y;return FALSE;}return TRUE;
}
//地图刷新
void Refresh(Body* head)
{int i, j;Body* p;p = head->Next;map[head->x][head->y] = 5;while (p != NULL){map[p->x][p->y] = 6;p = p->Next;}for (i = 0; i < length; i++){for (j = 0; j < length; j++){if (i == 0 || j == 0 || i == length - 1 || j == length - 1)map[i][j] = 1;else if (map[i][j] == 5)map[i][j] = 2;else if (map[i][j] == 6)map[i][j] = 3;else if(map[i][j] !=4)map[i][j] = 0;//0:NULL ; 1:WALL ; 2:head ; 3:body//4:FOOD}}return;
}
//自动行动
bool Auto_Action(Snake* TheSnake)
{if ((KEY_DOWN('W') || KEY_DOWN('w')) && Direction != DIRECTION::DOWN)//UP{Direction = DIRECTION::UP;}else if ((KEY_DOWN('S') || KEY_DOWN('s')) && Direction != DIRECTION::UP)//DOWN{Direction = DIRECTION::DOWN;}else if ((KEY_DOWN('A') || KEY_DOWN('a')) && Direction != DIRECTION::RIGHT)//LEFT{Direction = DIRECTION::LEFT;}else if ((KEY_DOWN('D') || KEY_DOWN('d')) && Direction != DIRECTION::LEFT)//RIGHT{Direction = DIRECTION::RIGHT;}else if ((KEY_DOWN('q') || KEY_DOWN('Q')))return FALSE;else if ((KEY_DOWN( VK_SPACE))){system("pause");}Sleep(Speed);return (TheSnake->Move());
}
TheLinkSnake.cpp
#include"Snake1.h"int main(void)
{Snake TheSnake;bool FLAG = 1;system("mode con cols=80 lines=46" );//设置cmd窗口大小SetConsoleTitleA("TheSnake");//更改窗口标题system("color 0a");//更换控制台颜色为蓝底红字//mciSendString("open dylanf - 卡农(经典钢琴版).mp3",0,0,0);initialize(TheSnake.Dataget());//初始化while (FLAG){ //生成食物while (Auto_FoodCreate(FoodCheck()));//清屏system("cls");//展示Deplay(&TheSnake);if (RULE == 1){//读取指令行动--判断游戏终止FLAG = (Action(&TheSnake));}else if (RULE == 2){FLAG = (Auto_Action(&TheSnake));}}return 0;
}
本文发布于:2024-02-01 22:12:14,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170679673539752.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |