current position:Home>C + + game programming -- Game Name: Flash Christmas tree 2.0

C + + game programming -- Game Name: Flash Christmas tree 2.0

2022-01-27 02:41:50 Code Knight

  Compile environment :Mictosoft Visual Studio 2022, EasyX_2021

do    person : Code Knight <[email protected]>

Last modified :2021-12-16

demonstration :

( Take a look at it. , The picture will move )

 

Ideas :

More beautiful .

Code :

#include<graphics.h>
#include<time.h>
#include<conio.h>
#include<iostream>
#include"EasyXPng.h"
using namespace std;

#pragma comment(lib,"Winmm.lib")

const int width = 600;

const int length = 600;

const int MAXSNOW = 200;

const int MAXLIGHT = 20;

const int BOUNDS = 20;

//const int SZ = 10;

int key = 0;// Function selection key :2 snow ,3 Lighting 

IMAGE gift, snows, star1, star2, voids, voidss, ren, ling, li, wa, llala;

struct SNOW
{
	int 	  x;
	double    y;// Along the y The shaft falls 
	double step;// Falling speed 
	int   color;
};

SNOW snow[MAXSNOW];// Array of structs (200 A snowflake )

int TREE[BOUNDS][BOUNDS] = {
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//15-x
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//15-x
	{0,0,0,0,0,0,0,7,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,1,1,1,0,0,0,0,0,0},
	{0,0,0,0,0,7,7,7,7,7,0,0,0,0,0},
	{0,0,0,0,1,1,1,1,1,1,1,0,0,0,0},
	{0,0,0,0,0,7,7,7,7,7,0,0,0,0,0},
	{0,0,0,0,1,1,1,1,1,1,1,0,0,0,0},
	{0,0,0,7,7,7,7,7,7,7,7,7,0,0,0},
	{0,0,0,0,1,1,1,1,1,1,1,0,0,0,0},
	{0,0,0,7,7,7,7,7,7,7,7,7,0,0,0},
	{0,0,1,1,1,1,1,1,1,1,1,1,1,0,0},
	{0,0,0,0,0,0,0,2,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,2,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,2,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,2,0,0,0,0,0,0,0}//15-y
};

class christmasTree
{
public:
	void Initialize();// Night 
	//
	void drawTree();// christmas tree 
	void drawTree2();//2tree
	void drawTree3();//3tree
	//
	void initSnow(int i);// Snowflake initialization 
	void moveSnow(int i);// snow 
	void Snow(christmasTree CT);// Flying snow 
	//
	void yellowLight(int i, int j, int SZ);
	void redLight(int i, int j, int SZ);
	void Light(int i, int j, int SZ);// flash light 
	//
	void Surprise();// Pleasantly surprised 
};

void christmasTree::Initialize()
{
	initgraph(width, length);
	//setbkcolor(WHITE);
	//cleardevice();
	loadimage(&gift, "gift.jpg", 300, 300);
	loadimage(&snows, "snows.png", 30, 30);
	loadimage(&voids, "voids.png", 20, 20);
	loadimage(&voidss, "voidss.png", 20, 20);
	loadimage(&star1, "star.png", 600, 600);
	loadimage(&star2, "star.png", 300, 300);
	
	loadimage(&ren, "ren.png", 500, 500);
	loadimage(&ling, "ling.png", 300, 300);
	loadimage(&li, "li.png", 300, 300);
	loadimage(&wa, "wa.png", 300, 300);
	loadimage(&llala, "llala.png", 300, 100);

	putimage(150, 150, &gift);
	_getch();
	closegraph();
	//BeginBatchDraw();
}

void christmasTree::yellowLight(int i, int j, int SZ)
{
	if (TREE[i][j] == 7)
	{
		int x = ((j + 0) * SZ + (j + 1) * SZ) / 2;
		int y = ((i + 0) * SZ + (i + 1) * SZ) / 2;
		int r = SZ / 4;
		setlinecolor(YELLOW);
		setfillcolor(YELLOW);
		fillcircle(x, y, r);// Center formula (x1+x2)/2
		//Sleep(20);
		setlinecolor(RGB(9, 163, 67));
		setfillcolor(RGB(9, 163, 67));
		fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
	}
}

void christmasTree::redLight(int i, int j, int SZ)
{
	if (TREE[i][j] == 1)
	{
		int x = ((j + 0) * SZ + (j + 1) * SZ) / 2;
		int y = ((i + 0) * SZ + (i + 1) * SZ) / 2;
		int r = SZ / 4;
		setlinecolor(RED);
		setfillcolor(RED);
		fillcircle(x, y, r);// Center formula (x1+x2)/2
		//Sleep(20);
		setlinecolor(GREEN);
		setfillcolor(GREEN);
		fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
	}
}
int color = 10;
void christmasTree::Light(int i, int j, int SZ)
{
	//yellowLight(i, j, SZ);
	//redLight(i, j, SZ);


	int x = 20;

	putimagePng(100 + x, 50, &star2);
	putimagePng(50 + x, 100, &star2);
	putimagePng(20 + x, 150, &star2);
	putimagePng(-10 + x, 200, &star2);
	putimagePng(-30 + x, 250, &star2);
	putimagePng(-50 + x, 300, &star2);
	putimagePng(-80 + x, 350, &star2);

	putimagePng(100 + x, 50, &star2);
	putimagePng(150 + x, 100, &star2);
	putimagePng(180 + x, 150, &star2);
	putimagePng(210 + x, 200, &star2);
	putimagePng(230 + x, 250, &star2);
	putimagePng(250 + x, 300, &star2);
	putimagePng(280 + x, 350, &star2);

	putimagePng(150 + x, 180, &ren);
	putimagePng(50 + x, 200, &ling);
	
	setbkmode(TRANSPARENT);// Display transparent text 
	settextcolor(RGB(255-color, 0+color,color++));// Set font color 
	settextstyle(50, 0, _T(" Song style "));// Set font size , typeface 
	outtextxy(240, 130, _T(" St. "));// Output font 
	settextstyle(50, 0, _T(" Song style "));// Set font size , typeface 
	outtextxy(240, 190, _T(" Birthday "));// Output font 
	settextstyle(50, 0, _T(" Song style "));// Set font size , typeface 
	outtextxy(240, 250, _T(" fast "));// Output font 
	settextstyle(50, 0, _T(" Song style "));// Set font size , typeface 
	outtextxy(240, 310, _T(" Happy "));// Output font 

}

