采用easyX插件进行C语言编写的贪吃蛇,自带播放音乐

/**********************************************************************************
*本程序采用VC++6.0编程,需要在easy X官网下载插件。

*程序作者:Booligan

*邮        箱:[email protected]

文件下载地址:https://download.csdn.net/download/qq_35146123/11220921

**********************************************************************************/


#include
#include
#include
#include
#include
#include
#pragma comment(lib,"Winmm.lib")


#define SIZEMAX 100
#define SNAKELEN 10
#define SPEED 150
#define NUM_R 10 //半径
#define NUM_X 25 //横向个数
#define NUM_Y 25 //纵向个数
#define NUM 30 //所需节点个数
//定义枚举来表示方向
typedef enum
{
up,down,left,right
}DIR;
//定义结构来存储坐标信息
typedef struct{
int x;
int y;
}COOR;
//定义结构来储存蛇的食物信息
struct FOOD 
{
COOR food_coor;
int flag;
}food;
//定义结构来储存蛇的信息
struct SNAKE
{
COOR snake_coor[SIZEMAX];
DIR dir;
int node;
}snake;


int stop_music=0;
int speed;   //设置贪吃蛇运动速度
void init();//对游戏进行初始化
void setfood();   //设置食物
void showfood();    //显示食物
void snakemove();   
void showsnake();
void changedir();  //改变蛇运行方向
void food_condition();
void die_condition();  //判断蛇死亡状态
void gameover();
void Quit();  
void NewGame();
void flush();


void main(void)
{
initgraph(640,480);
//输出提示信息
outtextxy(240, 240, _T("Press any key to start game!"));
_getch();
cleardevice();//清除屏幕信息
init();//界面初始化


while(1)
{
settextcolor(WHITE);
settextstyle(16,0,_T("宋体"));
setbkmode(TRANSPARENT);
outtextxy(0,270,"操作方法:");
    outtextxy(0, 300, _T("UP:W "));
outtextxy(0, 325, _T("DOWN:S "));
outtextxy(0, 350, _T("LEFT:A "));
outtextxy(0, 375, _T("RIGHT:D "));
outtextxy(260,400,"@Copyright by Booligan");
NewGame();
}
}


void init(void)
{
   for(int j=0;j<480;j++)
   {
  setlinecolor(LIGHTGRAY);
  line(0,j,640,j);
   }
   for(int i=48;i<378;i++)
   {
  setlinecolor(GREEN);
  line(148,i,498,i);
   }
   srand((unsigned)time(NULL));
   food.flag=0;
   snake.snake_coor[0].x=200+SNAKELEN;
   snake.snake_coor[0].y=60;
   snake.snake_coor[1].x=200;
   snake.snake_coor[1].y=60;
   snake.snake_coor[2].x=200;
   snake.snake_coor[2].y=60;
   snake.node=3;
   speed=SPEED;
   snake.dir=down;
   setcolor(BLUE);
   rectangle(148,48,498,378);
   rectangle(140,40,510,390);
   setcolor(WHITE);
}


void setfood(void)
{
  food.food_coor.x=rand()%(250/SNAKELEN)*SNAKELEN+240;
  food.food_coor.y=rand()%(300/SNAKELEN)*SNAKELEN+50;
  food.flag=1;
}


void showfood(void)
{
  setcolor(YELLOW);
  setfillcolor(YELLOW);
  fillcircle(food.food_coor.x+5,food.food_coor.y+5,5);
}


void snakemove(void)
{
  for(int i=snake.node;i>0;i--)
  {
 snake.snake_coor[i].x=snake.snake_coor[i-1].x;
 snake.snake_coor[i].y=snake.snake_coor[i-1].y;
  }
  switch(snake.dir)
  {
     case up:
snake.snake_coor[0].y-=SNAKELEN;
break;
case down:
snake.snake_coor[0].y+=SNAKELEN;
break;
case left:
snake.snake_coor[0].x-=SNAKELEN;
break;
case right:
snake.snake_coor[0].x+=SNAKELEN;
break;
  }
  showsnake();
}


void food_condition(void)
{
  if(snake.snake_coor[0].x==food.food_coor.x&&snake.snake_coor[0].y==food.food_coor.y)
  {
    snake.node++;
speed-=5;
setlinecolor(GREEN);
circle(food.food_coor.x+5,food.food_coor.y+5,5);
food.flag=0;
  }
}
void die_condition(void)
{
  if(snake.snake_coor[0].x>490||snake.snake_coor[0].x<150||snake.snake_coor[0].y<50||snake.snake_coor[0].y>370)
 gameover();
  for(int i=snake.node-1;i>0;i--)
  {
 if(snake.snake_coor[0].x==snake.snake_coor[i].x&&snake.snake_coor[0].y==snake.snake_coor[i].y)
 gameover();
  }
}


void showsnake(void)
{
  for(int i=snake.node-1;i>=0;i--)
  {
 circle(snake.snake_coor[i].x+5,snake.snake_coor[i].y+5,5);
  }
  setlinecolor(GREEN);
  setfillcolor(GREEN);
  fillcircle(snake.snake_coor[snake.node].x+5,snake.snake_coor[snake.node].y+5,5);
  setlinecolor(RED);
  setfillcolor(RED);
  fillcircle(snake.snake_coor[0].x+5,snake.snake_coor[0].y+5,5);
}


void changedir(void)
{
  char key;
  key=getch();
  switch(key)
  {
    case 'w':case 'W':
if(snake.dir!=down)
snake.dir=up;
break;
case 's':case 'S':
if(snake.dir!=up)
snake.dir=down;
break;
case 'a':case 'A':
if(snake.dir!=right)
snake.dir=left;
break;
case 'd':case 'D':
if(snake.dir!=left)
snake.dir=right;
break;
  }
}


void gameover(void)
{
   stop_music=1;
   if(stop_music) 
{
mciSendString("stop mymusic",NULL,0,NULL);
mciSendString("close mymusic",NULL,0,NULL);
}
   setcolor(RED);
   settextstyle(25,26,_T("宋体"));
   outtextxy(200,200,"Game Over!");
   HWND wnd = GetHWnd();
   if(MessageBox(wnd, "游戏结束。\n重来一局吗?","贪吃蛇",MB_YESNO|MB_ICONQUESTION)==IDYES)
main();
   else
Quit();
}
void Quit()
{
exit(0);
}
void NewGame()
{
// 播放音乐
mciSendString("open res\\123.mp3 alias mymusic", NULL, 0, NULL);
mciSendString("play mymusic", NULL, 0, NULL);
IMAGE img;
loadimage(&img, _T("res\\logo2.jpg"));
putimage(0, 100, &img);
    while(1)
{
while(!kbhit())
{
if(!food.flag) setfood();
showfood();
die_condition();
food_condition();
snakemove();
Sleep(speed);
}
changedir();
}
}

你可能感兴趣的:(采用easyX插件进行C语言编写的贪吃蛇,自带播放音乐)