图形库打造贪吃蛇,纯c语言,加上图形库函数,有音乐

一、工具

首先我们需要两个工具,我自己用的vc2017,然后需要图形库软件。EasyX,百度搜去官网就可以去找到。

二,源码

用到了循环,枚举,图形库函数,时间函数,结构体,如果你都懂那么都能看到了。

//贪吃蛇项目


#pragma once
#include
#include
#include //图形库头文件
#include //音乐播放头文件
#pragma comment(lib,"winmm.lib")
#include
#include

#define N 20 //图片的大小
#define MAXM 1000
void InitGaem();//初始化游戏
void SnakeDraw();//绘制蛇
void SnakeMobile();//蛇的移动
void SnakeDirection();//蛇的方向控制
void EatFood();//食物的坐标和蛇头
void FoodPaint();
void FoodCool();
void GameBreak();
void OverGame();
enum CH
{
up = 72,
    down = 80,
left = 75,
rigth =77
};
struct COOR//坐标定义
{
int x;
int y;
};
struct SNAME   //蛇的类型
{
int n;//蛇的节数
COOR scr[MAXM];//定义一个蛇节数的数组。
CH ch;
}snake;
struct FOOD
{
COOR fcr;//失误坐标
int flag;//表示有没有食物 1代表没有食物,没有食物需要生成食物
}food;
IMAGE foods, snakeup, snakedown, snakeleft, snakerigth, snakebody;
int main()
{
InitGaem();//创建窗口




while (1)//暂停
{
while (!_kbhit())
{
if (food.flag == 0)
FoodCool();
cleardevice();
FoodPaint();
SnakeMobile();//蛇的移动
SnakeDraw();//绘制蛇
Sleep(300);
EatFood();
GameBreak();
}
SnakeDirection();
}
return 0;
}


void InitGaem()//初始化游戏
{
initgraph(640, 480);
setbkcolor(WHITE);//定义颜色
cleardevice();//刷到背景去
//图片
loadimage(&foods, "西瓜.jpg", N, N);//图片的宽度和高度
loadimage(&snakeup, "蛇头上.jpg", N, N);//图片的宽度和高度
loadimage(&snakedown, "蛇头下.jpg", N, N);//图片的宽度和高度
loadimage(&snakeleft, "蛇头左.jpg", N, N);
loadimage(&snakerigth, "蛇头右.jpg", N, N);
loadimage(&snakebody, "蛇身.jpg", N, N);
snake.scr[0].x = 0;//蛇开始出现的坐标
snake.scr[0].y = 0;//蛇出现的坐标
snake.n = 1;//蛇开始的节数
snake.ch = down;//蛇开始行动的方向,这里一开始向下走
food.flag = 0;//初始化为没有食物
mciSendString("open 甩葱歌.mp3", 0, 0, 0);
mciSendString("play 甩葱歌.mp3", 0, 0, 0);
}
void SnakeDraw()//绘制蛇
{
//绘制图片
//蛇头
switch (snake.ch)
{
case up:
putimage(snake.scr[0].x,snake.scr[0].y,&snakeup);
break;
case down:
putimage(snake.scr[0].x, snake.scr[0].y, &snakedown);
break;
case left:
putimage(snake.scr[0].x, snake.scr[0].y, &snakeleft);
break;
case rigth:
putimage(snake.scr[0].x, snake.scr[0].y, &snakerigth);
break;
default:
break;
}
//蛇身
for (int i = snake.n - 1; i > 0; i--)
{
putimage(snake.scr[i].x, snake.scr[i].y, &snakebody);
}
}
//架构蛇移动代码
void  SnakeMobile()
{
for (int i = snake.n - 1; i > 0; i--)
{
snake.scr[i].x = snake.scr[i - 1].x;
snake.scr[i].y = snake.scr[i - 1].y;
}
switch (snake.ch)
{
case up:
snake.scr[0].y -= N;
break;
case down:
snake.scr[0].y += N;
break;
case left:
snake.scr[0].x -= N;
break;
case rigth:
snake.scr[0].x += N;
break;
default:
break;
}
}
//控制蛇的方向
void SnakeDirection()
{
switch (_getch())
{
case 72:
if(snake.ch!=down)
snake.ch = up;
break;
case 80:
if (snake.ch != up)
snake.ch = down;
break;
case 75:
if (snake.ch != rigth)
snake.ch = left;
break;
case 77:
if (snake.ch != left)
snake.ch = rigth;
break;


default:
break;
}
}


//吃食物
//食物的坐标 绘制食物


void FoodCool() 
{
srand((unsigned)time(NULL));
food.fcr.x = rand() % 32 * N;
food.fcr.y = rand() % 24 * N;


food.flag = 1;
}
void FoodPaint()
{
putimage(food.fcr.x, food.fcr.y, &foods);
}
//食物的坐标和蛇头坐标的重合
void EatFood()
{
if (snake.scr[0].x == food.fcr.x&&snake.scr[0].y == food.fcr.y)//食物的坐标和蛇头坐标的重合
{
//节数增加
snake.n++;
food.flag = 0;
}
}
//判断游戏退出
void GameBreak()
{
//撞墙 
if (snake.scr[0].y < -1 || snake.scr[0].y>471 || snake.scr[0].x < -1 || snake.scr[0].x>631)
{
//退出
OverGame();
}


for (int i = snake.n - 1; i > 0; i--)
{
if (snake.scr[0].x == snake.scr[i].x&&snake.scr[0].y == snake.scr[i].y)
{
//退出
OverGame();
}
}
}
void OverGame()
{
cleardevice();
settextcolor(GREEN);
settextstyle(50, 0, "黑体");
outtextxy(200, 200, "game over");
Sleep(2000);
exit(0);
}

你可能感兴趣的:(初入c语言)