写个贪吃蛇代码来玩玩~
上课看了贪吃蛇~然后自己动手做了个~程序代码:
/* 文件名称:main/cpp 文件描述:c语言+图形界面库实现人工智能版的贪吃蛇 编译环境:VC6.0 VS2008 VS 2010 VS2013 VS2015+图形界面库 <2017年3月17日> <创建工程> <制作人:九转星河> */ #include<stdio.h> #include<graphics.h> #include<conio.h> #include<time.h> #define WINDOW_WIDTH 640 //窗口的宽 #define WINDOW_HEIGHT 480 //窗口的高 #define WINDOW_BLOCK 100 //窗口的边界 #define MAXSNAKENUM 100 //蛇的最大节数 #define MAXFOODNUM 100 //食物的个数 #define SNAKESIZE 10 //蛇的像素 #define TIME 50 //用一个什么样的数据去表示这个元素 //自定义的结构体类型 当前有多少字节 蛇的方向 蛇的下标 typedef enum Ch { up=72, w='w', W='W', down=80, s='s', S='S', left=75, a='a', A='A', right=77, d='d', D='D', }; typedef struct Timer { char Print_hour[10]; //时 char Print_minute[10]; //分 char Print_second[10]; //秒 clock_t TIME_START; //设置游戏时间 clock_t TIME_NOW; //设置游戏时间 }Timer; typedef struct Coor { int x; int y; COLORREF color; }COOR; typedef struct Snake { COOR sCor[MAXSNAKENUM]; //当前的节数 Ch ch; //蛇的方向 int n; //节数的长度 char St_n[10]; //蛇身长度字符串 int score; //游戏得分 char St_score[10]; //用于转换字符串 }SNAKE; typedef struct Food { Coor fcoor[MAXFOODNUM]; COLORREF fcolor; int isEat; int Eatnum; //初始化被吃食物的编号 }Food; Timer timer={0}; //设计时间结构体 SNAKE snake={0}; //把蛇定义出来 Food food={0}; //初始化 void Draw_Main(); //绘图主函数 void GameInit(); void BLOCKDraw(); //边界绘制 void SnakeMove(); void SnakeStyle(); //蛇的状态 void SnakeDraw(); //蛇的绘制 void SnakeChange(); void SnakeBreak(); //判断是否撞墙 void FoodInit(); //初始化食物 void FoodCreat(); //创建食物 void FoodDraw(); //食物的绘制 void IsEatFood(); //判断食物有没有吃掉 void IsDelLength(); //判断蛇身的长度 void OuttextDraw(); //输出文本绘制 void GameOver(); //游戏结束 void GetTime(); //获取当前时间 void GetLength(); //获取当前蛇身长度 int main() { GameInit(); while (1) { while (!kbhit()) { cleardevice(); //清屏 SnakeStyle(); //更新蛇的状态量 SnakeMove(); //移动 IsEatFood(); //判断食物是否被吃 SnakeBreak(); //判断是否撞到墙 Sleep(TIME); } SnakeChange(); //改变蛇的方向 } getch(); closegraph(); return 0; } void GameInit() { srand((unsigned)time(NULL)); //获取随机种子 initgraph(WINDOW_WIDTH,WINDOW_HEIGHT); //初始化图形界面 setbkcolor(YELLOW); //背景颜色 setlinecolor(BLACK); //线条颜色 setfillcolor(LIGHTGRAY); //填充颜色 settextcolor(RED); //文字颜色 settextstyle(30,0,"微软雅黑"); //文字大小和风格 setbkmode(TRANSPARENT); //字体不背景透明处理 cleardevice(); FoodCreat(); snake.ch=down; snake.n=3; //节数 sprintf(snake.St_score,"%d",0); //初始化分数 snake.sCor[0].x=100; snake.sCor[0].y=100; snake.sCor[0].color=getfillcolor(); snake.sCor[1].x=100; snake.sCor[1].y=90; snake.sCor[1].color=getfillcolor(); snake.sCor[2].x=100; snake.sCor[2].y=80; snake.sCor[2].color=getfillcolor(); food.Eatnum=0; FoodInit(); SnakeDraw(); BLOCKDraw(); FoodDraw(); outtextxy(10,WINDOW_HEIGHT-WINDOW_BLOCK+3*SNAKESIZE, "简单说明:wsad上下左右移动,ESC退出游戏"); outtextxy(10,WINDOW_HEIGHT-WINDOW_BLOCK+6*SNAKESIZE, "连续吃掉三种相同颜色的食物可以减少蛇身长度"); outtextxy(300,WINDOW_HEIGHT-WINDOW_BLOCK+SNAKESIZE,"作者:九转星河"); outtextxy(10,WINDOW_HEIGHT-WINDOW_BLOCK+SNAKESIZE,"按任意键开始游戏"); getch(); OuttextDraw(); timer.TIME_START=clock();//游戏开始 } void FoodInit() //初始化食物 { COLORREF foodcolor[6]={BLUE,GREEN,CYAN,RED,MAGENTA,BROWN}; for (int i=0;i<MAXFOODNUM;++i) { food.fcoor[i].x=rand()%((WINDOW_WIDTH)/SNAKESIZE)*SNAKESIZE; food.fcoor[i].y=rand()%((WINDOW_HEIGHT-WINDOW_BLOCK)/SNAKESIZE)*SNAKESIZE; food.fcoor[i].color=foodcolor[rand()%6]; for (int j=0;j<snake.n;++j) if (snake.sCor[j].x==food.fcoor[i].x&&snake.sCor[j].y==food.fcoor[i].y) { food.fcoor[i].x=rand()%(WINDOW_WIDTH/SNAKESIZE)*SNAKESIZE; food.fcoor[i].y=rand()%(WINDOW_HEIGHT-WINDOW_BLOCK/SNAKESIZE)*SNAKESIZE; j=0; } for ( j=0;j<i;++j) if (food.fcoor[j].x==food.fcoor[i].x&&food.fcoor[j].y==food.fcoor[i].y) { food.fcoor[i].x=rand()%(WINDOW_WIDTH/SNAKESIZE)*SNAKESIZE; food.fcoor[i].y=rand()%(WINDOW_HEIGHT/SNAKESIZE)*SNAKESIZE; j=0; } } } void Draw_Main() { SnakeDraw(); BLOCKDraw(); FoodDraw(); OuttextDraw(); } void BLOCKDraw() { setfillcolor(LIGHTCYAN); fillrectangle(0,WINDOW_HEIGHT-WINDOW_BLOCK+SNAKESIZE,WINDOW_WIDTH,WINDOW_HEIGHT); } void SnakeStyle() { for (int i=snake.n-1;i>0;--i) { snake.sCor[i].x=snake.sCor[i-1].x; snake.sCor[i].y=snake.sCor[i-1].y; } if (food.isEat==0&&snake.n<MAXSNAKENUM-1) //如果食物被吃 { snake.sCor[snake.n].color=food.fcoor[food.Eatnum].color; //预先设定下一节的颜色 FoodCreat(); //创建食物 ++snake.n; snake.sCor[snake.n-1].x=snake.sCor[snake.n-2].x; snake.sCor[snake.n-1].y=snake.sCor[snake.n-2].y; snake.score+=10; //分数奖励 sprintf(snake.St_score,"%d",snake.score); //把分数转换成字符串形式保存 } IsDelLength(); } void SnakeMove() { switch (snake.ch) { case up: snake.sCor[0].y-=SNAKESIZE; break; case down: snake.sCor[0].y+=SNAKESIZE; break; case left: snake.sCor[0].x-=SNAKESIZE; break; case right: snake.sCor[0].x+=SNAKESIZE; break; default : break; } Draw_Main(); } void SnakeDraw() //蛇的绘制 { for (int i=0;i<snake.n;++i) { setfillcolor(snake.sCor[i].color); fillrectangle(snake.sCor[i].x,snake.sCor[i].y, snake.sCor[i].x+SNAKESIZE, snake.sCor[i].y+SNAKESIZE); } } void SnakeChange() { char move; move=getch(); switch(move) { case up: case W: case w: if (snake.ch!=down) snake.ch=up; break; case down: case S: case s: if (snake.ch!=up) snake.ch=down; break; case left: case A: case a: if (snake.ch!=right) snake.ch=left; break; case right: case D: case d: if (snake.ch!=left) snake.ch=right; break; case VK_ESCAPE: GameOver(); //游戏结束处理 break; default: break; } } void FoodCreat() //创建食物 { COLORREF foodcolor[6]={BLUE,GREEN,CYAN,RED,MAGENTA,BROWN}; //获取食物信息 food.fcoor[food.Eatnum].x=rand()%((WINDOW_WIDTH)/SNAKESIZE)*SNAKESIZE; food.fcoor[food.Eatnum].y=rand()%((WINDOW_HEIGHT-WINDOW_BLOCK)/SNAKESIZE)*SNAKESIZE; food.fcoor[food.Eatnum].color=foodcolor[rand()%6]; for (int i=0;i<snake.n;++i) { if (snake.sCor[i].x==food.fcoor[food.Eatnum].x&&snake.sCor[i].y==food.fcoor[food.Eatnum].y) { food.fcoor[food.Eatnum].x=rand()%(WINDOW_WIDTH/SNAKESIZE)*SNAKESIZE; food.fcoor[food.Eatnum].y=rand()%(WINDOW_HEIGHT/SNAKESIZE)*SNAKESIZE; i=0; } for (int j=0;j<MAXFOODNUM;++j) if (j!=food.Eatnum&&food.fcoor[j].x==food.fcoor[food.Eatnum].x&&food.fcoor[j].y==food.fcoor[food.Eatnum].y) { food.fcoor[food.Eatnum].x=rand()%(WINDOW_WIDTH/SNAKESIZE)*SNAKESIZE; food.fcoor[food.Eatnum].y=rand()%(WINDOW_HEIGHT/SNAKESIZE)*SNAKESIZE; j=0; } } food.isEat=1; } void FoodDraw() { for (int i=0;i<MAXFOODNUM;++i) //食物的绘制 { setfillcolor(food.fcoor[i].color); fillroundrect(food.fcoor[i].x,food.fcoor[i].y, food.fcoor[i].x+SNAKESIZE,food.fcoor[i].y+SNAKESIZE, SNAKESIZE,SNAKESIZE); } } void IsEatFood() { for (int i=0;i<MAXFOODNUM;++i) if (snake.sCor[0].x==food.fcoor[i].x&&snake.sCor[0].y==food.fcoor[i].y) { food.isEat=0; food.Eatnum=i; break; } } void IsDelLength() //如果遇到相同三种颜色则消去其长度 { COLORREF A=snake.sCor[snake.n-1].color; COLORREF B=snake.sCor[snake.n-2].color; COLORREF C=snake.sCor[snake.n-3].color; if (A==B&&B==C&&snake.n>3) { snake.n-=3; snake.score+=50; } } void SnakeBreak() { if (snake.n>=MAXSNAKENUM-1) GameOver(); if (snake.sCor[0].x>WINDOW_WIDTH||snake.sCor[0].y>WINDOW_HEIGHT-WINDOW_BLOCK) GameOver(); if (snake.sCor[0].x<0||snake.sCor[0].y<0) GameOver(); for (int i=snake.n-1;i>0;--i) if (snake.sCor[0].x==snake.sCor[i].x&&snake.sCor[0].y==snake.sCor[i].y) GameOver(); } void GetTime() //获取当前时间 { timer.TIME_NOW=clock(); double duration = (double)(timer.TIME_NOW-timer.TIME_START)/CLOCKS_PER_SEC; int my_time=(int )duration; int now_second=my_time%60; int now_minute=(my_time/60)%60; int now_hour=my_time/3600; sprintf(timer.Print_hour,"%02d",now_hour); sprintf(timer.Print_minute,"%02d",now_minute); sprintf(timer.Print_second,"%02d",now_second); } void GetLength() { sprintf(snake.St_n,"%d",snake.n); } void OuttextDraw() //输出文本绘制 { GetTime(); GetLength(); outtextxy(10,WINDOW_HEIGHT-WINDOW_BLOCK+SNAKESIZE,"当前游戏时间"); outtextxy(180,WINDOW_HEIGHT-WINDOW_BLOCK+SNAKESIZE,timer.Print_hour); outtextxy(205,WINDOW_HEIGHT-WINDOW_BLOCK+SNAKESIZE,":"); outtextxy(210,WINDOW_HEIGHT-WINDOW_BLOCK+SNAKESIZE,timer.Print_minute); outtextxy(235,WINDOW_HEIGHT-WINDOW_BLOCK+SNAKESIZE,":"); outtextxy(240,WINDOW_HEIGHT-WINDOW_BLOCK+SNAKESIZE,timer.Print_second); outtextxy(10,WINDOW_HEIGHT-WINDOW_BLOCK+3*SNAKESIZE,"当前分数:"); outtextxy(110,WINDOW_HEIGHT-WINDOW_BLOCK+3*SNAKESIZE,snake.St_score); outtextxy(300,WINDOW_HEIGHT-WINDOW_BLOCK+SNAKESIZE,"蛇身长度:"); outtextxy(410,WINDOW_HEIGHT-WINDOW_BLOCK+SNAKESIZE,snake.St_n); } void GameOver() { outtextxy(WINDOW_WIDTH/2-50,WINDOW_HEIGHT-WINDOW_BLOCK/2,"游戏结束"); getch(); closegraph(); exit(0); }