当前位置: 首页 > news >正文

C语言搭配EasyX实现贪吃蛇小游戏

封面展示

内部展示

完整代码

#define _CRT_SECURE_NO_WARNINGS
#include<easyx.h>
#include<stdio.h>
#include<mmsystem.h>
#pragma comment (lib,"winmm.lib")
#define width 40//宽有40个格子
#define height 30//长有40个格子
#define size 25//每个正方形格子的边长为20
int snack[width][height] = { 0 };//蛇
char direction='w';//初始方向向上
int foodx, foody;//食物刷新位置
int isover = 0;//游戏是否结束
int result = 0;//得分
int speed = 5;//速度
int maxscore;//历史最高分
char title1[] = "开始游戏",title2[] = "退出游戏",title3[] = "查看历史最高分",title4[] = "游戏帮助",title5[]="选择难度模式";//主页文字
int x = width * size / 2, y = height * size / 2;//提前用于文字居中
void getmaxscore();//获得历史最高分
void Remembermaxscore();//记住最高分
void menu();//菜单
void choose();//选择指令
void choosespeed();//选择蛇的移动速度
void Look_maxscore();//查看最高分,重新打印封面
void BGM();//这么刺激的游戏,没有点音乐可不行
void game();//游戏实现
void gamehelp();//游戏帮助
void gamemap();//绘制游戏地图
void Initialize();//绘制初始蛇以及食物
void draw();//每次蛇不同,重新绘制
void movespeed();//控制蛇的移动速度
void operate();//控制蛇的操作
void move();//蛇的运动
int main()
{getmaxscore();initgraph(width * size, height * size);//地图尺寸choose();return 0;
}
void getmaxscore()
{FILE* fp = fopen("历史最高分.txt", "r");fscanf(fp, "%d", &maxscore);fclose(fp);
}
void Remembermaxscore()
{if (result > maxscore){FILE* fp = fopen("历史最高分.txt", "w");fprintf(fp, "%d", result);fclose(fp);}}
void menu()
{IMAGE img;loadimage(&img, "./贪吃蛇封面.jpg", width * size, height * size);putimage(0, 0, &img);setfillcolor(YELLOW);setlinecolor(BLACK);setbkmode(TRANSPARENT);settextstyle(20, 0, "黑体");settextcolor(BLACK);fillrectangle(x - 100, y - 60, x + 100, y - 20);//打印四个按钮,按钮居中outtextxy(x - 100 + (200 - textwidth(title1))/2, y - 60 + (40 - textheight(title1))/2, title1);fillrectangle(x - 100, y - 20, x + 100, y + 20);outtextxy(x - 100 + (200 - textwidth(title2)) / 2, y - 20 + (40 - textheight(title2)) / 2, title2);fillrectangle(x - 100, y + 20, x + 100, y + 60);outtextxy(x - 100 + (200 - textwidth(title3)) / 2, y + 20 + (40 - textheight(title3)) / 2, title3);fillrectangle(x - 100, y + 60, x + 100, y + 100);outtextxy(x - 100 + (200 - textwidth(title4)) / 2, y + 60 + (40 - textheight(title4)) / 2, title4);fillrectangle(x - 100, y + 100, x + 100, y + 140);outtextxy(x - 100 + (200 - textwidth(title5)) / 2, y + 100 + (40 - textheight(title5)) / 2, title5);
}
void choose()
{menu();int f = 0;while (1){ExMessage msg;if (f) break;while (peekmessage(&msg, EX_MOUSE)){switch (msg.message){case WM_LBUTTONDOWN:if (msg.x > x - 100 && msg.x<x + 100 && msg.y>y - 60 && msg.y < y - 20){game();menu();}else if (msg.x > x - 100 && msg.x<x + 100 && msg.y>y - 20 && msg.y < y + 20)f = 1;else if (msg.x > x - 100 && msg.x<x + 100 && msg.y>y + 20 && msg.y < y + 60){Look_maxscore();menu();}else if (msg.x > x - 100 && msg.x<x + 100 && msg.y>y + 60 && msg.y < y + 100){gamehelp();menu();}else if (msg.x > x - 100 && msg.x<x + 100 && msg.y>y + 100 && msg.y < y + 140){choosespeed();menu();}break;default:break;}}}
}
void choosespeed()
{IMAGE img;loadimage(&img, "./01.jpg", width * size, height * size);putimage(0, 0, &img);char back[] = "<<===";settextstyle(30, 0, "宋体");setfillcolor(BROWN);fillrectangle(10, 10, 100, 50);outtextxy(10 + (90 - textwidth(back)) / 2, 10 + (40 - textheight(back)) / 2, back);setfillcolor(GREEN);fillrectangle(200, 305, 400, 445);outtextxy(200 + (200 - textwidth("老年人模式")) / 2, 305 + (140 - textheight("老年人模式")) / 2, "老年人模式");setfillcolor(YELLOW);fillrectangle(420, 305, 620, 445);outtextxy(420 + (200 - textwidth("儿童模式")) / 2, 305 + (140 - textheight("儿童模式")) / 2, "儿童模式");setfillcolor(RED);fillrectangle(640, 305, 840, 445);outtextxy(640 + (200 - textwidth("大学生模式")) / 2, 305 + (140 - textheight("大学生模式")) / 2, "大学生模式");int f = 0;while (1){ExMessage msg;while (peekmessage(&msg, EX_MOUSE)){switch (msg.message){case WM_LBUTTONDOWN:if (msg.x > 200 && msg.x < 400 && msg.y>305 && msg.y < 445){speed = 10;}else if (msg.x > 420 && msg.x < 620 && msg.y>305 && msg.y < 445){speed = 5;}else if (msg.x > 640 && msg.x < 840 && msg.y>305 && msg.y < 445){speed = 3;}else if (msg.x > 10 && msg.x < 100 && msg.y>10 && msg.y < 50){f = 1;}break;default:break;}}if (f) break;}
}
void Look_maxscore()
{IMAGE img;loadimage(&img, "./01.jpg", width * size, height * size);putimage(0, 0, &img);char score[100];char back[] = "<<===";settextstyle(30, 0, "宋体");sprintf_s(score, "历史最高分:%d", maxscore);outtextxy(width*size/2-120, height*size/2, score);setfillcolor(BROWN);fillrectangle(10, 10, 100, 50);outtextxy(10 + (90 - textwidth(back)) / 2, 10 + (40 - textheight(back)) / 2,back);int f = 0;while (1){ExMessage msg;if (f) break;while(peekmessage(&msg, EX_MOUSE)){switch (msg.message){case WM_LBUTTONDOWN:if (msg.x > 10 && msg.x < 100 && msg.y>10 && msg.y < 50)f = 1;break;default:break;}}}
}
void gamehelp()
{IMAGE img;loadimage(&img, "./01.jpg", width * size, height * size);putimage(0, 0, &img);char back[] = "<<===";settextstyle(30, 0, "宋体");setfillcolor(BROWN);fillrectangle(10, 10, 100, 50);outtextxy(10 + (90 - textwidth(back)) / 2, 10 + (40 - textheight(back)) / 2, back);outtextxy(50, 100, "帮助:");outtextxy(200, 200, "按键操作:");outtextxy(200, 250, "w:向上");outtextxy(200, 300, "s:向下");outtextxy(200, 350, "a:向左");outtextxy(200, 400, "d:向右"); outtextxy(200, 450, "尽量吃更多的食物获得更高的分数。");outtextxy(200, 500, "注意:蛇头不能撞上地图边缘和自己身体!");int f = 0;while (1){ExMessage msg;if (f) break;while (peekmessage(&msg, EX_MOUSE)){switch (msg.message){case WM_LBUTTONDOWN:if (msg.x > 10 && msg.x < 100 && msg.y>10 && msg.y < 50)f = 1;break;default:break;}}}
}
void BGM()
{mciSendString("open ./level.mp3 alias s1", 0, 0, 0);mciSendString("play s1 repeat", 0, 0, 0);
}
void game()
{gamemap();Initialize();BGM();while (1){draw();movespeed();operate();}
}
void gamemap()
{setbkmode(BLUE);cleardevice();BeginBatchDraw();setlinecolor(WHITE);//网格颜色for (int i = size; i < width * size; i += size)//打印网格line(i, 0, i, height * size);for (int i = size; i < height * size; i += size)line(0, i, width * size,i);
}
void Initialize()
{snack[width / 2][height / 2] = 1;//初始蛇头for (int i = 1; i <= 4; i++)snack[width / 2][height / 2 + i] = i + 1;//初始蛇身foodx = rand() % (width - 2) + 1;//随机刷出食物位置foody = rand() % (height - 2) + 1;
}
void draw()
{for (int i = 0; i < width; i++) {for (int j = 0; j < height; j++) {if (snack[i][j] != 0)setfillcolor(HSVtoRGB(snack[i][j] * 10, 0.9, 1));elsesetfillcolor(LIGHTGRAY);fillrectangle(i * size, j * size, (i + 1) * size, (j + 1) * size);}}setfillcolor(LIGHTMAGENTA);fillrectangle(foodx * size, foody * size, (foodx + 1) * size, (foody + 1) * size);if (isover){Remembermaxscore();char a[100];sprintf_s(a, "最后得分:%d", result);settextcolor(RED);settextstyle(40, 0, "宋体");setbkmode(TRANSPARENT);outtextxy(200, 150, "游戏结束");outtextxy(200, 200, a);}FlushBatchDraw();
}
void operate()
{if (GetAsyncKeyState('W')){if (direction == 's')direction = 's';else direction = 'w';}if (GetAsyncKeyState('S')){if (direction == 'w')direction = 'w';else direction = 's';}if (GetAsyncKeyState('A')){if (direction == 'd')direction = 'd';else direction = 'a';}if (GetAsyncKeyState('D')){if (direction == 'a')direction = 'a';else direction = 'd';}
}
void movespeed()
{if (isover) return;static int wait = 1;wait++;if (wait == speed){move();wait = 1;}
}
void move()
{for (int i = 0; i < width; i++)for (int j = 0; j < height; j++)if (snack[i][j] != 0) snack[i][j]++;int oldHeadX, oldHeadY, oldTailX, oldTailY;int tailsnacks = 0;for (int i = 0; i < width; i++) {for (int j = 0; j < height; j++) {if (tailsnacks < snack[i][j]) {tailsnacks = snack[i][j];oldTailX = i;oldTailY = j;}if (snack[i][j] == 2) {oldHeadX = i;oldHeadY = j;}}}int newHeadX = oldHeadX, newHeadY = oldHeadY;switch (direction) {case'a':newHeadX -= 1;break;case 's':newHeadY += 1;break;case 'd':newHeadX += 1;break;case 'w':newHeadY -= 1;break;}if (newHeadX >= width || newHeadX < 0 || newHeadY >= height || newHeadY < 0 || snack[newHeadX][newHeadY] != 0) {isover = 1;//判断是否失败return;}snack[newHeadX][newHeadY] = 1;if (newHeadX == foodx && newHeadY == foody) {result++;//重新刷新食物foodx = rand() % (width - 2) + 1;foody = rand() % (height - 2) + 1;}elsesnack[oldTailX][oldTailY] = 0;
}

音乐和背景图大家可以自定义添加

相关文章:

  • AJAX-认识URL
  • 通过Nacos权重配置,实现微服务金丝雀发布效果(不停机部署)
  • leetcode209长度最小的子数组|滑动窗口算法详细讲解学习
  • Docker 集群配置
  • 如何使用wireshark解析二进制文件
  • ubuntu+nginx+uwsgi部署django项目
  • 【misc | CTF】攻防世界 2017_Dating_in_Singapore
  • Compose | UI组件(十二) | Lazy Layout - 列表
  • 行为型设计模式—命令模式
  • 【issue-halcon例程学习】lines_gauss.hdev
  • 模拟请求ElasticSearch
  • python21-Python的字符串查找、替换相关方法
  • 实习记录——第十天
  • 低代码ERP系统助力企业成本优化,全面解析数字化转型之道!
  • 社区店加盟:如何选择适合的品牌和项目?
  • 【译】JS基础算法脚本:字符串结尾
  • 【Leetcode】104. 二叉树的最大深度
  • 【node学习】协程
  • bootstrap创建登录注册页面
  • ES2017异步函数现已正式可用
  • IDEA 插件开发入门教程
  • Java 实战开发之spring、logback配置及chrome开发神器(六)
  • Java 网络编程(2):UDP 的使用
  • JAVA之继承和多态
  • JS实现简单的MVC模式开发小游戏
  • Meteor的表单提交:Form
  • seaborn 安装成功 + ImportError: DLL load failed: 找不到指定的模块 问题解决
  • ViewService——一种保证客户端与服务端同步的方法
  • 机器学习学习笔记一
  • 微信小程序--------语音识别(前端自己也能玩)
  • 学习JavaScript数据结构与算法 — 树
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • 一些关于Rust在2019年的思考
  • 源码之下无秘密 ── 做最好的 Netty 源码分析教程
  • 扩展资源服务器解决oauth2 性能瓶颈
  • ​LeetCode解法汇总307. 区域和检索 - 数组可修改
  • ###STL(标准模板库)
  • #HarmonyOS:软件安装window和mac预览Hello World
  • #QT(一种朴素的计算器实现方法)
  • #常见电池型号介绍 常见电池尺寸是多少【详解】
  • (5)STL算法之复制
  • (Redis使用系列) Springboot 实现Redis 同数据源动态切换db 八
  • (分享)自己整理的一些简单awk实用语句
  • (论文阅读32/100)Flowing convnets for human pose estimation in videos
  • (循环依赖问题)学习spring的第九天
  • (一)WLAN定义和基本架构转
  • (一)使用IDEA创建Maven项目和Maven使用入门(配图详解)
  • (转)winform之ListView
  • (转)微软牛津计划介绍——屌爆了的自然数据处理解决方案(人脸/语音识别,计算机视觉与语言理解)...
  • ***php进行支付宝开发中return_url和notify_url的区别分析
  • **PHP二维数组遍历时同时赋值
  • .bat批处理(五):遍历指定目录下资源文件并更新
  • .equals()到底是什么意思?
  • .Net Framework 4.x 程序到底运行在哪个 CLR 版本之上
  • .net 打包工具_pyinstaller打包的exe太大?你需要站在巨人的肩膀上-VC++才是王道