赞
踩
在计算机图形学的世界中,有很多方法可以使程序的界面更加吸引人。在本篇博客中,我将向大家介绍如何使用 EasyX 图形库在 C++ 中创建一个动态的爱心背景。这不仅是一个简单的动画效果,它还包括背景的星星、旋转的心形以及一个美观的背景渐变。
EasyX 是一个简单易用的 C++ 图形库,特别适合初学者和那些想要快速为其应用程序添加图形的开发者。它提供了一系列函数,可以帮助你绘制形状、设置颜色和实现动画效果。
我们的目标是创建一个动态的爱心,它会在背景中旋转。背景将有一个从深空蓝渐变到黑色的效果,并散布有颜色各异的小星星。
每颗星星都有其坐标、颜色和亮度。我们使用一个 struct
来表示:
- struct Star {
- int x, y;
- COLORREF color;
- float intensity;
- float intensityChange;
- };
我们使用参数方程来绘制心形,并为它添加了旋转效果:
- void DrawHeart(int x, int y, COLORREF color, float scale, float angle) {
- BeginBatchDraw(); // 开始批量绘制
- const int thickness = 2; // 调整这个值来改变心形的粗细
- for (float t = 0; t < 2 * 3.14159; t += 0.01) {
- float x_cord = scale * (16 * pow(sin(t), 3));
- float y_cord = scale * -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t));
-
- // 旋转
- float rotatedX = x_cord * cos(angle) - y_cord * sin(angle);
- float rotatedY = x_cord * sin(angle) + y_cord * cos(angle);
-
- for (int dx = -thickness; dx <= thickness; dx++) {
- for (int dy = -thickness; dy <= thickness; dy++) {
- putpixel(x + rotatedX + dx, y + rotatedY + dy, color);
- }
- }
- }
- EndBatchDraw(); // 结束批量绘制,并显示在前台
- }

在 main()
函数中,我们首先初始化 EasyX 的图形窗口。接着,我们随机生成一组星星,并存储它们的属性。在主循环中,我们绘制背景渐变、星星和心形,并实现心形的动态效果。
- #include <graphics.h>
- #include <conio.h>
- #include <math.h>
- #include <vector>
-
- struct Star {
- int x, y;
- COLORREF color;
- float intensity;
- float intensityChange;
- };
-
-
- // 修改后的心形公式函数
- void DrawHeart(int x, int y, COLORREF color, float scale, float angle) {
- BeginBatchDraw(); // 开始批量绘制
- const int thickness = 2; // 调整这个值来改变心形的粗细
- for (float t = 0; t < 2 * 3.14159; t += 0.01) {
- float x_cord = scale * (16 * pow(sin(t), 3));
- float y_cord = scale * -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t));
-
- // 旋转
- float rotatedX = x_cord * cos(angle) - y_cord * sin(angle);
- float rotatedY = x_cord * sin(angle) + y_cord * cos(angle);
-
- for (int dx = -thickness; dx <= thickness; dx++) {
- for (int dy = -thickness; dy <= thickness; dy++) {
- putpixel(x + rotatedX + dx, y + rotatedY + dy, color);
- }
- }
- }
- EndBatchDraw(); // 结束批量绘制,并显示在前台
- }
-
- int main() {
- // 初始化图形窗口
- initgraph(640, 480);
- setbkcolor(BLACK); // 设置背景色为黑色
- cleardevice(); // 清空屏幕
-
- // 创建星星
- const int numStars = 100;
- std::vector<Star> stars;
- for (int i = 0; i < numStars; i++) {
- Star star = {
- rand() % 640,
- rand() % 480,
- RGB(rand() % 256, rand() % 256, rand() % 256),
- (rand() % 100) / 100.0f,
- (rand() % 5 + 1) / 500.0f
- };
- stars.push_back(star);
- }
-
- float scale = 10;
- bool increase = true;
- COLORREF heartColor = RED;
- float angle = 0;
- BeginBatchDraw(); // 开始批量绘制
-
- while (!_kbhit()) { // 直到有键被按下
- cleardevice(); // 清空屏幕
-
- // 绘制渐变背景
- for (int i = 0; i < 480; i++) {
- float ratio = (float)i / 480;
- COLORREF bgColor = RGB(0, 0, ratio * 50);
- setlinecolor(bgColor);
- line(0, i, 640, i);
- }
-
- // 绘制星星
- for (auto& star : stars) {
- star.intensity += star.intensityChange;
- if (star.intensity > 1 || star.intensity < 0.5) {
- star.intensityChange = -star.intensityChange;
- }
- COLORREF modifiedColor = RGB(GetRValue(star.color) * star.intensity, GetGValue(star.color) * star.intensity, GetBValue(star.color) * star.intensity);
- putpixel(star.x, star.y, modifiedColor);
- }
-
- if (increase) {
- scale += 0.1;
- }
- else {
- scale -= 0.1;
- }
-
- if (scale > 15 || scale < 10) {
- increase = !increase;
- }
-
- // 改变心的颜色
- int r = GetRValue(heartColor);
- int g = GetGValue(heartColor);
- int b = GetBValue(heartColor);
- r = (r + 1) % 256;
- g = (g + 2) % 256;
- b = (b + 3) % 256;
- heartColor = RGB(r, g, b);
-
- DrawHeart(320, 240, heartColor, scale, angle);
- angle += 0.005; // 调整这个值来改变旋转速度
-
- Sleep(10);
- EndBatchDraw(); // 结束批量绘制,并显示在前台
- }
-
- closegraph();
- return 0;
- }

当你运行上述代码时,你将看到一个美丽的动态背景,上面有一个不断旋转的爱心和一群闪烁的星星。通过调整参数,你可以轻松改变动画的速度、颜色和其他属性。
参考资源:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。