用c++类
创建头文件
#include"coordinate.h"
struct coord//坐标
{
int x;
int y;
};
1、蛇的处理
创建头文件
#include"snake.h"
#pragma once//防止头文件包含
#include<iostream>
#include<graphics.h>//图形库函数
#include<conio.h>
#include"coordinate.h"//包含坐标的头文件
#include<time.h>//随机函数头文件
using namespace std;
class Snake
{
public:
Snake();//初始化
coord Snake_l(int i);//返回蛇的坐标
int Snake_s();//返回蛇的长度
int Snake_d();//返回蛇的方向
void Draw_Snake();//画蛇
void Oper_Snake();//操作蛇
void across_wall();//过墙
protected:
coord Snake_length[100];//蛇最长为100结
int Snake_size;//蛇的长度
int Snake_dir;//蛇的方向
enum
{
right, left, up, down
};
};
创建.cpp文件
#include"snake.h"
Snake::Snake()
{
this->Snake_size = 3;
this->Snake_dir = right;
for (int i = Snake_size-1; i >= 0; i--)
{
this->Snake_length[i].x=(Snake_size-i)*10;
this->Snake_length[i].y=10;
}
}
coord Snake::Snake_l(int i)//返回蛇的坐标
{
return this->Snake_length[i];
}
int Snake::Snake_s()//返回蛇的长度
{
return this->Snake_size++;
}
int Snake::Snake_d()//返回蛇的方向
{
return this->Snake_dir;
}
void Snake::Draw_Snake()//画蛇
{
setcolor(BLACK);
for (int i = Snake_size-1; i > 0; i--)
{
this->Snake_length[i].x = this->Snake_length[i - 1].x;
this->Snake_length[i].y = this->Snake_length[i - 1].y;
rectangle(this->Snake_length[i].x, this->Snake_length[i].y,
this->Snake_length[i].x + 10, this->Snake_length[i].y + 10);
}
switch (this->Snake_dir)
{
case right:
Snake_length[0].x += 10;
break;
case left:
Snake_length[0].x -= 10;
break;
case up:
Snake_length[0].y -= 10;
break;
case down:
Snake_length[0].y += 10;
break;
}
rectangle(this->Snake_length[0].x, this->Snake_length[0].y,
this->Snake_length[0].x + 10, this->Snake_length[0].y + 10);
}
void Snake::Oper_Snake()//操作蛇
{
char ch=0;//按
if (_kbhit())//按键存在
{
ch = _getch();//获取按键值
switch (ch)
{
case 'W':
case 'w':
case 72://上
if (this->Snake_dir != down)
{
this->Snake_dir = up;
}
break;
case 'S':
case 's':
case 80://下
if (Snake_dir != up)
{
Snake_dir = down;
}
break;
case 'A':
case 'a':
case 75://左
if (Snake_dir != right)
{
Snake_dir = left;
}
break;
case 'D':
case 'd':
case 77://右
if (Snake_dir != left)
{
Snake_dir = right;
}
break;
case 27:
exit(0);
break;
}
}
}
void Snake::across_wall()//过墙
{
switch (Snake_dir)
{
case right:
if (Snake_length[0].x > 640)
{
Snake_length[0].x -= 640;
}
break;
case left:
if (Snake_length[0].x <0)
{
Snake_length[0].x += 640;
}
break;
case up:
if (Snake_length[0].y < 0)
{
Snake_length[0].y += 470;
}
break;
case down:
if (Snake_length[0].y > 480)
{
Snake_length[0].y -= 480;
}
break;
}
}
2、食物处理
食物头文件
#include"food.h"
#pragma once
#include<iostream>
#include<time.h>
#include<conio.h>
#include<graphics.h>
#include"coordinate.h"
using namespace std;
class Food
{
public:
Food();
bool Food_e(bool);//修改食物存在信息
void Draw_food();//画食物
coord Food_d();//返回食物坐标
bool Food_ex();//食物是否存在
void Creat_food();//创建食物
protected:
coord Food_dir;//食物的坐标
bool Food_exist;//食物是否存在
};
.cpp文件
#include"food.h"
Food::Food()
{
//随机食物坐标
Food_dir.x = rand() % 64 * 10;
Food_dir.y = rand() % 48 * 10;
Food_exist = false;
}
bool Food::Food_e(bool a)
{
Food_exist = a;
return a;
}
coord Food::Food_d()//返回食物的坐标
{
return Food_dir;
}
bool Food::Food_ex()
{
return Food_exist;
}
void Food::Creat_food()//创建食物
{
Food_dir.x = rand() % 64 * 10;
Food_dir.y = rand() % 48 * 10;
}
void Food::Draw_food()//画食物
{
setcolor(RED);//设置颜色
circle(Food_dir.x, Food_dir.y, 5);
Food_exist = true;
}
3、主函数界面
#include"main.cpp"
#include"snake.h"
#include"food.h"
void Eat_Food(Food *pFood, Snake *pSnake)
{
if (pFood->Food_ex())
{
if (pSnake->Snake_l(0).x+5 >= pFood->Food_d().x - 8 && pSnake->Snake_l(0).x+5 <= pFood->Food_d().x + 8 &&
pSnake->Snake_l(0).y+5 >= pFood->Food_d().y - 8 && pSnake->Snake_l(0).y+5 <= pFood->Food_d().y + 8)
{
pSnake->Snake_s();
pFood->Food_e(false);
}
}
}
int main()
{
srand(time(NULL));//随机初始化种子
initgraph(640, 480);//窗口化大小
setbkcolor(WHITE);//背景色设置
Food pFood;
Snake pSnake;
pFood.Draw_food();
while (1)
{
cleardevice();//刷新
pSnake.Draw_Snake();
pSnake.Oper_Snake();
if (!pFood.Food_ex())
{
pFood.Creat_food();
}
pFood.Draw_food();
Eat_Food(&pFood, &pSnake);
pSnake.across_wall();
Sleep(100);//延时 ( 蛇的速度)
}
_getch();
return 0;
}