通过easyx图形库对贪食蛇的简单实现。
/************************************************
*
*名称:贪食蛇
*工具:VS2015
*作者:黑盒子
*
*************************************************/
#include <stdio.h>
#include <graphics.h>
#include <mmsystem.h>
#include <conio.h>
#include <time.h>
#include <Windows.h>
#pragma comment(lib,"winmm.lib")
#define SNAKE_LENGTH 100 //蛇的最大长度
#define SNAKESIZE 10 //食物大小
void InitSnake();
void DrawSnake();
void MoveSnake();
void ChangeSnake();
void CoorFood();
void DrawFood();
int EatFood();
void BreakSnake();
void OverSnake();
typedef enum ch
{
up = 72,
down = 80,
left = 75,
right = 77
}Ch;
//定义坐标结构体
struct Coor
{
int x;
int y;
}COOR;
//定义蛇的结构体 蛇的属性(长度 坐标 方向)
struct Snake
{
Coor szb[SNAKE_LENGTH]; //蛇每一节的坐标
int n;//当前长度
Ch ch;//方向
}SNAKE;
//定义食物的结构体
struct Food
{
Coor fzb;
int flag; //flag=0表示食物没有被吃 反之表示被吃了,
//flag 来限制食物的不断出现,每次只出现一个食物
}FOOD;
int main()
{
int flag=0;
InitSnake();
while (1)
{
while (!_kbhit())
{
if (flag == 0)
{
CoorFood();
}
DrawFood();
BreakSnake();
MoveSnake();
flag = EatFood();
Sleep(100);
}
ChangeSnake();
}
getchar();
return 0;
}
void InitSnake()
{
initgraph(640, 480);
mciSendString(L"open TheFatRat-Unity.mp3 alias bgm", 0, 0, 0);
mciSendString(L"play bgm repeat", 0, 0, 0);
SNAKE.szb[0].x = 0;//第一节蛇的坐标
SNAKE.szb[0].y = 0;
SNAKE.n = 1;//蛇的初始节数
SNAKE.ch = right;
rectangle(0, 0, 10, 10);//试用小矩形来初始化小蛇
setbkcolor(RED);
cleardevice();
}
//在界面上画蛇
void DrawSnake()
{
for (int i = SNAKE.n-1; i >= 0; i--)
{
rectangle(SNAKE.szb[i].x, SNAKE.szb[i].y,
SNAKE.szb[i].x + SNAKESIZE, SNAKE.szb[i].y + SNAKESIZE);
}
setcolor(RED);
rectangle(SNAKE.szb[SNAKE.n].x, SNAKE.szb[SNAKE.n].y,
SNAKE.szb[SNAKE.n].x + SNAKESIZE,
SNAKE.szb[SNAKE.n].y + SNAKESIZE);
setcolor(WHITE);
rectangle(SNAKE.szb[SNAKE.n-1].x, SNAKE.szb[SNAKE.n-1].y,
SNAKE.szb[SNAKE.n-1].x + SNAKESIZE,
SNAKE.szb[SNAKE.n-1].y + SNAKESIZE);
}
//移动
void MoveSnake()
{
for (int i = SNAKE.n; i > 0; i--)
{
SNAKE.szb[i].x = SNAKE.szb[i - 1].x;
SNAKE.szb[i].y = SNAKE.szb[i - 1].y;
}
switch (SNAKE.ch)
{
case up:
SNAKE.szb[0].y -= SNAKESIZE;
break;
case down:
SNAKE.szb[0].y += SNAKESIZE;
break;
case left:
SNAKE.szb[0].x -= SNAKESIZE;
break;
case right:
SNAKE.szb[0].x += SNAKESIZE;
break;
}
DrawSnake();
}
//让蛇移动 接收键盘方向键
void ChangeSnake()
{
int move;
move = getch();
switch (move)
{
case up:
if (SNAKE.ch != down)
{
SNAKE.ch = up;
}
break;
case down:
if (SNAKE.ch != up)
{
SNAKE.ch = down;
}
break;
case left:
if (SNAKE.ch != right)
{
SNAKE.ch = left;
}
break;
case right:
if (SNAKE.ch != left)
{
SNAKE.ch = right;
}
break;
}
}
//生成食物
void DrawFood()
{
setcolor(WHITE);
roundrect(FOOD.fzb.x, FOOD.fzb.y, FOOD.fzb.x + SNAKESIZE,
FOOD.fzb.y + SNAKESIZE, 10, 10);
}
void CoorFood()
{
srand(unsigned(time(NULL)));
FOOD.fzb.x = rand() % (640 / SNAKESIZE)*SNAKESIZE;
FOOD.fzb.y = rand() % (480 / SNAKESIZE)*SNAKESIZE;
FOOD.flag = 1;
}
int EatFood()
{
if (SNAKE.szb[0].x == FOOD.fzb.x&&SNAKE.szb[0].y == FOOD.fzb.y)
{
SNAKE.n++;
setcolor(RED);
roundrect(FOOD.fzb.x, FOOD.fzb.y, FOOD.fzb.x + SNAKESIZE,
FOOD.fzb.y + SNAKESIZE, 10, 10);
setcolor(WHITE);
FOOD.flag = 0;
return FOOD.flag;
}
return 1;
}
//判断是否撞墙或自己
void BreakSnake()
{
if (SNAKE.szb[0].x < 0 || SNAKE.szb[0].x>640 ||
SNAKE.szb[0].y < 0 || SNAKE.szb[0].y>470)//是否超出范围
{
OverSnake();
}
for (int i = SNAKE.n-2; i < SNAKE.n - 2; i--)
{
if (SNAKE.szb[0].x == SNAKE.szb[i].x&&
SNAKE.szb[0].y == SNAKE.szb[i].y)
{
OverSnake();
}
}
}
void OverSnake()
{
cleardevice();
setcolor(YELLOW);
settextstyle(50, 0, L"华文彩云");
outtextxy(200, 150, L"you loss!");
outtextxy(180, 200, L"即将退出游戏-_-");
Sleep(2000);
closegraph();
exit(0);
}
想玩玩的话就去下载吧:http://pan.baidu.com/s/1miuIwvI