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

黄金矿工(小游戏)-----------C语言+easyx实现_第1张图片

啥也不说,上代码。

黄金矿工(小游戏)-----------C语言+easyx实现_第2张图片

头文件:


#include 
#include 
#include "tools.hpp"		//实现easyx透明贴图的 头文件
#include 
#include 
#include 
#include 
#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   endy
			hook->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;			//坐标x
	int y;			//坐标y
	int 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 10
struct Mine
{
	int x;
	int y;
	int width;
	int height;
	bool isDie;	//是否被抓
	int gold;	//金币数
	int type;	//矿还是钱袋
};
enum MineType{Gold, Wallet, Stone};		//0 1 2
Mine 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 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,c语言,开发语言)