C language project practice: 2048 zero foundation project 208 lines of source code example

This article mainly introduces the implementation of C language in detail -- the example code in project 2048 is introduced in great detail, which has certain reference value. Interested partners can refer to it!

Introduction to the game:

2048 is a puzzle game. The rules of the game are very simple. It is a simple and easy-to-use digital game. The rules of the game are very simple. You need to control all the squares to move in the same direction. After two identical digital squares collide together, they are merged into their sum. After each operation, a 2 or 4 will be randomly generated. Finally, you will win if you get a "2048" square!

Simple and easy to play! Therefore, it has also become a very good small project for our programming learners' C language practice. If you haven't played it, you can play it first. If you can play it, it is also very helpful for the understanding of project logic! Next, let's have a look!

Plug in: the graphics library plug-in easyX, which involves picture materials, can be found by Baidu or collected at the end of the text;

Rendering display:

Supporting tutorial: 2048 game tutorial - Bili bilihttps://www.bilibili.com/video/BV1BC4y1H7jz?spm_id_from=333.999.0.0

Source code example:

#include<stdio.h>
#include<time.h>
#include<math.h>
#include<conio.h>
#Include < graphics. H > / / this header file is available only after installation
#define MAX_GRID 4 		// Number of cells per row
#define GRID_WIDTH 100 	// Lattice width
#define INTERVAL 15 		// interval
#define WIN_SIZE (5*INTERVAL+MAX_GRID*GRID_WIDTH) / / window size
enum Color //Enumerate grid colors
{
	zero = RGB(205, 193, 180), //Color of 0
	twoTo1 = RGB(238, 228, 218), //Color of 2
	twoTo2 = RGB(237, 224, 200), //Color of 4
	twoTo3 = RGB(242, 177, 121), //Color of 8
	twoTo4 = RGB(245, 149, 99), //Color of 16
	twoTo5 = RGB(246, 124, 95), //Color of 32
	twoTo6 = RGB(246, 94, 59), //64 color
	twoTo7 = RGB(242, 177, 121), //128 colors
	twoTo8 = RGB(237, 204, 97), //256 colors
	twoTo9 = RGB(255, 0, 128), //512 color
	twoTo10 = RGB(145, 0, 72), //1024 color
	twoTo11 = RGB(242, 17, 158), //2048 colors
	back = RGB(187, 173, 160), //background color 
};
Color arr[13] = { zero, twoTo1, twoTo2, twoTo3, twoTo4, twoTo5, twoTo6, twoTo7, twoTo8, twoTo9, twoTo10, twoTo11, back };
//Two dimensional array of data
int map[MAX_GRID][MAX_GRID];
int createNum()
{
	int num = 0;
	if (rand() % 10 == 0)
	{
		num = 4;
	}
	else
	{
		num = 2;
	}
	return num;
}
void gameInit()
{
	srand((unsigned)time(NULL));
	//Randomly generate two integers in a blank position
	for (int i = 0; i < 5; )
	{
		//Random number rand() randomly obtains an integer 0~ tens of thousands
		int r = rand() % MAX_GRID; //0~3
		int c = rand() % MAX_GRID; //0~3
		if (map[r][c] == 0)
		{
			map[r][c] = createNum();
			i++;
		}		
	}

}
void gameDraw()
{
	//Draw the interface, set the background color 187173160 three primary colors set background color
	setbkcolor(RGB(187, 173, 160));
	//After setting the color, you need to clear the screen (the original color is black, and the set color is covered)
	cleardevice();//Clear drawing equipment

	for (int i = 0; i < MAX_GRID; i++)//->y
	{
		for (int k = 0; k < MAX_GRID; k++)//->x
		{
			//00 01 02 03 
			//10 11 12 13
			//Find the x,y of each lattice
			int x = k*GRID_WIDTH + INTERVAL*(k + 1);
			int y = i*GRID_WIDTH + INTERVAL*(i + 1);
			//4->2
			//32->5
			//2^n=map[i][k];  ->log2map[i][k]=n;
			int index = (int)log2((float)map[i][k]);
			setfillcolor(arr[index]);
			//Draw rectangle: hollow rectangle, filled rectangle, filled rectangle, rounded rectangle
			//solidrectangle(x, y, x + GRID_WIDTH, y + GRID_WIDTH);
			solidroundrect(x, y, x + GRID_WIDTH, y + GRID_WIDTH, 15, 15);

			if (map[i][k] != 0)
			{
				//Draw numbers
				//printf("%d", map[i][k]);// Output to console window
				//Set text size
				settextstyle(50, 0, "Blackbody");
				settextcolor(RGB(163, 73, 164));
				setbkmode(TRANSPARENT);//Set the text background to transparent
				//Convert a number to a string format string
				char str[10] = "";
				sprintf(str, "%d", map[i][k]);
				//To center the text in the middle of the grid
				int textx = textwidth(str) / 2;
				int texty = textheight(str) / 2;
				int win = GRID_WIDTH / 2;		//Half the grid width
				outtextxy(x + win - textx, y + win - texty, str);
			}
		}
	}
}
void moveup()
{
	for (int i = 0; i < MAX_GRID; i++)
	{
		int temp = 0;
		for (int begin = 1; begin < MAX_GRID; begin++)
		{
			if (map[begin][0] != 0)
			{
				if (map[temp][i] == 0)
				{
					map[temp][i] = map[begin][i];
					map[begin][i] = 0;
				}
				else if (map[temp][i] == map[begin][i])
				{
					map[temp][i] += map[begin][i];
					map[begin][i] = 0;
					temp++;
				}
				else
				{
					map[temp+1][i] = map[begin][i];
					if (temp + 1 != begin)
					{
						map[begin][i] = 0;
					}
					temp++;
				}
			}
		}
	}
}
void moveDown()
{
	for (int i = 0; i < MAX_GRID; i++)
	{
		int temp = MAX_GRID-1;
		for (int begin = MAX_GRID-2; begin >=0; begin--)
		{
			if (map[begin][0] != 0)
			{
				if (map[temp][i] == 0)
				{
					map[temp][i] = map[begin][i];
					map[begin][i] = 0;
				}
				else if (map[temp][i] == map[begin][i])
				{
					map[temp][i] += map[begin][i];
					map[begin][i] = 0;
					temp--;
				}
				else
				{
					map[temp - 1][i] = map[begin][i];
					if (temp - 1 != begin)
					{
						map[begin][i] = 0;
					}
					temp--;
				}
			}
		}
	}
}
//Get keyboard keys_ getch(), which does not need to press enter to confirm the input
void keyControl()
{
	switch (_getch())
	{
	case 'w':
	case 'W':
	case 72:
		moveup();
		break;
	case 's':
	case 'S':
	case 80:
		moveDown();
		break;
	case 'a':
	case 'A':
	case 75:
		break;
	case 'd':
	case 'D':
	case 77:
		break;
	}
	printf("key\n");
}
int main()
{
	//No console window, I want graphics window easyx
	initgraph(WIN_SIZE,WIN_SIZE,SHOWCONSOLE);//The first parameter is width and the second is height
	gameInit();

	while (1)
	{
		gameDraw();
		keyControl();
	}
	return 0;
}

For the unfinished digital part of the code, you can also think about it first. Every thinking is the process of your progress!

If you have any problems in the process of learning and what you don't understand about this project, you can come to me for communication and I'll help you!

So that's all for today's sharing. More wonderful projects or knowledge content will be updated in the future. We should learn C language c + +~

Write at the end: for those who are ready to learn C/C + + programming, if you want to better improve your core programming ability (internal skill), you might as well start now!

C language c + + programming learning and communication circle, QQ group: 829164294[ Click to enter WeChat official account: C language programming learning base

Sorting and sharing (source code, project practice video, project notes, basic introductory tutorial)

Welcome to change careers and learn programming partners. Use more materials to learn and grow faster than you think!

Programming learning video sharing:

 

 

Keywords: C Programming Game Development

Added by 0riole on Sun, 21 Nov 2021 00:31:38 +0200