失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 黄金矿工(小游戏)-----------C语言+easyx实现

黄金矿工(小游戏)-----------C语言+easyx实现

时间:2020-04-09 14:21:27

相关推荐

黄金矿工(小游戏)-----------C语言+easyx实现

啥也不说,上代码。

头文件:

#include <stdio.h>#include <easyx.h>#include "tools.hpp"//实现easyx透明贴图的 头文件#include <math.h>#include <stdlib.h>#include <time.h>#include <mmsystem.h>#pragma comment(lib,"winmm.lib")

钩子部分:

/*@钩子*///钩子struct Hook{double x;//钩子的起点坐标 不变double y;double len;//长度double endx;//结束点坐标double endy;double angle;//角度int swingDir;//摆动方向double dx;//钩子的速度 double dy;int state;//钩子的状态(伸长 缩短)int index;//抓取到的矿的下标}hook;enum Dir { Left, Right };enum State { Normal, Long, Short };//钩子初始化void hook_init(Hook* hook,int x, int y){hook->x = x;hook->y = y;hook->len = 80;hook->endx = hook->x;hook->endy = hook->len + hook->y;hook->angle = 0.0;hook->swingDir = Right;hook->dx = 0;hook->dy = 0;hook->state = 0;//钩子的状态是Normal正常hook->index = -1;}//钩子的绘制void hook_draw(Hook* hook){setlinecolor(BROWN);setlinestyle(PS_SOLID, 5);line(hook->x , hook->y, hook->endx, hook->endy);//画个钩钩setfillcolor(GREEN);solidcircle(hook->endx, hook->endy, 10);}//钩子摆动void hook_swing(Hook* hook, double angleStep){//正常条件下才会摆动if (hook->state == Normal){//限制摆动的角度if (hook->angle >= HOOK_SWING_MAX_ANGLE){hook->swingDir = Left;}else if(hook->angle <= -HOOK_SWING_MAX_ANGLE){hook->swingDir = Right;}if (hook->swingDir == Left){hook->angle -= angleStep;//printf("向左 angle = %f\n", hook->angle);}else if (hook->swingDir == Right){hook->angle += angleStep;//printf("向右 angle= %f\n", hook->angle);}//更新endx endyhook->endx = hook->x + sin(3.1415 / 180 * hook->angle) * hook->len;hook->endy = hook->y + cos(3.1415 / 180 * hook->angle) * hook->len;}}void hook_contorl(Hook* hook,int speed){//按空格键 钩子伸长if (GetAsyncKeyState(VK_SPACE) & 0x80000 && hook->state == Normal){hook->state = Long;//按键按下状态变为伸长 不再摆动hook-> dx = sin(3.14 / 180 * hook->angle)*speed;hook-> dy = cos(3.14 / 180 * hook->angle)* speed;}if (hook->state == Long){//钩子的变化就是endx 和endy的变化然后再重新绘制(在while(1)里面重绘了)hook->endx += hook->dx;hook->endy += hook->dy;}else if (hook->state == Short ){hook->endx -= hook->dx;hook->endy -= hook->dy;//缩短什么时候 恢复正常状态if (MANHATTAN_DIS(hook) <= hook->len && hook->index == -1)//麦哈顿公式{hook->state = Normal;}}//碰到边界缩回if (hook->endx <= 0 || hook->endx >= getwidth() || hook->endy >= getheight() || hook->endy <= 0){hook->state = Short;}}

角色:

/* 角色(老头)*///角色的函数 就是一个基本功能函数主要在 所有初始化和绘图的时候容易调用// /* @老人*/struct Role{int x;//坐标xint y;//坐标yint width;//老人即图片的宽度int height;//老人即图片的高度int coin;//总金币数}oldMan;//角色初始化void role_init(Role* role,int x,int y){role->x = x;role->y = y;role->width = imgs[3].getwidth();role->height = imgs[3].getheight();role->coin = 0;}//角色绘图void role_draw(Role* role){drawImg(role->x,role->y, &imgs[3]);//140是老头图片的宽度,居中显示 drawImg透明贴图//分数setbkmode(TRANSPARENT);settextstyle(48, 0, "宋体");settextcolor(BLACK);char score[50] = { 0 };sprintf(score, "金币数:%d", role->coin);outtextxy(20, (role->height - textheight(score))/2, score);}

初始化和绘制

//全体初始化void init(){//随机数种子srand(time(NULL));role_init(&oldMan, ((getwidth() - imgs[3].getwidth()) / 2), 0);hook_init(&hook, oldMan.x + 60, oldMan.y + 100);for (int i = 0; i < MINENUM; i++){int x = rand() % getwidth() + 50 ;int y = oldMan.height + rand() % (getheight() - oldMan.height);mine_init(&mines[i], x, y, rand() % 3);}}//全体绘制void draw(){//绘制背景图putimage(0, 120, &bk_img);//绘制老头所在位置的背景颜色setfillcolor(YELLOW);//设置填充颜色solidrectangle(0, 0, getwidth(), imgs[3].getheight());//在老头的一行绘制一个 矩形role_draw(&oldMan);//角色绘制//钩子hook_draw(&hook);//矿for (int i = 0; i < MINENUM; i++){mine_draw(&mines[i]);}}

矿+钱袋

//@矿的绘制和抓取#define MINENUM 10struct Mine{int x;int y;int width;int height;bool isDie;//是否被抓int gold;//金币数int type;//矿还是钱袋};enum MineType{Gold, Wallet, Stone};//0 1 2Mine mines[MINENUM];//初始化void mine_init(Mine* mine, int x, int y, int type){mine->x = x;mine->y = y;mine->type = type;mine->isDie = false;mine->gold = rand() % 100;mine->width = imgs[mine->type].getwidth();mine->height = imgs[mine->type].getheight();}//绘制void mine_draw(Mine* mine){if (!mine->isDie){drawImg(mine->x, mine->y, &imgs[mine->type]);}}//抓到后 矿跟着钩子走void hookGrapeMines( Hook* hook, Mine* mines){//正常摆动状态下if (hook->state == Normal){return;}for (int i = 0; i < MINENUM; i++){if (!mines[i].isDie&&hook->endx > mines[i].x && hook->endx < mines[i].x + mines[i].width &&hook->endy > mines[i].y && hook->endy <mines[i].y + mines[i].height){hook->index = i;break;//找到了就退出 遍历}}//抓到之后if (hook->index != -1){hook->state = Short;//跟着钩子跑mines[hook->index].y = hook->endy ;mines[hook->index].x = hook->endx ;//钩子最短时 矿消失if (MANHATTAN_DIS(hook) < hook->len){hook->state = Normal;mines[hook->index].isDie = true;//加价钱oldMan.coin += mines[hook->index].gold;hook->index = -1;//重置//printf("进来了\n");}}}

如果觉得《黄金矿工(小游戏)-----------C语言+easyx实现》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。