这篇文章主要为大家详细介绍了C语言实现——《黄金矿工项目》,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下!

c语言简单实战项目(黄金矿工零基础项目)(1)

游戏介绍:

在规定的时间内得到规定的金钱时即可。玩法非常简单,按“↓”键开使捞金子,如有炸弹,按“↑”键放炸弹,买东西点鼠标左键。不过本游戏之设计到钩子勾金子的程度,后面的关卡大家可以自行设计,相信能够设计金子了,钻石的话同理还是非常简单的。

本项目编译环境:VS2019/VS2013;

插件:图形库插件easyX,涉及图片素材可以自行百度找也可以关注文末领取;

效果图展示

c语言简单实战项目(黄金矿工零基础项目)(2)

配套讲解教程:黄金矿工游戏教程——哔哩哔哩

源代码示例:

#include<stdio.h> #include<graphics.h> #include<math.h> #define WIDTH 1080 #define HEIGHT 640 #define PI 3.1415 #define MAX_ANGLE 80 enum Index { i_gold = 1, i_money = 3, i_roleDown = 5, i_roleUp = 7, i_stone = 9 }; /* 钩子摆动状态,摆动,伸长,缩短,停止 */ enum ATTR { LEFT, RIGHT, M_NORMAL, M_LONG, M_SHORT }; struct Role { int x; int y; int width; int height; int coin; //总金钱 }; struct Hook { int x; int y; int endx; int endy; int len;//钩子长度 double angle;//钩子当前角度 int dir;//钩子摆动方向,左,右 int vx;//速度变化量 int vy; int state;//当前状态 }; struct Role role; struct Hook hook; struct Mine//物品 { int x; int y; int size; int type; bool flag; int gold; }; struct Mine mine[10]; //把图片加载进程序 IMAGE img[11]; void loadImg() { for (int i = 0; i < 10; i ) { //如何把数字,转成字符串 char fileName[20] = ""; sprintf(fileName, "./images/%d.jpg", i); loadimage(img i, fileName); } loadimage(img 10,"./images/bk.jpg",WIDTH,HEIGHT); } void gameInit() { loadImg(); role.width = 140; role.height = 120; role.x = WIDTH / 2 - role.width / 2; role.y = 0; hook.len = 50; hook.x = role.x 40; hook.y = role.y 100; hook.endx = hook.x; hook.endy = hook.y hook.len; hook.angle = 0; hook.dir = RIGHT; hook.state = M_NORMAL; } void gameDraw() { setfillcolor(GREEN); solidrectangle(0, 0, WIDTH, role.height); putimage(0, role.height, img 10); putimage(role.x, role.y, img i_roleDown - 1, SRCAND); putimage(role.x, role.y, img i_roleDown, SRCPAINT); //输出我的钱 setbkmode(TRANSPARENT);//设置背景透明 settextstyle(50, 0, "黑体"); char coin[20] = ""; sprintf(coin, "金钱:%d", role.coin); outtextxy(20, 20, coin); //绘制钩子 setlinecolor(RED); setlinestyle(PS_SOLID, 5); line(hook.x, hook.y, hook.endx, hook.endy); setfillcolor(YELLOW); solidcircle(hook.endx, hook.endy, 5); } //钩子摆动 void hookRock() { if (hook.state == M_NORMAL) { if (hook.dir == RIGHT) { hook.angle ; } else if (hook.dir == LEFT) { hook.angle--; } if (hook.angle > MAX_ANGLE) { hook.dir = LEFT; } else if (hook.angle<-MAX_ANGLE) { hook.dir = RIGHT; } hook.endx = hook.x sin(PI / 180 * hook.angle)*hook.len; hook.endy = hook.y cos(PI / 180 * hook.angle)*hook.len; } } double distance(Hook h) { return sqrt((double)(hook.endx - hook.x)*(hook.endx - hook.x) (hook.endy - hook.y)*(hook.endy - hook.y)); } void keyControl(int speed) { if (GetAsyncKeyState(VK_SPACE))//virtual key { hook.state = M_LONG; hook.vx = sin(PI / 180 * hook.angle)*speed; hook.vy = cos(PI / 180 * hook.angle)*speed; } if (hook.state == M_LONG) { hook.endx = hook.vx; hook.endy = hook.vy; } else if (hook.state==M_SHORT) { hook.endx -= hook.vx; hook.endy -= hook.vy; if (distance(hook)<=hook.len) { hook.state =M_NORMAL; } } //碰到边界反回 if (hook.endx<0 || hook.endx>WIDTH || hook.endy<0 || hook.endy > HEIGHT) { hook.state = M_SHORT; } } int main() { //控制台窗口(只能用来输出文字) //我们需要能够画图的窗口 initgraph(WIDTH, HEIGHT, SHOWCONSOLE); gameInit(); while (1) { //双缓冲绘图,防止闪屏 BeginBatchDraw(); gameDraw(); EndBatchDraw(); hookRock(); keyControl(5); } return 0; }

写在最后:对于准备学习C/C 编程的小伙伴,如果你想更好的提升你的编程核心能力(内功)不妨从现在开始!

编程学习书籍分享:

c语言简单实战项目(黄金矿工零基础项目)(3)

编程学习视频分享:

c语言简单实战项目(黄金矿工零基础项目)(4)

整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)

欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!

对于C/C 感兴趣可以关注小编在后台私信我:【编程交流】一起来学习哦!可以领取一些C/C 的项目学习视频资料哦!已经设置好了关键词自动回复,自动领取就好了!

,