首页 > 其他分享 >【C语言】爱心代码与EasyX的安装

【C语言】爱心代码与EasyX的安装

时间:2024-10-13 11:48:12浏览次数:3  
标签:index int double frame EasyX C语言 爱心 points size

前几天学习了C语言上爱心代码:

1.静态的

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main(int argc,char*argv[]){
	float x,y,a;
	for(y=1.5;y>-1.5;y-=0.1){
		for(x=-1.5;x<1.5;x+=0.05){
			a = x * x +y * y -1;
			putchar(a*a*a-x*x*y*y*y<=0.0?'*':' ');
		}
		system("color 0c");
		putchar('\n');
	}
	printf("天天开心\n");
	printf("好好学习 好好生活\n");
	return 0;
}

2.动态的

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

struct Point {
    double x, y;
    COLORREF color;
};

COLORREF colors[256] = { RGB(255,32,83), RGB(252,222,250), RGB(255,0,0), RGB(255,0,0), RGB(255,2,2), RGB(255,0,8), RGB(255,5,5) };
const int xScreen = 1200;
const int yScreen = 800;
const double PI = 3.14159265358979323846;
const double e = 2.71828;
const double averag_distance = 0.162;
const int quantity = 506;
const int circles = 210; 
const int frames = 20;
Point origin_points[quantity];
Point points[circles * quantity];  // 修改为circles
IMAGE images[frames];

double screen_x(double x) {
    x += xScreen / 2;
    return x;
}

double screen_y(double y) {
    y = -y + yScreen / 2;
    return y;
}

int creat_random(int x1, int x2) {
    if (x2 > x1)
        return rand() % (x2 - x1 + 1) + x1;
    return x1;
}

void creat_data() {
    int index = 0;
    double x1 = 0, y1 = 0, x2 = 0, y2 = 0;
    for (double radian = 0.1; radian <= 2 * PI; radian += 0.005) {
        x2 = 16 * pow(sin(radian), 3);
        y2 = 13 * cos(radian) - 5 * cos(2 * radian) - 2 * cos(3 * radian) - cos(4 * radian);

        double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
        if (distance > averag_distance) {
            x1 = x2, y1 = y2;
            origin_points[index].x = x2;
            origin_points[index++].y = y2;
        }
    }
    index = 0;
    for (double size = 0.1, lightness = 1.5; size <= 20; size += 0.1) {
        double success_p = 1 / (1 + pow(e, 8 - size / 2));
        if (lightness > 1) lightness -= 0.0025;
        for (int i = 0; i < quantity; ++i) {
            if (creat_random(0, 100) / 100.0 < success_p) { 
                COLORREF color = colors[creat_random(0, 6)];
                points[index].color = RGB(GetRValue(color) / lightness, GetGValue(color) / lightness, GetBValue(color) / lightness);
                points[index].x = size * origin_points[i].x + creat_random(-4, 4);
                points[index++].y = size * origin_points[i].y + creat_random(-4, 4);
            }
        }
    }
    int points_size = index;
    for (int frame = 0; frame < frames; ++frame) {
        images[frame] = IMAGE(xScreen, yScreen);
        SetWorkingImage(&images[frame]);
        for (index = 0; index < points_size; ++index) {
            double x = points[index].x, y = points[index].y;
            double distance = sqrt(pow(x, 2) + pow(y, 2));
            double distance_increase = -0.0009 * distance * distance + 0.35714 * distance + 5;
            double x_increase = distance_increase * x / distance / frames;
            double y_increase = distance_increase * y / distance / frames;
            points[index].x += x_increase;
            points[index].y += y_increase;
            setfillcolor(points[index].color);
            solidcircle(screen_x(points[index].x), screen_y(points[index].y), 1);
        }
        for (double size = 17; size < 23; size += 0.3) {
            for (index = 0; index < quantity; ++index) {
                double x, y;
                if ((creat_random(0, 100) / 100.0 > 0.6 && size >= 20) || (size < 20 && creat_random(0, 100) / 100.0 > 0.95)) {
                    x = origin_points[index].x * size + creat_random(-frame * frame / 5 - 15, frame * frame / 5 + 15);
                    y = origin_points[index].y * size + creat_random(-frame * frame / 5 - 15, frame * frame / 5 + 15);
                }
                else {
                    x = origin_points[index].x * size + creat_random(-5, 5);
                    y = origin_points[index].y * size + creat_random(-5, 5);
                }
                setfillcolor(colors[creat_random(0, 6)]);
                solidcircle(screen_x(x), screen_y(y), 1);
            }
        }
    }
    SetWorkingImage();
}

int main() {
    initgraph(xScreen, yScreen);
    BeginBatchDraw();
    srand(time(0));
    creat_data();

    bool extend = true, shrink = false;
    for (int frame = 0; !_kbhit(); ) {
        putimage(0, 0, &images[frame]);
        FlushBatchDraw();
        Sleep(20);
        cleardevice();
        if (extend) {
            frame == 19 ? (shrink = true, extend = false) : ++frame;
        }
        else {
            frame == 0 ? (shrink = false, extend = true) : --frame;
        }
    }
    EndBatchDraw();
    closegraph();
    return 0;
}

这里需要一个图形库文件:<graphics.h>

但是如今的IDE中不再内置这个头文件,需要自行安装EasyX

安装方法:

1.下载安装包:

打开官网:EasyX Graphics Library for C++

点击右上角【下载】

点击右侧【下载EasyX】(注意自行更改下载位置)

打开安装文件并点击【下一步】

!!!注意:必须安装了C++桌面开发组件才可以被检测到,若没有安装请在Visual Studio Installer中进行安装

