# "Gold miner" is a very classic game. In the game, players can get gold through continuous mining, and finally break into the next level. In this process, there will be rocks, smoke and mice to make trouble, and even small mice carrying explosive bags. Players must overcome them in order to enter deeper mines.

## Intense and exciting battle, relaxed and interesting playing methods, rich and diverse props, and exquisite and detailed pictures. This is the project "gold miner" we are going to complete today

Before that, I would like to explain to you that because this is a relatively large project, it will be difficult to show all the codes, including the participation of the database. Therefore, I cut the main part of the code to let you understand the difficulties and logical ideas of realizing this project. I hope you can understand

Well, don't say much. Let's start

## First, we create a header file and put some structures and functions of enumeration types into it. This will make the whole project look more orderly and better understood. First, put the enumeration types into it

```enum ATTR
{
//Array subscript corresponding to the picture
i_gold=1,
i_money=3,
i_role=5,
i_stone=9,
i_bk= i_stone+1,
//Window size
WIDTH = 1080,
HEIGHT= 640,
//Item quantity
MINE_NUM=10,
};
enum TYPE
{
//Item type
GOLD,	//Nuggets
MONEY,	//Purse
STONE,	//stone
//Swing direction
LEFT,
RIGHT,
//Swing state
M_LONG,
M_NORMAL,
M_SHORT,

};```

## Then put our old friend structure in

```struct Role
{
int x;		//Location of the map
int y;
int width;//Picture width and height
int height;
int coin;//Gold coins
};
struct Mine //goods
{
int x;
int y;
int size;//Used to calculate collisions
int flag;//Does the item exist
int type;//Item type, purse, stone, bullion
int gold;//value
};
//hook
struct Hook
{
double x;//The starting coordinates of the rope are fixed
double y;
double endx;//Coordinates of end change
double endy;
int len;//Rope length
int dir;//Swing direction
double angle;//Swing angle
double speed;//speed
double vx;//Velocity component
double vy;
int swing;//Is it swinging
int state;//Elongation state, elongation, normal, shortening
int index;//Captured item subscript
};```

## OK, the next step is our main function main.Cpp. Remember to add the header file we wrote at the beginning and write the initialization function first

```void GameInit()
{
//Initialize random number seed
srand(GetTickCount());
//Initialize role data
role.coin = 0;
role.width = 140;
role.height = 120;
role.x = WIDTH / 2 - role.width / 2;//Center the character picture
role.y = 0;
for (int i = 0; i < 10; i++)
{
char fileName[20];
sprintf(fileName, "./images/%d.jpg", i);
if (i <= 1)
{
}
else
{
}
}
//Initialize item
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;
}
//Initialize hook
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;
}```

## Then write our drawing function, which is relatively simple, that is, mapping

```void Gamedraw()
{
BeginBatchDraw();
//Set background color
setbkcolor(GREEN);
cleardevice();
putimage(0, role.height, &img[i_bk]);
//Transparent map two pictures, one mask image and one original image
putimage(role.x, role.y, &img[i_role],SRCPAINT);//Original drawing
//Draw hook
setlinestyle(PS_SOLID, 5);
setlinecolor(BROWN);
line(hook.x, hook.y, hook.endx, hook.endy);
//Draw items
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],SRCAND);
putimage(mine[i].x, mine[i].y, &img[i_gold],SRCPAINT);
break;
case MONEY:
putimage(mine[i].x, mine[i].y, &img[i_money-1], SRCAND);
putimage(mine[i].x, mine[i].y, &img[i_money], SRCPAINT);
break;
case STONE:
putimage(mine[i].x, mine[i].y, &img[i_stone-1], SRCAND);
putimage(mine[i].x, mine[i].y, &img[i_stone], SRCPAINT);
break;
}
}
}
//Draw score
char s[30];
sprintf(s, "Gold coins:%d", role.coin);
settextstyle(50, 0, "Blackbody");
outtextxy(50, 50, s);
EndBatchDraw();
}```

## The function of hook swing and how to swing the hook is mainly to make it not swing to the sky

```//Hook swing
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()
{
//Lengthen by space
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 it is shortened to the original length, it will stop shortening and judge whether the distance between the starting point and the end is equal to the length
if (distance(hook))
{
hook.state = M_NORMAL;
}
}
}```

## Next is our grab function, which is also relatively simple

```​
void grap()
{
//Find out which item you're grabbing
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;//Save the subscript of the caught item
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;
}
}
}

​```

## Finally, our main function

```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;
}```

## This is the main code of our "gold miner". Some people will ask. It doesn't seem to be much. It's really good from the code, but there are a lot of things involved in this project, including the database. Considering that some students can't understand it, I won't put it here. Interested students can go in to get the source code and study by themselves. Finally, what I want to say is, In fact, it's OK to write a game project. It's mainly to sort out the initialization function and update function. It's easy to understand it when packaged. Well, I hope you can get the knowledge and happiness you want here. I also hope you can pay attention to the UP master. Thank you very much!!!

## Enter the group to receive learning materials and project source material packagehttps://jq.qq.com/?_wv=1027&k=sttR3REF

Posted on Mon, 08 Nov 2021 01:52:42 -0500 by spartan789