怎么用C语言实现黄金矿工游戏
短信预约 -IT技能 免费直播动态提醒
这篇文章主要介绍“怎么用C语言实现黄金矿工游戏”,在日常操作中,相信很多人在怎么用C语言实现黄金矿工游戏问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用C语言实现黄金矿工游戏”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
首先我们先创建一个头文件,把一些结构体以及枚举类型的函数放进去,这样会让整个项目看起来更加有序,更好理解,先把枚举类型放进去
enum ATTR{//图片对应的数组下标i_gold=1,i_money=3,i_role=5,i_stone=9,i_bk= i_stone+1,//窗口尺寸WIDTH = 1080,HEIGHT= 640,//物品数量MINE_NUM=10,}; enum TYPE{//物品类型GOLD,//金块MONEY,//钱袋STONE,//石头//摆动方向LEFT,RIGHT,//摆动状态M_LONG,M_NORMAL,M_SHORT, };
之后把我们的老朋友结构体也放进去
struct Role{int x;//贴图的位置int y;int width;//图片宽度和高度int height;int coin;//金币};struct Mine //物品{int x;int y;int size;//用来计算碰撞int flag;//物品是否存在int type;//物品类型,钱袋,石头,金块int gold;//价值};//钩子struct Hook{double x;//绳子开始坐标,固定不变的double y;double endx;//末端变化的坐标double endy;int len;//绳子长度int dir;//摆动方向double angle;//摆动角度double speed;//速度double vx;//速度分量double vy;int swing;//是否在摆动int state;//伸长状态,伸长,正常,缩短int index;//抓到的物品下标};
OK,接下来就是我们的主要函数main.Cpp了,记得开始的时候加上我们写的头文件,先写初始化函数
void GameInit(){//初始化随机数种子srand(GetTickCount());//初始化角色数据role.coin = 0;role.width = 140;role.height = 120;role.x = WIDTH / 2 - role.width / 2;//让角色图片居中显示role.y = 0;//加载图片for (int i = 0; i < 10; i++){char fileName[20];sprintf(fileName, "./images/%d.jpg", i);if (i <= 1){loadimage(&img[i], fileName,73,62);}else{loadimage(&img[i], fileName);}}loadimage(&img[i_bk], "./images/bk.jpg",WIDTH,HEIGHT-role.height);//初始化物品for (int i = 0; i < MINE_NUM; i++){mine[i].flag = 1;mine[i].size = 60;mine[i].type = rand() % 3;mine[i].x=rand()%(WIDTH-mine[i].size);mine[i].y=rand()%(HEIGHT-role.height-100)+ role.height+ 50;mine[i].gold = rand()%600+rand()%200;}//初始化钩子hook.x = role.x+45;hook.y = role.y+100;hook.len = 50;hook.endx = hook.x;hook.endy=hook.y+hook.len;hook.angle = 0.0;hook.dir = RIGHT;hook.state = M_NORMAL;hook.vx = 0;hook.vy = 0;hook.speed = 5.0;hook.index = -1;}
再写我们的绘制函数,这个比较简单,就是贴图
void Gamedraw(){BeginBatchDraw();//设置背景颜色setbkcolor(GREEN);cleardevice();putimage(0, role.height, &img[i_bk]);//透明贴图 两张图片,一张掩码图,一张原图putimage(role.x, role.y, &img[i_role-1],class="lazy" data-srcAND);//掩码图putimage(role.x, role.y, &img[i_role],class="lazy" data-srcPAINT);//原图//绘制钩子setlinestyle(PS_SOLID, 5);setlinecolor(BROWN);line(hook.x, hook.y, hook.endx, hook.endy);//绘制物品for (int i = 0; i < MINE_NUM; i++){if (mine[i].flag){switch (mine[i].type){case GOLD:putimage(mine[i].x, mine[i].y, &img[i_gold-1],class="lazy" data-srcAND);putimage(mine[i].x, mine[i].y, &img[i_gold],class="lazy" data-srcPAINT);break;case MONEY:putimage(mine[i].x, mine[i].y, &img[i_money-1], class="lazy" data-srcAND);putimage(mine[i].x, mine[i].y, &img[i_money], class="lazy" data-srcPAINT);break;case STONE:putimage(mine[i].x, mine[i].y, &img[i_stone-1], class="lazy" data-srcAND);putimage(mine[i].x, mine[i].y, &img[i_stone], class="lazy" data-srcPAINT);break;}}}//绘制分数char s[30];sprintf(s, "金币:%d", role.coin);settextstyle(50, 0, "黑体");outtextxy(50, 50, s);EndBatchDraw();}
钩子摆动的函数,钩子该如何的摆,主要是让他不要往天上摆就行
//钩子摆动void hookRock(){if (hook.state == M_NORMAL){if (hook.dir == RIGHT){hook.angle++;}else{hook.angle--;}if (hook.angle > 80){hook.dir = LEFT;}else if (hook.angle < -80){hook.dir = RIGHT;}hook.endx = hook.x + sin(π / 180 * hook.angle) * hook.len;hook.endy = hook.y + cos(π / 180 * hook.angle) * hook.len;}}int distance(struct Hook hook){double dis=sqrt((hook.x-hook.endx)* (hook.x - hook.endx) + (hook.y-hook.endy) * (hook.y - hook.endy));return dis <= hook.len;}void keyControl(){//按空格伸长if (GetAsyncKeyState(VK_SPACE) && hook.state == M_NORMAL){hook.state = M_LONG;hook.vx = sin(π / 180 * hook.angle) * hook.speed;hook.vy = cos(π / 180 * hook.angle) * hook.speed;} if (hook.endx <= 0 || hook.endx >= WIDTH || hook.endy >= HEIGHT){hook.state = M_SHORT;}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.state = M_NORMAL;}}}
接下来是我们的抓取函数,也是比较简单
void grap(){//找到抓取的是哪个物品for (int i = 0; i < MINE_NUM; i++){if (mine[i].flag &&hook.endx > mine[i].x && hook.endx<mine[i].x + mine[i].size &&hook.endy>mine[i].y && hook.endy < mine[i].y + mine[i].size){hook.index = i;//保存抓到的物品的下标break;}}if (hook.index != -1){hook.state = M_SHORT;mine[hook.index].x = hook.endx-mine[hook.index].size/2;mine[hook.index].y = hook.endy- mine[hook.index].size / 2;if (distance(hook)){hook.state = M_NORMAL;mine[hook.index].flag = 0;role.coin += mine[hook.index].gold;hook.state = M_NORMAL;hook.index = -1;}}}
最后是我们的主函数
int main(){initgraph(WIDTH,HEIGHT,1);GameInit(); while (1){printf("%lf,%lf vxy(%lf,%lf)\n", hook.endx, hook.endy,hook.vx,hook.vy);hookRock();Gamedraw();keyControl(); grap();}closegraph();return 0;}
到此,关于“怎么用C语言实现黄金矿工游戏”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341