资源简介
寒假里自己写的一个贪吃蛇小游戏,利用队列写出来的,代码不多,大家可以看看
代码片段和文件信息
#include
#include
#include
#include
#include
using namespace std;
//*********************************************************************数据声明区
#define Max_Length 1000
#define width 20 //宽度
#define length 30 //长度
#define Speed 10 //速度
int map[length][width]; //地图
enum Flag {creatdestory};
struct Direction //方向结构体
{
int add_x add_y;
};
struct Direction Move[4];
struct Node //节点结构体
{
int x y; //坐标
int d; //方向
};
class Snake_Body //队列类
{
public:
Snake_Body(); //构造函数(初始化函数)
void Push(struct Node tmp); //入队函数
void Pop(struct Node *tmp); //出队函数
void Get_top(struct Node *tmp); //取队首函数
void Get_end(struct Node *tmp); //取队尾函数
private:
struct Node node[Max_Length];
int top; //队首
int end; //队尾
};
class Snake //贪吃蛇类
{
public:
Snake(); //构造函数(初始化)
int control(); //控制函数
int move(); //移动函数
int get_food(); //判断是否吃到食物
private:
Node head; //头
Node tail; //尾巴
Snake_Body body; //身体
};
//**********************************************************************函数声明
void Set_Move(); //初始化方向结构体
void Set_Map(); //初始化地图
void creat_food(); //产生食物
void out(int& x int& y); //越界处理
void gameover(); //游戏结束
void print_point(struct Node tmp enum Flag flag); //画图函数
//**********************************************************************主函数
void main()
{
initgraph(width*11length*11);
Set_Move(); //初始化方向结构体
Set_Map(); //初始化地图
Snake snake;
snake.control();
gameover();
_getch();
closegraph();
}
//******************************************************************************************************************函数实现
//****************************************************************队列的相关函数
Snake_Body::Snake_Body() //构造函数(初始化函数)
{
top = end = -1;
}
void Snake_Body::Push(struct Node tmp) //入队函数
{
node[top].d = tmp.d;
top++;
if (top == Max_Length)
top = 0;
node[top] = tmp;
}
void Snake_Body::Pop(struct Node *tmp) //出队函数
{
end++;
if (end == Max_Length)
end = 0;
*tmp = node[end];
}
void Snake_Body::Get_top(struct Node *tmp) //取队首
{
*tmp = node[top];
}
void Snake_Body::Get_end(struct Node *tmp) //取队尾
{
*tmp = node[end];
}
//*****************************************************************************贪吃蛇的相关函数
Snake::Snake() //构造函数(初始化)
{
head.x = head.y = 1;
tail.x = tail.y = 1;
head.d = tail.d = 0;
body.Push(head);
}
int Snake::control() //控制函数
{
char choice;
int x yd;
Set_Move();
creat_food();
while (1)
{
body.Get_top(&head);
if (_kbhit() == 0)
{
d = head.d;
x = head.x + Move[d].add_x;
y = head.y + Move[d].add_y;
out(x y);
if (map[y][x] == 1)
return 0;
else
{
if(map[y][x]!=2)
map[y][x] = 1;
head.x = x;
head.y = y;
head.d =
- 上一篇:RSA加解密算法 C语言实现
- 下一篇:VC++实现算数编码
相关资源
- c++ 简易贪吃蛇源码
- 贪吃蛇大作战(c源码+报告文档)
- C语言ege贪吃蛇游戏
- C++贪吃蛇控制台小游戏代码
- 彩色贪吃蛇.c
- c++游戏程序(包含源代码,有扫雷,
- 基于qt的c++编写的贪吃蛇游戏
- c语言 linux 贪吃蛇.doc
- 基于sfml的贪吃蛇
- 基于SDL的贪吃蛇游戏
- 贪吃蛇VC6 MFC
- MFC VC6.0 简单贪吃蛇
- MFC下实现的贪吃蛇
- C++课程设计贪吃蛇源码+课设报告
- MFC课程设计报告-手把手教你写贪吃蛇
- C++写的贪吃蛇(含源代码和注释,采
- 基于c++的贪吃蛇游戏设计
- c++贪吃蛇编程源代码论文设计c语言
- VC6.0贪吃蛇MFC
- C语言基于SDL的贪吃蛇项目
- 贪吃蛇(有完全源码和每一个功能的
- VC++版贪吃蛇游戏源代码
- vc++6.0 MFC 写的贪吃蛇工程源码
- MFC 贪吃蛇
- MFC编写的贪吃蛇源码
- visual c++的几个经典小游戏
- C++贪吃蛇设计+论文
- 用Visual C++6.0开发的贪吃蛇游戏含注释
- C语言贪吃蛇
- opengl/c++贪吃蛇.rar
评论
共有 条评论