/************** 程序名称:贪吃蛇游戏 *
* 编译环境:vs2008 *
* 作者相关:往事随风<1034882113> *
* 修改时间:2013-04-12 *
***************************************/
#include <graphics.h>#include <conio.h>
#include <time.h>#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <string.h>
// 引用 Windows Multimedia API
#pragma comment(lib,"Winmm.lib")
/***********宏定义区******************/
#define X500// 保存蛇节点坐标的个数
#define Y 500
int len = 640;
int wide = 480;
/***********结构定义区****************/
struct food{
int x;// 横坐标
int y;// 纵坐标
int flag;// 食物状态 1: 有食物; 0:没有食物
}food;
struct snake{
int x[X];// 蛇身横坐标
int y[Y];
int life;// 生命状态
int node;// 蛇的长度
char direction;// 蛇的方向
int score;// 分数
int grade;// 难度
int speed;// 速度}snake;
/*************函数声明区**************/
void initsnake();// 初始化一条蛇
void print_wall();// 输出围墙
void out_food();// 出现食物
void Gameover();// 结束游戏
void Gamebegin();// 开始游戏
void dead_case();// 死亡情况(撞墙 撞自身)
void eat_food();// 吃掉食物
void init_data();// 初始化数据
void get_direction(char ch);// 得到方向
void print_snake();// 输出蛇
void print_score();// 输出分数
void set_grade();// 设置等级
/************函数定义区***************/
void set_grade()
{
int grade = 0;
if (snake.speed > 30)
{
grade = snake.score / 100 + 1;
if (snake.grade != grade)
{
snake.grade = grade;// 最高只有7级
snake.speed -= 30;
}
}
}
void print_score()
{
char score[10] = "0";
char grade[3] = "0";
int x = 0, y1 = 0,y2 = 0;
x =( len /3 + len % 3) * 2;
y1 = wide / 5;
y2 = wide / 3;
set_grade();
itoa(snake.score,score,10);
itoa(snake.grade,grade,10);
setbkmode(TRANSPARENT);// 设置文字背景透明
setcolor(RGB(255,255,0));// 设置文字颜色
outtextxy(x + len / 10,y1,"分数:");
outtextxy(x + len / 6,y1,score);
outtextxy(x + len / 10,y2,"操作说明");
outtextxy(x + len / 10,y2 + 20,"a:向左走");
outtextxy(x + len / 10,y2 + 40,"d:向右走");
outtextxy(x + len / 10,y2 + 60,"w:向上走");
outtextxy(x + len / 10,y2 + 80,"s:向下走");
outtextxy(x + len / 10,y1 * 4,"等级:");
outtextxy(x + len / 10 + 40,y1 * 4,grade);
outtextxy(x + len / 10 + 50,y1 * 4,"级");
}
void print_snake()
{
int i;
setfillcolor(BLACK);// 设置当前填充色为黑色
/* 输出蛇 */
for (i = 0; i < snake.node; i++)
fillrectangle(snake.x[i],snake.y[i],snake.x[i] + 10,snake.y[i] + 10);
for (i = snake.node; i > 0; i--)// 蛇体坐标除了蛇头都读取前一位坐标
{
snake.x[i] = snake.x[i - 1];
snake.y[i] = snake.y[i - 1];
}
}
void get_direction(char ch)
{
/* 排除反方向 */
if (snake.direction != 'a' && ch == 'd')
snake.direction = 'd';
else if (snake.direction != 'd' && ch == 'a')
snake.direction = 'a';
else if (snake.direction != 'w' && ch == 's')
snake.direction = 's';
else if (snake.direction != 's' && ch == 'w')
snake.direction = 'w';
/* 蛇移动过程中的方向 */
switch (snake.direction)
{
case 'a': snake.x[0] -= 10; break;
case 'd': snake.x[0] += 10;break;
case 'w': snake.y[0] -= 10;break;
case 's': snake.y[0] += 10;break;
}
}
void init_data()
{
/***初始化***/
snake.score = 0;
snake.x[0] = 20;
snake.y[0] = 30;
snake.node = 1;
snake.life = 1;// 1表示活着
food.flag = 0;
snake.grade = 1;
snake.speed = 200;
}
void eat_food()
{
/* 吃掉食物 */
if (snake.x[0] == food.x && snake.y[0] == food.y)
{
//mciSendString("close run.mp3",NULL,0,NULL);// 关闭背景音乐
// 播放吃的音乐
mciSendString("play ate.mp3", NULL, 0, NULL);
setfillcolor(RGB(219,49,0));
fillrectangle(food.x,food.y,food.x + 10,food.y + 10); // 擦掉食物
snake.node++;// 蛇长加1
food.flag = 0;// 改变食物状态
snake.score += 10;// 改变分数
}
}
void dead_case()
{
int i = 0;
int x = 0;
x =( len /3 + len % 3) * 2;
/* 判断撞到自身 */
for (i = 2; i < snake.node; i++)
{
if (snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0])
{snake.life = 0;break;}
}
/* 判断是否撞墙 */
if (snake.x[0] > x || snake.x[0] < 10 || snake.y[0] < 10 || snake.y[0] > 470)
* 编译环境:vs2008 *
* 作者相关:往事随风<1034882113> *
* 修改时间:2013-04-12 *
***************************************/
#include <graphics.h>#include <conio.h>
#include <time.h>#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <string.h>
// 引用 Windows Multimedia API
#pragma comment(lib,"Winmm.lib")
/***********宏定义区******************/
#define X500// 保存蛇节点坐标的个数
#define Y 500
int len = 640;
int wide = 480;
/***********结构定义区****************/
struct food{
int x;// 横坐标
int y;// 纵坐标
int flag;// 食物状态 1: 有食物; 0:没有食物
}food;
struct snake{
int x[X];// 蛇身横坐标
int y[Y];
int life;// 生命状态
int node;// 蛇的长度
char direction;// 蛇的方向
int score;// 分数
int grade;// 难度
int speed;// 速度}snake;
/*************函数声明区**************/
void initsnake();// 初始化一条蛇
void print_wall();// 输出围墙
void out_food();// 出现食物
void Gameover();// 结束游戏
void Gamebegin();// 开始游戏
void dead_case();// 死亡情况(撞墙 撞自身)
void eat_food();// 吃掉食物
void init_data();// 初始化数据
void get_direction(char ch);// 得到方向
void print_snake();// 输出蛇
void print_score();// 输出分数
void set_grade();// 设置等级
/************函数定义区***************/
void set_grade()
{
int grade = 0;
if (snake.speed > 30)
{
grade = snake.score / 100 + 1;
if (snake.grade != grade)
{
snake.grade = grade;// 最高只有7级
snake.speed -= 30;
}
}
}
void print_score()
{
char score[10] = "0";
char grade[3] = "0";
int x = 0, y1 = 0,y2 = 0;
x =( len /3 + len % 3) * 2;
y1 = wide / 5;
y2 = wide / 3;
set_grade();
itoa(snake.score,score,10);
itoa(snake.grade,grade,10);
setbkmode(TRANSPARENT);// 设置文字背景透明
setcolor(RGB(255,255,0));// 设置文字颜色
outtextxy(x + len / 10,y1,"分数:");
outtextxy(x + len / 6,y1,score);
outtextxy(x + len / 10,y2,"操作说明");
outtextxy(x + len / 10,y2 + 20,"a:向左走");
outtextxy(x + len / 10,y2 + 40,"d:向右走");
outtextxy(x + len / 10,y2 + 60,"w:向上走");
outtextxy(x + len / 10,y2 + 80,"s:向下走");
outtextxy(x + len / 10,y1 * 4,"等级:");
outtextxy(x + len / 10 + 40,y1 * 4,grade);
outtextxy(x + len / 10 + 50,y1 * 4,"级");
}
void print_snake()
{
int i;
setfillcolor(BLACK);// 设置当前填充色为黑色
/* 输出蛇 */
for (i = 0; i < snake.node; i++)
fillrectangle(snake.x[i],snake.y[i],snake.x[i] + 10,snake.y[i] + 10);
for (i = snake.node; i > 0; i--)// 蛇体坐标除了蛇头都读取前一位坐标
{
snake.x[i] = snake.x[i - 1];
snake.y[i] = snake.y[i - 1];
}
}
void get_direction(char ch)
{
/* 排除反方向 */
if (snake.direction != 'a' && ch == 'd')
snake.direction = 'd';
else if (snake.direction != 'd' && ch == 'a')
snake.direction = 'a';
else if (snake.direction != 'w' && ch == 's')
snake.direction = 's';
else if (snake.direction != 's' && ch == 'w')
snake.direction = 'w';
/* 蛇移动过程中的方向 */
switch (snake.direction)
{
case 'a': snake.x[0] -= 10; break;
case 'd': snake.x[0] += 10;break;
case 'w': snake.y[0] -= 10;break;
case 's': snake.y[0] += 10;break;
}
}
void init_data()
{
/***初始化***/
snake.score = 0;
snake.x[0] = 20;
snake.y[0] = 30;
snake.node = 1;
snake.life = 1;// 1表示活着
food.flag = 0;
snake.grade = 1;
snake.speed = 200;
}
void eat_food()
{
/* 吃掉食物 */
if (snake.x[0] == food.x && snake.y[0] == food.y)
{
//mciSendString("close run.mp3",NULL,0,NULL);// 关闭背景音乐
// 播放吃的音乐
mciSendString("play ate.mp3", NULL, 0, NULL);
setfillcolor(RGB(219,49,0));
fillrectangle(food.x,food.y,food.x + 10,food.y + 10); // 擦掉食物
snake.node++;// 蛇长加1
food.flag = 0;// 改变食物状态
snake.score += 10;// 改变分数
}
}
void dead_case()
{
int i = 0;
int x = 0;
x =( len /3 + len % 3) * 2;
/* 判断撞到自身 */
for (i = 2; i < snake.node; i++)
{
if (snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0])
{snake.life = 0;break;}
}
/* 判断是否撞墙 */
if (snake.x[0] > x || snake.x[0] < 10 || snake.y[0] < 10 || snake.y[0] > 470)