This is the 27th day of my participation in the August Genwen Challenge.More challenges in August

Easyx is a plugin library used to write small games, the official website is here, you can download: portal

EasyX Graphics Library is a free drawing Library for Visual C++, supporting VC6.0 ~ VC2019, simple and easy to use, learning cost is extremely low, a wide range of applications. At present, many universities have applied EasyX in teaching.

The official also has given the study method, oneself can enter the official to have a look.

Same old rule. Renderings first

This dozen aircraft small game materials are very general, direct online cutout down. But we should learn how to get started with this classic little game. Game logic is the most important thing to learn.

First of all, the game objects are these things

// Global artboard
IMAGE bk;
IMAGE BK;
IMAGE Plane;
IMAGE Diren;
IMAGE Zidan;
Copy the code

This game uses the game plugin easyX, we want to load these images.

// Preload the resource before using it
void loadRes(a)
{
	loadimage(&bk, _T("res\\bg.png"));

	loadimage(&BK, _T("res\\bg.png"));
	loadimage(&Plane, _T("res\\plane.png"));

	loadimage(&Diren, _T("res\\diren.png"));
	loadimage(&Zidan, _T("res\\zidan.png"));
}

Copy the code

Bullets and enemy structures are designed to control their position.

struct ZIDAN
{
	int x;
	int y;
};

struct DIREN
{
	int x;
	int y;
};

Copy the code

If the bullet and the enemy collide, you need to write collision detection, which is actually the distance between the two points, which is the classic collision algorithm.

bool isPeng(int x2,int y2,int x1,int y1)
{
	int result=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
	if(result<2500)
	{
		return true;
	}
	return false;
}
Copy the code

The enemy was destroyed when the bullet collided with them. All you need to do is move enemies off the screen, and since bullets and enemies are reusable, it’s best to create a pool of objects that you can reuse.

	// Determine whether the bullet collided with the plane
		for(i=0; i<8; i++) {for(int j=0; j<5; j++) {if(isPeng(zidans[j].x,zidans[j].y,direns[i].x+25,direns[i].y+15))
				{
					direns[i].y = - 100.; }}}Copy the code

Wsad is required to control the rocker for aircraft flight

		if (_kbhit()) 
		{
			char ch = _getch();
			if (ch == 'w') 
			{
				planeY-=5;
			}
			if(ch == 's')
			{
				planeY+=5;
			}
			if(ch == 'a')
			{
				planeX-=5;
			}
			if(ch == 'd')
			{
				planeX+=5; }}Copy the code

The above code makes the main logic of easyX aircraft war clear