void christmasTree::drawTree()
{
	int i, j;
	setlinestyle(PS_SOLID, 36);
	int SZ = width / BOUNDS*1.25;
	for (int i = 0; i < BOUNDS; i++)
	{
		for (int j = 0; j < BOUNDS; j++)
		{
			if (TREE[i][j] == 1)
			{
				setlinecolor(GREEN);
				setfillcolor(GREEN);
				fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
				Sleep(1);
			}
			if (TREE[i][j] == 2)
			{
				setlinecolor(RGB(95, 41, 31));
				setfillcolor(RGB(95,41,31));
				fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
				Sleep(1);
			}
			if (TREE[i][j] == 7)
			{
				setlinecolor(RGB(9, 163, 67));
				setfillcolor(RGB(9, 163, 67));
				fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
				Sleep(1);
			}
			if (key == 3)
			{
				//BeginBatchDraw();
				//drawTree();
				Light(i, j, SZ);// flash light 
				//FlushBatchDraw();
			}

		}
	}
	putimagePng(-25, -40, &star1);
}

void christmasTree::drawTree2()
{
	int i, j;
	int SZ = width / BOUNDS * 1.25;
	for (int i = 0; i < BOUNDS; i++)
	{
		for (int j = 0; j < BOUNDS; j++)
		{
			if (TREE[i][j] == 1)
			{
				setlinecolor(GREEN);
				setfillcolor(GREEN);
				fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
			}
			if (TREE[i][j] == 2)
			{
				setlinecolor(RGB(95, 41, 31));
				setfillcolor(RGB(95, 41, 31));
				fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
			}
			if (TREE[i][j] == 7)
			{
				setlinecolor(RGB(9, 163, 67));
				setfillcolor(RGB(9, 163, 67));
				fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
			}
			if (key == 3)
			{
				//BeginBatchDraw();
				drawTree();
				putimagePng(-25, -40, &star1);
				Light(i, j, SZ);// flash light 
				//FlushBatchDraw();
			}
		}
	}
	putimagePng(-25, -40, &star1);
	//FlushBatchDraw();
}

void christmasTree::initSnow(int i)
{
	snow[i].y = 0;
	snow[i].x = rand() % width;
	snow[i].step = (rand() % 5000) / 10000.0 + 1;
	//snow[i].color = (int)(snow[i].step * 255 / 6.0 + 0.5);// Gradual 
	//snow[i].color = RGB(snow[i].color, snow[i].color, snow[i].color);
	cleardevice();
	drawTree2();// Painting trees 
}

void christmasTree::moveSnow(int i)// snow 
{
	//BeginBatchDraw();
	setlinestyle(PS_SOLID, 36);
	//putpixel((int)snow[i].x, snow[i].y, 0);// Painting snow 
	//cleardevice();
	putimagePng(-25, -40, &star1);
	putimagePng((int)snow[i].x, snow[i].y, &voids);
	snow[i].y += snow[i].step;
	if (snow[i].y > length) initSnow(i);// Generate new snowflakes 
	//putpixel((int)snow[i].x, snow[i].y, snow[i].color);// Draw snow again 
	putimagePng((int)snow[i].x, snow[i].y, &snows);
	//cleardevice();
	//FlushBatchDraw();
}

void christmasTree::Snow(christmasTree CT)
{
	srand((unsigned)time(NULL));
	for (int i = 0; i < MAXSNOW; i++)
	{
		CT.initSnow(i);
		snow[i].y = rand() % length;
	}

	while (!_kbhit())
	{
			drawTree2();// Painting trees 
		
		for (int i = 0; i < MAXSNOW; i++)
			CT.moveSnow(i);
		Sleep(20);
	}
	//FlushBatchDraw();
}

int main()
{
	christmasTree CT;

	CT.Initialize();
	
	initgraph(width, length);

	mciSendString(_T("open 123.mp3 alias bkmusic"), NULL, 0, NULL);// Play music 
	mciSendString(_T("play bkmusic repeat"), NULL, 0, NULL);// Loop Playback 


	CT.drawTree();

	while (cin>>key)
	{
		switch (key)
		{
		case 2:
			CT.Snow(CT);
			break;
		case 3:
			CT.Snow(CT);
			break;
		case 4:
			return 0;
		}
	}
	return 0;
}

copyright notice
author[Code Knight],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270241486624.html

Random recommended