!!!注意:如果你的Visual Studio2022安装在了C盘这里可以自动检测出来,如果安装在其他盘无法被自动检测到,需要自行安装,操作如下:

解压安装包(使用本地压缩软件(如7-zip,但是不免费)进行解压或使用在线网页(free),如:在线文件压缩和转换变得简单。免费! | ezyZip

选择导航栏的【在线解压文件-其他】,寻找到解压exe文件,上传文件并进行解压,解压后保存本地

然后按照EasyX 文档 - 安装中提供的VS版本找出自己需要安装的文件并复制

粘贴到如下所示:

EasyX安装包\include\easyx.h				拷贝到 D:\App\VS2022\VC\Auxiliary\VS\include
EasyX安装包\include\graphics.h			拷贝到 D:\App\VS2022\VC\Auxiliary\VS\include
EasyX安装包\lib\VC2015\x86\EasyXa.lib	拷贝到 D:\App\VS2022\VC\Auxiliary\VS\lib\x86
EasyX安装包\lib\VC2015\x86\EasyXw.lib	拷贝到 D:\App\VS2022\VC\Auxiliary\VS\lib\x86
EasyX安装包\lib\VC2015\x64\EasyXa.lib	拷贝到 D:\App\VS2022\VC\Auxiliary\VS\lib\x64
EasyX安装包\lib\VC2015\x64\EasyXw.lib	拷贝到 D:\App\VS2022\VC\Auxiliary\VS\lib\x64

[备注]引用资源与参考内容:

https://easyx.cn/

https://docs.easyx.cn/zh-cn/setup

在线文件压缩和转换变得简单。免费! | ezyZip

安装 EasyX 图形库时检测不到我的 VisualStudio 2019 / 2022 - CodeBus

标签:index,int,double,frame,EasyX,C语言,爱心,points,size
From: https://blog.csdn.net/wzzzz6423/article/details/142869019

相关文章

  • 【C语言基础】核心关键字详解与应用
    目录一、void1.1.作用1.2.代码示例二、基本数据类型(char、int、float、double等)2.1.char(字符类型)2.2.int(整型)2.3.float(单精度浮点型)2.4.double(双精度浮点型)2.5.代码示例三、控制流程语句(if、else、switch、case、default等)3.1.if和else语句3.2.switch......
  • 【C语言基础】全局变量与局部变量的深入解析
    目录一、全局变量1.1.定义与声明1.2.特性1.2.1.生命周期1.2.2.作用域1.2.3.跨文件访问1.2.4.限制访问范围1.3. 示例1.4.注意事项1.4.1.过度使用全局变量导致代码难以理解和维护1.4.2.限制全局变量的使用范围1.4.3.清晰的命名和文档1.4.4.考虑替代方案......
  • C语言中指针在数组的使用--摘自Micro_Frank
    #include<stdio.h>#include<stdint.h>intmain(void){ //指针的算术运算 int32_tnumbers[]={10,20,30,40,50,60,70,80,90,100}; int32_t*ptr=numbers;//无取地址符,也无数组下标,表示&numbers[0] //数组在内存中是连续的 //0->00000000090 //所以......
  • C语言之printf的解析
    一、前言我们学习程序开发的第一个编程基本就是输出。下面我们学习一下的输出函数printf并学习。二、项目实践1.引入库文件#include<stdio.h>2.标准输出标准格式:printf(格式控制字符串,输出列表);#include<stdio.h>intmain(){ printf("helloworld\n"); return0;}执行程......
  • c语言进阶版第19课—文件操作
    文章目录1.文件1.1文件的作用1.2文件是什么1.3文件名1.4二进制文件和文本文件2.文件的打开和关闭2.1流和标准流2.2文件指针2.3文件的打开和关闭2.4文件的顺序读写2.4.1fputc函数2.4.2fgetc函数2.4.3fputs函数2.4.4fgets函数2.4.5fprintf函数2.4.6fscanf......
  • C语言 队列例程
    队列的数据成员为结构体或数组#include<stdio.h>#include<stdlib.h>#include<assert.h>typedefstructnodeArr{ intdat[2]; structnodeArr*next;}NodeArr;NodeArr*initQueueAr();intis_emeptyAr(NodeArr*Q);voidenQueueAr(NodeArr*Q,int*arr);......
  • c语言链表-学生管理系统
    include<stdio.h>include<stdlib.h>include<string.h>//定义结构体structSTU{charnum[8];//学号charname[5];//姓名intscore;//成绩};//定义链表structtemp{structSTU*s;structtemp*next;};voidadd(structtemp**head);//......
  • C语言的常见概念(三)
    上一期讲到了关于转义字符的知识与使用,今天继续为大家介绍C语言中的语句与语句使用,以及关于注释的相关知识语句和语句的使用语句和语句分类C语言的代码是由⼀条⼀条的语句构成的,C语言中的语句可为以下五类:•空语句•表达式语句•函数调用语句•复合语句•控制......
  • 【C语言】语义陷阱(5):揭秘空指针与空字符串的微妙差异
    目录一、空指针(NullPointer)1.1.定义与表示1.2.用途1.3.安全性 1.4.注意事项1.5.空指针与野指针的区别1.5.1.特性对比1.5.2.安全性与风险1.5.3.编程实践二、指向空字符串的指针2.1.定义2.2.字符数组与空字符串2.3.指针的初始化2.4.空字符串的用途2......
  • 【趣学C语言和数据结构100例】
    【趣学C语言和数据结构100例】问题描述一个球从100m高度自由落下,每次落地后反弹回原高度的一半,再落下,求它在第10次时共经过多少米,第10次反弹多高。猴子吃桃问题。猴子第1天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第2天早上又将剩下的桃子吃掉一......