C语言实现的冰墩墩
在windows系统下,vs 2022编译。
其中#include <graphics.h>需要自己下载安装。
环境配置没什么难度,直接上demo。
代码如下:
#include <graphics.h>
#include <conio.h>
#include <math.h>
#define PI acos(-1.0)
#define WIDTH 800
#define HEIGHT 800
double th = PI / 180;
void DrawBack();
void DrawEar();
void DrawLeg();
void DrawArm();
void DrawBody();
void DrawEye();
void DrawNose();
void DrawMouth();
void DrawColour();
void DrawLogo();
void DrawHat();
void heart(int x0, int y0, int size, COLORREF C);
void DrawEllipse(int x0, int y0, int a, int b, int k, COLORREF color);
int main()
{
initgraph(WIDTH, HEIGHT);
DrawBack();
setorigin(WIDTH / 2, HEIGHT / 2);
DrawEar();
DrawLeg();
DrawArm();
DrawBody();
DrawEye();
DrawNose();
DrawMouth();
DrawColour();
DrawLogo();
heart(330, -120, 10, RED);
setfillcolor(RED);
floodfill(330, -100, RED);
_getch();
return 0;
}
void DrawBack()
{
float H = 190;
float S = 1;
float L = 0.7f;
for (int y = 0; y < HEIGHT; y++)
{
L += 0.0002f;
setlinecolor(HSLtoRGB(H, S, L));
line(0, y, WIDTH - 1, y);
}
}
void DrawEar()
{
setfillcolor(BLACK);
solidcircle(172,-300,62);
solidcircle(-172,-300,62);
}
void DrawLeg()
{
setfillcolor(BLACK);
solidellipse(-44, -155, 168, 348);
solidellipse(44, -155, -168, 348);
}
void DrawArm()
{
DrawEllipse(-267, 50, 100, 60, 50, BLACK);
DrawEllipse(297, -60, 100, 60, 50, BLACK);
setfillcolor(BLACK);
floodfill(-267, 50, BLACK);
floodfill(297, -60, BLACK);
}
void DrawBody() {
setfillcolor(BLACK);
setlinestyle(PS_SOLID, 8);
setfillcolor(WHITE);
fillellipse(-270, -354, 270, 260);
}
void DrawEye() {
DrawEllipse(-109, -131, 84, 60, 314, BLACK);
DrawEllipse(109, -131, 84, 60, 46, BLACK);
setfillcolor(BLACK);
floodfill(-109, -131, BLACK);
floodfill(109, -131, BLACK);
setfillcolor(WHITE);
solidcircle(-92, -137, 30);
solidcircle(92, -137, 30);
setfillcolor(BLACK);
solidcircle(-92, -137, 26);
solidcircle(92, -137, 26);
setfillcolor(WHITE);
solidcircle(-81, -146, 9);
solidcircle(81, -146, 9);
}
void DrawNose() {
setlinestyle(PS_SOLID, 1);
setfillcolor(BLACK);
solidellipse(-26, -106, 26, -63);
}
void DrawMouth() {
setlinecolor(BLACK);
setlinestyle(PS_SOLID, 8);
arc(-43, -75, 43, -7, PI, 0);
}
void DrawColour() {
setfillcolor(BLACK);
setlinestyle(PS_SOLID, 8);
setfillcolor(RGB(91,198,250));
ellipse(-205, -265, 205, 74);
setlinecolor(RGB(119,216,113));
ellipse(-215, -275, 215, 84);
setlinecolor(RGB(254,122,185));
ellipse(-225, -285, 225, 94);
}
void DrawLogo() {
RECT r = { -116,100,116,175 };
settextcolor(BLACK);
settextstyle(60, 0, _T("黑体"));
drawtext(_T("BeiJing"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
r = { -116,-175,116,250 };
drawtext(_T("2022"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
void DrawEllipse(int x0, int y0, int a, int b, int k, COLORREF color)
{
double i;
double x, y, tx, ty;
for (i = -180; i < 180; i += 0.5)
{
x= a * cos(i * th);
y = b * sin(i * th);
tx = x;
ty = y;
x = tx * cos(k * th) - ty * sin(k * th)+x0;
y = y0 - (ty * cos(k * th) + tx * sin(k * th));
setfillcolor(color);
solidcircle((int)x,(int)y, 2);
}
}
void heart(int x0, int y0, int size, COLORREF C)
{
double m, n, x, y;
double i;
for (i = 0; i < 2 * size; i += 0.01)
{
m = i;
n=-size * (((sin(i)) * sqrt(fabs(cos(i)))) / (sin(i) + 1.14142))- 2*sin(i) + 2;
x = m * cos(m) + x0;
y = n * sin(n) + y0;
setfillcolor(C);
solidcircle((int)x, (int)y, 1);
}
}
实现如下: