直接搬运朋友的
https://blog.csdn.net/sandalphon4869/article/details/80862023
下面是我将几个例子结合运行的总代码,并进行了一些改进和实现方法注释
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<windows.h>
#define Width 640
#define Height 480
int main() {
initgraph(640, 480);//初始化绘图界面宽640,高480
for (int y = 0; y <= 480; y += 48)//画十条直线
line(0, y, 640, y);
_getch();
cleardevice();//清屏
//closegraph();
//initgraph(640, 480);
for (int y = 0; y <= 480; y++)//渐变色绘图
{
setcolor(RGB(0, 0, y / 2));//R红 G绿 B蓝 三色调和
line(0, y, 640, y);//间隔1画横线
}
_getch();
//closegraph();
cleardevice();//清屏
for (int y = 0; y <= 480; y += 48)//红蓝交替各5条
{
if ((y / 48) % 2 == 1)
setcolor(RGB(255, 0, 0));
else
setcolor(RGB(0, 0, 255));
line(0, y, 640, y);
}
_getch();
cleardevice();
//setfillcolor(BLUE);//当前填充色
for (int y = 0; y <= 480; y += 10)//改为边框填充色均变化的下落小球
{
setcolor(RGB(0, 0, y));
setfillcolor(RGB(y/5, y/5, y/10 ));
fillcircle(100, y, 40);//填充圆 圆心坐标100 y 半径40
Sleep(200);//滞留200毫秒
cleardevice();//然后清除
}
_getch();
cleardevice();
//反弹的小球
double ball_x = 100, ball_y = 100;//初始圆心坐标
double ball_vx = 1, ball_vy = 1;//横纵坐标偏移量
double radius = 25;
BeginBatchDraw();//批量绘图取消闪烁
while (1)
{
setcolor(GREEN);
setfillcolor(BLUE);
fillcircle(ball_x, ball_y, radius);
Sleep(5);
FlushBatchDraw();
cleardevice();//完全可以用清屏代替
/*setcolor(BLACK);
setfillcolor(BLACK);
fillcircle(ball_x, ball_y, radius);//用黑色小球将5毫秒前绘制的蓝色小球覆盖 避免留下长长的运动轨迹
*/
ball_x += ball_vx;
ball_y += ball_vy;//小球45度角起飞反弹
if (ball_x <= radius || ball_x >= Width - radius)//判断圆心到边框距离 保持圆不飞出边框
ball_vx = -ball_vx;//到达边框则使偏移量取反 营造反弹效果
if (ball_y <= radius || ball_y >= Height - radius)
ball_vy = -ball_vy;
}
EndBatchDraw();
_getch();
closegraph();
return 0;
}