[C language] simple game item: "don't step on white blocks"

Posted by jcornett on Wed, 08 Dec 2021 00:59:13 +0100

order

I suddenly like the word "startle Hong". Love at first sight is too superficial. Love over time is too pale. Others look at you secretly.

Hi! This is the fox~

The new week begins again. Time flies quickly. I don't know what to share with you. If you want to send partial algorithms, you're afraid that you don't understand, and if you want to send knowledge, you're afraid that you feel boring. Think about it, you'd better share some projects with you, but what projects to share are tangled. You need your wisdom here, You can tell me what you want to see by commenting. I will read every comment. I will focus on the items you want to see!!!

Well, let's get to the point. Today, let's share a simple little game - "don't step on white blocks". I don't know if you have played it. It was once popular. In fact, the game is very simple. Next, let me tell you how to simply realize this game project. (of course, I still do the low est version. You can recreate it. I believe you are better than me).

                                                          

 

Project source code

Header file

#include<stdio.h>
#include<graphics.h> 	// easyx graphics library needs to be installed
#include<time.h>
#include<mmsystem.h> 				// Header file: multimedia device interface
#pragma comment(lib,"winmm.lib") 	// Library file

#define INTERVAL 100 / / the first interval

 

Black block representation function

int score;
//How to express black fast?
int flag[4];
void initFlag()
{
	for (int i = 0; i < 4; i++)
	{
		flag[i] = rand() % 4;	//[0-4)
		//printf("%d ", flag[i]);
	}
}

  Draw function

void drawMap()
{
	settextstyle(25, 0, "Blackbody");
	settextcolor(RED);
	//Output text game ends
	outtextxy(150, 30, "Don't step on white pieces");

	char str[30] = { 0 };
	sprintf_s(str, "score:%d", score);
	outtextxy(280, 20, str);

	//Set line style
	setlinestyle(PS_SOLID, 2);
	setlinecolor(RGB(59, 59, 59));
	for (int i = 0; i < 5; i++)
	{
		line(0, i * 150 + INTERVAL, 400, i * 150 + INTERVAL);
		line(i * 100, INTERVAL, i * 100, 700);
	}
	//Draw black fast
	setfillcolor(BLACK);
	for (int i = 0; i < 4; i++)
	{
		int x = flag[i] * 100;
		int y = i * 150 + INTERVAL;
		fillrectangle(x, y, x + 100, y + 150);
	}
}

Mouse control function  

bool mouseEvent()
{
	//Get mouse message
	MOUSEMSG msg = GetMouseMsg();
	if (msg.uMsg == WM_LBUTTONDOWN)	//Left key press
	{
		//Get the coordinates of the lowest black fast (upper left corner)
		int x = flag[3] * 100;
		int y = 3 * 150 + INTERVAL;
		//Determine whether the mouse clicks the lowest black fast
		if (msg.x > x && msg.x<x + 100 && msg.y>y && msg.y < y + 150)
		{
			cleardevice();
			//Move as a whole, and move array elements backward
			for (int i = 3; i >0; i--)
			{
				flag[i] = flag[i - 1];
			}
			//Regenerate the first black fast
			flag[0] = rand() % 4;
			score += 10;
		}
		else
		{
			return false;
		}
	}
	return true;
}
//Press the keyboard to play the game
bool keyMouse()
{
	return false;
}

Game end function  

void gameOver()
{
	settextstyle(25, 0, "Blackbody");
	settextcolor(RED);
	//Output text game ends
	outtextxy(150, 30, "Game Over!");
	MessageBox(GetHWnd(), "Game Over!", "low B", MB_OK);
}

 

Main function

int main()
{
	//Play BGM 
	mciSendString("open Wild bees flying.mp3 alias BGM", NULL, 0, NULL);
	mciSendString("play BGM", NULL, 0, NULL);

	//Create a graphics window initgraph(int width, int height, int flag = NULL); 	//  Initialize the graphics environment
	initgraph(400, 700/*,EW_SHOWCONSOLE*/);	//Flag flag, whether to display the console window or close the button
	//Set background color
	setbkcolor(WHITE);
	cleardevice();
	//Set random number seed
	srand((unsigned)time(NULL));

	initFlag();
	while (1)
	{
		drawMap();
		if (!mouseEvent())
			break;
	}
	gameOver();

	getchar();
	closegraph();
	return 0;
}

summary

This project can be said to be very simple. It does not need any materials to run. It only needs an easyx graphics library. You can directly copy the code to run it. It should be no problem. By the way, you have asked a lot about easyx graphics library before, because many compilers do not support this graphics library, Here we still suggest that you use VS to realize this project. Of course, the optimization of this project depends on your wisdom. You can add music, interface and background. You can go online to find materials. Maybe you can directly enter the group to get the materials we have prepared.

Well, you must tell me what else you want to see in the comment area. In the future, I will release more project sources or learning materials. I hope you can continue to pay attention and reply to any questions. If you want C/C + + learning materials and the source code of other projects, you can add group [1083227756] to understand. Interested in the future development of programmers, you can focus on WeChat official account: Fox's encoding time, hope to learn progress with everyone!!!

Topics: C C++ Programming Game Development