项目预习:
安装Visual Studio 2013;
VS2013安装教程和使用_哔哩哔哩_bilibili
实现代码:
#include <graphics.h> #include<Windows.h> #include<conio.h>? #include<time.h>? #include<stdio.h> #include<easyx.h> #pragma warning(disable:4996) #pragma comment(lib, "Winmm.lib") //音效头文件
#define WINDOW_LENGTH 640 //窗口长 #define WINDOW_WIDE 480 //串口宽 #define M_TOP 50 //地图左上顶点y坐标 #define M_LEFT 20 //地图左上顶点x坐标 #define M_BOTTOM WINDOW_WIDE-30 //地图右下顶点y坐标 #define M_RIGHT WINDOW_LENGTH-30 //地图右下顶点x坐标 #define SNAKE_UP 72 //蛇向上运动 #define SNAKE_DOWN 80 //蛇向下运动 #define SNAKE_LEFT 75 //蛇向左运动 #define SNAKE_RIGHT 77 ?//蛇向右运动 #define SNAKE_QUIT 27 //结束游戏ESC键 #define SNAKE_SPEED 500 //蛇运动速度 #define SNAKE_PAUSS 32 //蛇暂停运动 void backgroundmusic(); //背景音乐
typedef struct FOOD{ //定义食物结构体 ?? ?int x; ?? ?int y; ?? ?int FOOD_LIVE; }FOOD;
typedef struct NODE{ //定义蛇的节点 ?? ?int x; ?? ?int y; ?? ?NODE *PRE; ?? ?NODE *NEXT; }NODE;
typedef struct SANKE{ //定义一个蛇 ?? ?NODE *TAIL; ?? ?NODE *HEAD; ?? ?int NODES; //蛇的节数 ?? ?int DIRECTION; //蛇的运动方向 ?? ?int SANKE_LIVE; //存活状态 }SANKE;
int score_ = 0; FOOD f; SANKE s; int rank; int t = 1;
int biteSelf(); // 判断是否咬到了自己 int snake_life(); //蛇是否存活 void gameover(); //结束界面 int eatfood(); //吃食物 void score(); //分数 void time(); //时间 void meau(); //菜单 void initGAME(); //初始化游戏 void creatWINDOW(int x, int y); //创建窗口 void closeWINDOW(); //关闭窗口 void creatmap(); //创建地图 void creatfood(); //创建食物 void drawfood(); //画食物 void drawSANKE(); //画蛇 void initSANKE(); //创建蛇 void moveSANKE(); //移动蛇 void speed(); //调整速度 void playgame(); //开始游戏 int direction(int c); //判断操作方向是否与蛇的方向相同 void Data(); //保存分数 void ESC(); //esc结束 void musicforgg(); //结束时的音乐 void backgroundmusic(); //背景音乐
int main() { ?? ?while (t) ?? ?{ ?? ??? ?backgroundmusic(); ?? ??? ?meau(); ?? ??? ?initGAME(); ?? ??? ?playgame(); ?? ??? ?closeWINDOW(); ?? ?} }
void initGAME() { ?? ?creatWINDOW(WINDOW_LENGTH, WINDOW_WIDE); ?? ?creatmap(); ?? ?creatfood(); ?? ?initSANKE(); ?? ?score(); ?? ?f.FOOD_LIVE = 0; }
void creatWINDOW(int x, int y){ ?? ?initgraph(WINDOW_LENGTH, WINDOW_WIDE); ?? ?setbkcolor(RGB(166, 135, 100)); ?? ?cleardevice(); }
void closeWINDOW(){ ?? ?Data(); ?? ?_getch(); ?? ?closegraph(); }
void creatmap(){ ?? ?int i; ?? ?setlinecolor(YELLOW); ?? ?rectangle(M_LEFT, M_TOP, M_RIGHT, M_BOTTOM); ?? ?rectangle(M_LEFT - 10, M_TOP - 10, M_RIGHT + 10, M_BOTTOM + 10); ?? ?for (i = M_LEFT; i <= M_RIGHT; i = i + 10) ?? ??? ?line(i, M_TOP, i, M_TOP - 10); ?? ?for (i = M_TOP; i <= M_BOTTOM; i = i + 10) ?? ??? ?line(M_LEFT, i, M_LEFT - 10, i); ?? ?for (i = M_LEFT; i <= M_RIGHT; i = i + 10) ?? ??? ?line(i, M_BOTTOM, i, M_BOTTOM + 10); ?? ?for (i = M_TOP; i <= M_BOTTOM; i = i + 10) ?? ??? ?line(M_RIGHT, i, M_RIGHT + 10, i); }
void creatfood(){ ?? ?srand(time(0)); ?? ?int x, y; ?? ?while (1){ ?? ??? ?x = rand() % (600 - 20 + 1) + 20; ?? ??? ?y = rand() % (440 - 50 + 1) + 50; ?? ??? ?if (x % 10 == 0 && y % 10 == 0){ ?? ??? ??? ?f.x = x; ?? ??? ??? ?f.y = y; ?? ??? ??? ?break; ?? ??? ?} ?? ?} ?? ?f.FOOD_LIVE = 1; }
void drawfood(){ ?? ?setfillcolor(YELLOW); ?? ?fillrectangle(f.x, f.y, f.x + 9, f.y + 9); }
void initSANKE(){ ?? ?s.HEAD = (NODE *)malloc(sizeof(NODE)); ?? ?s.TAIL = (NODE *)malloc(sizeof(NODE)); ?? ?s.HEAD->x = 30; ?? ?s.HEAD->y = 400; ?? ?s.TAIL->x = 30; ?? ?s.TAIL->y = 410; ?? ?s.HEAD->NEXT = s.TAIL; ?? ?s.HEAD->PRE = NULL; ?? ?s.TAIL->NEXT = NULL; ?? ?s.TAIL->PRE = s.HEAD; ?? ?s.DIRECTION = SNAKE_UP; ?? ?s.NODES = 2; ?? ?s.SANKE_LIVE = 1; }
void drawSANKE(){ ?? ?NODE *p; ?? ?p = s.HEAD; ?? ?while (p) ?? ?{ ?? ??? ?if (p == s.HEAD)//头部颜色蓝色; ?? ??? ??? ?setfillcolor(BLUE); ?? ??? ?else ?? ??? ??? ?setfillcolor(YELLOW);//蛇身为黄色; ?? ??? ?fillrectangle(p->x, p->y, p->x + 9, p->y + 9); ?? ??? ?p = p->NEXT; ?? ?} }
int direction(int c){ ?? ?//if ((c == SNAKE_DOWN&&s.DIRECTION == SNAKE_DOWN&&s.DIRECTION == SNAKE_UP) || (c == SNAKE_UP&&s.DIRECTION == SNAKE_UP&&s.DIRECTION == SNAKE_DOWN )||? ?? ?//(c == SNAKE_LEFT&&s.DIRECTION == SNAKE_LEFT&&s.DIRECTION == SNAKE_RIGHT )|| (c == SNAKE_RIGHT&&s.DIRECTION == SNAKE_RIGHT&&s.DIRECTION == SNAKE_LEFT)) ?? ?if (s.DIRECTION == SNAKE_DOWN&&c == SNAKE_UP) ?? ??? ?return 1; ?? ?else if (s.DIRECTION == SNAKE_UP&&c == SNAKE_DOWN) ?? ??? ?return 1; ?? ?else if (s.DIRECTION == SNAKE_LEFT&&c == SNAKE_RIGHT) ?? ??? ?return 1; ?? ?else if (s.DIRECTION == SNAKE_RIGHT&&c == SNAKE_LEFT) ?? ??? ?return 1; ?? ?else ?? ??? ?return 0; }
void playgame(){ ?? ?int c; ?? ?while (true){ ?? ??? ?if (f.FOOD_LIVE == 1)//若食物存在,则画出食物 ?? ??? ?{ ?? ??? ??? ?drawfood(); ?? ??? ?} ?? ??? ?if (f.FOOD_LIVE == 0)//若食物不存在,则生成食物 ?? ??? ?{ ?? ??? ??? ?creatfood(); ?? ??? ??? ?drawfood(); ?? ??? ?} ?? ??? ?if (snake_life() == 0)//若蛇死亡,游戏结束 ?? ??? ?{ ?? ??? ??? ?gameover(); ?? ??? ??? ?closeWINDOW(); ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?while (!kbhit())//当没有按键的时候 ?? ??? ?{ ?? ??? ??? ?time(); ?? ??? ??? ?if (eatfood() == 1) ?? ??? ??? ?{ ?? ??? ??? ??? ?creatfood(); ?? ??? ??? ??? ?drawfood(); ?? ??? ??? ?} ?? ??? ??? ?if (snake_life() == 0)//若蛇死亡,游戏结束 ?? ??? ??? ?{ ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?moveSANKE(); ?? ??? ??? ?drawSANKE(); ?? ??? ??? ?if (tt) ?? ??? ??? ??? ?upgrade(); ?? ??? ??? ?speed(); ?? ??? ?} ?? ??? ?while (kbhit())//当有按键的时候 ?? ??? ?{ ?? ??? ??? ?c = getch(); ?? ??? ??? ?if (direction(c) == 0)//不是相反的方向 ?? ??? ??? ?{ ?? ??? ??? ??? ?switch (c) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ?case SNAKE_LEFT: s.DIRECTION = SNAKE_LEFT; break; ?? ??? ??? ??? ?case SNAKE_RIGHT: s.DIRECTION = SNAKE_RIGHT; break; ?? ??? ??? ??? ?case SNAKE_UP: s.DIRECTION = SNAKE_UP; break; ?? ??? ??? ??? ?case SNAKE_DOWN: s.DIRECTION = SNAKE_DOWN; break; ?? ??? ??? ??? ?case SNAKE_PAUSS:system("pause"); break; ?? ??? ??? ??? ?case SNAKE_QUIT:ESC(); ?? ??? ??? ??? ?default:break; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ?} }
void ESC(){ ?? ?gameover(); ?? ?getch(); ?? ?exit(0); }
void moveSANKE(){ ?? ?NODE *p; ?? ?p = (NODE *)malloc(sizeof(NODE)); ?? ?clearrectangle(s.TAIL->x, s.TAIL->y, s.TAIL->x + 9, s.TAIL->y + 9); ?? ?creatmap();
?? ?p = s.TAIL;
?? ?while (p->PRE != NULL) ?? ?{ ?? ??? ?p->x = p->PRE->x; ?? ??? ?p->y = p->PRE->y; ?? ??? ?p = p->PRE; ?? ?}
?? ?switch (s.DIRECTION){ ?? ?case SNAKE_UP:s.HEAD->y -= 10; break; ?? ?case SNAKE_LEFT:s.HEAD->x -= 10; break; ?? ?case SNAKE_DOWN:s.HEAD->y += 10; break; ?? ?case SNAKE_RIGHT:s.HEAD->x += 10; break; ?? ?default:break; ?? ?} }
void speed(){ ?? ?Sleep(rank); }
int snake_life(){ ?? ?int x, y; ?? ?x = s.HEAD->x; ?? ?y = s.HEAD->y; ?? ?if (x <20 || x > 600 || y > 440 || y < 50) ?? ??? ?s.SANKE_LIVE = 0; ?? ?if (biteSelf() == 1) ?? ??? ?s.SANKE_LIVE = 0; ?? ?return s.SANKE_LIVE; } //蛇是否存活
void gameover(){ ?? ?int c, x; ?? ?x = 1; ?? ?musicforgg(); ?? ?settextstyle(50, 0, _T("宋体")); ?? ?settextcolor(YELLOW); ?? ?char s1[] = "你死掉了。。。"; ?? ?outtextxy(150, 200, s1); ?? ?getch(); ?? ?initgraph(WINDOW_LENGTH, WINDOW_WIDE); ?? ?setbkcolor(BLACK); ?? ?cleardevice(); ?? ?settextcolor(YELLOW); ?? ?char s[] = ""; ?? ?sprintf(s, "%s%d", "您的分数是:", score_); ?? ?char s3[] = "1.重新挑战"; ?? ?char s2[] = "2.退出游戏"; ?? ?settextstyle(30, 0, _T("宋体")); ?? ?outtextxy(200, 200, s3); ?? ?outtextxy(200, 250, s2); ?? ?settextstyle(50, 0, _T("宋体")); ?? ?outtextxy(150, 100, s);
?? ?settextcolor(WHITE); ?? ?char s5[] = "按任意键继续。。。"; ?? ?char s7[] = "输入有误。。。"; ?? ?char s8[] = "谢谢使用!"; ?? ?while (x){ ?? ??? ?kbhit(); ?? ??? ?c = getch(); ?? ??? ?switch (c){ ?? ??? ?case '1':t = 1; x = 0; outtextxy(150, 350, s5); score_ = 0; break; ?? ??? ?case '2':outtextxy(150, 350, s8); getch(); exit(0); ?? ??? ?default:x = 1; outtextxy(150, 350, s7); break; ?? ??? ?} ?? ?} }//结束界面
int eatfood(){ ?? ?int x1 = s.HEAD->x; ?? ?int y1 = s.HEAD->y; ?? ?int x2 = f.x; ?? ?int y2 = f.y; ?? ?NODE *p; ?? ?if (x1 == x2&&y1 == y2) ?? ?{ ?? ??? ?s.TAIL->NEXT = (NODE*)malloc(sizeof(NODE));//扩大蛇的长度 ?? ??? ?s.TAIL->NEXT->x = s.TAIL->x; ?? ??? ?s.TAIL->NEXT->y = s.TAIL->y; ?? ??? ?s.TAIL->NEXT->PRE = s.TAIL; ?? ??? ?s.TAIL->NEXT->NEXT = NULL; ?? ??? ?p = s.TAIL->NEXT; ?? ??? ?s.TAIL = p;//尾部扩展 ?? ??? ?s.NODES++;//蛇的长度+1
?? ??? ?f.FOOD_LIVE = 0; ?? ??? ?s.NODES++; ?? ??? ?f.x = 0 - f.x; ?? ??? ?score_ += 10; ?? ??? ?score(); ?? ??? ?return 1; ?? ?} ?? ?else ?? ??? ?return 0; } //吃食物
void score(){ ?? ?char str[20]; ?? ?//settextcolor(BLUE);//颜色:灰色 ?? ?settextstyle(20, 0, _T("宋体"));//大小和字体 ?? ?sprintf(str, "得分:%d", score_);//生成字符串 ?? ?outtextxy(10, 20, str); } //分数
void time() { ?? ?char str[20]; ?? ?char c[] = "提示:空格键暂停游戏,ESC键结束游戏。"; ?? ?time_t t; ?? ?struct tm * lt; ?? ?settextstyle(20, 0, _T("宋体"));//大小和字体 ?? ?time(&t);//获取Unix时间戳。 ?? ?lt = localtime(&t);//转为时间结构。 ?? ?sprintf(str, "时间:%2d:%2d:%2d", lt->tm_hour, lt->tm_min, lt->tm_sec);//生成字符串 ?? ?outtextxy(500, 20, str); ?? ?outtextxy(100, 20, c); }//时间
void meau(){ ?? ?int c; ?? ?int x; ?? ?x = 1; ?? ?initgraph(WINDOW_LENGTH, WINDOW_WIDE); ?? ?setbkcolor(BLACK); ?? ?cleardevice(); ?? ?settextstyle(50, 0, _T("宋体")); ?? ?settextcolor(YELLOW); ?? ?char s[] = "欢迎来到贪吃蛇!"; ?? ?outtextxy(170, 100, s); ?? ?settextstyle(25, 0, _T("宋体")); ?? ?char s1[] = "选择难度:"; ?? ?outtextxy(250, 200, s1); ?? ?char s6[] = "制作者:蔡学斐,192210713111"; ?? ?outtextxy(200, 420, s6); ?? ?char s2[] = "1.简单模式"; ?? ?settextcolor(GREEN); ?? ?outtextxy(250, 240, s2); ?? ?char s3[] = "2.普通模式"; ?? ?settextcolor(BLUE); ?? ?outtextxy(250, 280, s3); ?? ?char s4[] = "3.困难模式"; ?? ?settextcolor(RED); ?? ?outtextxy(250, 320, s4); ?? ?char s7[] = "输入有误。。。";
?? ?settextcolor(WHITE); ?? ?char s5[] = "按任意键继续。。。"; ?? ?while (x) ?? ?{ ?? ??? ?kbhit(); ?? ??? ?c = getch(); ?? ??? ?switch (c) ?? ??? ?{ ?? ??? ?case '1':rank = 300; x = 0; tt = 0; ?? ??? ??? ?outtextxy(250, 390, s5); ?? ??? ??? ?break;
?? ??? ?case '2':rank = 200; x = 0; tt = 0; ?? ??? ??? ?outtextxy(250, 390, s5); ?? ??? ??? ?break;
?? ??? ?case '3':rank = 80; x = 0; tt = 0; ?? ??? ??? ?outtextxy(250, 390, s5); ?? ??? ??? ?break;
?? ??? ?default:x = 1; outtextxy(250, 390, s7); break; ?? ??? ?} ?? ?} ?? ?getch(); } //菜单
int biteSelf(){ ?? ?NODE *self; ?? ?self = s.HEAD->NEXT; ?? ?while (self != NULL) ?? ?{ ?? ??? ?if (self->x == s.HEAD->x&&self->y == s.HEAD->y) ?? ??? ?{ ?? ??? ??? ?return 1; ?? ??? ?} ?? ??? ?self = self->NEXT; ?? ?} ?? ?return 0; }
void Data() { ?? ?FILE *fp1 = fopen("text.txt", "w"); ?? ?FILE *fp2 = fopen("count.txt", "w"); ?? ?fprintf(fp1, "%d", score); ?? ?fprintf(fp2, "1"); ?? ?fclose(fp1); }
void musicforgg(){ ?? ?PlaySound("E:\\Bin\\vs项目\\贪吃蛇\\结束.wav", NULL, SND_FILENAME | SND_ASYNC); ?? ?//等待声音播放? }
void backgroundmusic(){ ?? ?PlaySound("E:\\岁月神偷 (Demo) - 金玟岐.mp3", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP); }
?
|