各位,今天我们用C语言搞点好玩的,给大家带来一个会自动变色的心,话不多说,上源码:
代码语言:javascript复制
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define I 20
#define R 340
#include <string.h>
int main()
{
system("mode con cols=80 lines=35");//设置窗口大小
system("color 84"); //设置窗口颜色
system("title 难以抑制的心");//设置标题
char answer[10];
printf("输入love,你可以看到我的真心n");
scanf("%s", answer);
float y, x, z, f;
for (y = 1.5f; y > -1.5f; y -= 0.1f)
{
for (x = -1.5f; x < 1.5f; x = 0.05f)
{
z = x * x y * y - 1;
f = z * z*z - x * x*y*y*y;
putchar(f <= 0.0f ? "*********"[(int)(f*-8.0f)] : ' ');
}
putchar('n');
}
long time;
for (;;)
{
system("color a");
for (time = 0; time<99999999; time );
system("color b");
for (time = 0; time<99999999; time );
system("color c");
for (time = 0; time<99999999; time );
system("color d");
for (time = 0; time<99999999; time );
system("color e");
for (time = 0; time<99999999; time );
system("color f");
for (time = 0; time<99999999; time );
system("color 0");
for (time = 0; time<99999999; time );
system("color 1");
for (time = 0; time<99999999; time );
system("color 2");
for (time = 0; time<99999999; time );
system("color 3");
for (time = 0; time<99999999; time );
system("color 4");
for (time = 0; time<99999999; time );
system("color 5");
for (time = 0; time<99999999; time );
system("color 6");
for (time = 0; time<99999999; time );
system("color 7");
for (time = 0; time<99999999; time );
system("color 8");
for (time = 0; time<99999999; time );
system("color 9");
}
getchar();
return 0;
}
怎么样,效果还可以吧!其实这个实现的原理还是比较简单的,为了让窗口别跟控制台那样难看,我调用了system函数对窗口的大小、颜色和标题进行了设置,心形的绘制和变色主要是通过for循环实现的,绘制心形是代码中第一个大的for循环,比较简单就不说了,而颜色的变化主要是通过下面两行代码控制的,不同数字代表不同的颜色,不断循环即可!
代码语言:javascript复制
for (time = 0; time<99999999; time );
system("color 1");
这里有一点提醒一下:代码要求输入love进行心形的显示,为了方便简单同时避免输错不显示的问题,我省略了对输入内容检查的代码,也就是说随便输什么都会显示心形,有兴趣的可以自己加上输入检测,也比较简单,好了就这么多吧,怎么样,对你有帮助吗?感谢耐心阅读!