【C++】Teach you how to draw a starry sky for your family during the Mid-Autumn Festival

foreword

The Mid-Autumn Festival is approaching, and everyone wants to surprise their families! Today I will teach you how to draw a starry sky with C++ and Easyx.

(effect map:)


1. Prepare Easyx

First we have to go to the Easyx official website to download the installer,

After the download is complete, open the program and click "Next";

Then select your editor and click "Install".


 2. Function introduction

Here I briefly introduce a few functions that I will use today:

1、solidcircle

This function is used to draw filled circles without borders.

Function prototype:

void solidcircle(
	int x,
	int y,
	int radius
);

Parameter explanation:

x: the x-coordinate of the center of the circle

y: y coordinate of the center of the circle

radius: the radius of the circle

2、setfillcolor

This function is used to set the current device fill color.

Function prototype:

void setfillcolor(COLORREF color);

Parameter explanation:

color: fill color

3、getbkcolor

This function is used to get the current device background color.

COLORREF getbkcolor();

4initgraph

This function is used to initialize the drawing window.

Function prototype:

HWND initgraph(
	int width,
	int height,
	int flag = NULL
);

Parameter explanation:

width: the width of the drawing window

height : the height of the drawing window

flag: The style of the drawing window, the default is NULL. Can be the following values:

value meaning
EW_DBLCLKS Support mouse double click event in drawing window
EW_NOCLOSE Disable the close button of the plot window
EW_NOMINIMIZE Disable the minimize button of the plot window
EW_SHOWCONSOLE show console window

5、closegraph

This function is used to close the drawing window.

void closegraph();

6、putpixel

This function is used to draw points.

void putpixel(
	int x,
	int y,
	COLORREF color
);

parameter

x: the x coordinate of the point

y: the y coordinate of the point

color: the color of the point


 3. Realization principle

The implementation principle of this project is not difficult. The moon is a filled circle without borders; the big star is to draw a circle first, and then draw a few circles around it with the background color; the small star is to draw dots at random positions.


4. Tutorial

1. Open the header file:

#include <graphics.h>

2. Initialize the window in the main function (the same below):

initgraph(640, 480);

3. Set the fill color:

setfillcolor(YELLOW);

4. Draw a filled circle with no border:

solidcircle(450, 80, 50);

Since we're not just drawing one big star, we're better off doing it with a function.

5. Make a function to draw a big star outside the main function:

void star(int x, int y) {
	setfillcolor(YELLOW);
	solidcircle(x, y, 20);
	setfillcolor(getbkcolor());
	solidcircle(x - 20, y - 20, 20);
	solidcircle(x + 20, y - 20, 20);
	solidcircle(x - 20, y + 20, 20);
	solidcircle(x + 20, y + 20, 20);
	return;
}

6. Then write in the main function:

star(50, 50);
star(570, 400);
star(300, 60);
star(250, 300);
star(50, 400);

And because the positions of our little stars are random, in order to generate random numbers, we need:

7. Import the header file:

#include <time.h>

8. Write a sentence at the beginning of the main function:

srand((unsigned int)time(NULL));

9. Add a code for drawing (one hundred) small stars after the code for drawing big stars:

for (int i = 0; i < 100; i++) {
	putpixel(rand() % 640, rand() % 480, YELLOW);
}

To have the effect of pressing any key to exit, we need:

10. Import the header file:

#include <conio.h>

11. Write a sentence after the code for drawing the little star:

_getch();

12. Finally, close the window and exit the program:

closegraph();
return 0;

5. Complete code

#include <graphics.h>
#include <time.h>
#include <conio.h>

void star(int x, int y) {
	setfillcolor(YELLOW);
	solidcircle(x, y, 20);
	setfillcolor(getbkcolor());
	solidcircle(x - 20, y - 20, 20);
	solidcircle(x + 20, y - 20, 20);
	solidcircle(x - 20, y + 20, 20);
	solidcircle(x + 20, y + 20, 20);
	return;
}

int main() {
	srand((unsigned int)time(NULL));
	initgraph(640, 480);
	setfillcolor(YELLOW);
	solidcircle(450, 80, 50);
	star(50, 50);
	star(570, 400);
	star(300, 60);
	star(250, 300);
	star(50, 400);
	for (int i = 0; i < 100; i++) {
		putpixel(rand() % 640, rand() % 480, YELLOW);
	}
	_getch();
	closegraph();
	return 0;
}

Alright, that's it for today. The article has references. Please support if you like it!

Guess you like

Origin blog.csdn.net/qq_43546083/article/details/126548294