EasyX知识点
程序员文章站
2022-04-07 19:17:01
...
1、设置背景色时,需要两条命令:
setbkcolor(YELLOW);
cleardevice;
示例代码:
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
int main() {
initgraph(640,480);
setbkcolor(YELLOW);
cleardevice();
for (int y = 0; y <= 480;y+=48) {
setcolor(RGB(255, 0, y));
line(0, y, 640, y);
}
for (int x = 0; x <= 640; x+= 64) {
setcolor(RGB(255, 0, x));
line(x, 0, x,480);
}
getch();
closegraph();
return 0;
}
运行结果:
2、围棋棋盘
示例代码:
#include <graphics.h> //引用EasyX图形库
#include <conio.h>
#include <stdio.h>
int main()
{
int step = 30;
initgraph(640, 480); //初始画布
setbkcolor(YELLOW);
cleardevice();
setlinestyle(PS_SOLID, 2);
setcolor(RGB(0, 0, 0));
int i;
for (i = 1;i <= 19; i++)
{
line(i*step, 1 * step, i*step, 19 * step);
line(1 * step, i*step, 19 * step, i*step);
}
getch();
closegraph();
return 0;
}
运行结果:
3、国际象棋棋盘
运行代码:
#include <graphics.h> //引用EasyX图形库
#include <conio.h>
int main()
{
initgraph(500, 500); //初始画布
int step = 50;
setbkcolor(YELLOW); //设置背景色为黄色
cleardevice(); //用背景色清空屏幕
int i, j;
for (i = 1; i <= 8; i++)
{
for (j = 1; j <= 8; j++)
{
if ((i + j) % 2 == 1)
{
setfillcolor(BLACK);
solidrectangle(i*step, j*step, (i + 1)*step, (j + 1)*step); //绘制黑色砖块
}
else
{
setfillcolor(WHITE);
solidrectangle(i*step, j*step, (i + 1)*step, (j + 1)*step); //绘制白色砖块
}
}
}
getch();
closegraph();
return 0;
}
运行结果:
4、画一个圆形的渐变色
解析:
首先,我们要用到圆形的基本公式:
x*x + y*y = r*r
让弧度从0~2*3.14,然后需要根据弧度和半径算出(x,y),
用pi表示圆周率
用r表示半径
用a表示弧度(小数)
用c表示颜色
于是:
x=r*cos(a)
y=r*sin(a)
c=a*255/(2*pi)
代码:
#include <graphics.h>
#include <conio.h>
#include <math.h>
#define PI 3.14
void main()
{
initgraph(640, 480);
int c;
double a;
int x, y, r = 200;
for (a = 0; a < PI * 2; a += 0.0001)
{
x = (int)(r * cos(a) + 320 + 0.5);
y = (int)(r * sin(a) + 240 + 0.5);
c = (int)(a * 255 / (2 * PI) + 0.5);
setcolor(RGB(c, 0, 0));
line(320, 240, x, y);
}
getch();
closegraph();
}
运行结果:
5、 最简单的,来个全屏的渐变色吧,目的时理解怎么将数学当作工具
解析:
就是需要将0~255的颜色和0~479的y轴对应起来
c 表示颜色,范围0~255
y 表示y轴,范围0~479
于是:
c / 255 = y / 479
c = y / 479 * 255 = y * 255 / 479 (先算乘法再算除法可以提高精度)
代码:
#include <graphics.h>
#include <conio.h>
void main()
{
initgraph(640, 480);
int c;
for (int y = 0; y < 480; y++)
{
c = y * 255 / 479;
setcolor(RGB(0, 0, c));
line(0, y, 639, y);
}
getch();
closegraph();
}
运行结果:
6、简单动画,小球在画布上面*运动
代码:
#include <graphics.h>
#include <conio.h>
#include <windows.h>
#define Width 640
#define High 480
int main()
{
float ball_x, ball_y;
float ball_vx, ball_vy;
float radius;
initgraph(Width,High);
ball_x = Width / 2;
ball_y = High / 2;
ball_vx = 1;
ball_vy = 1;
radius = 20;
BeginBatchDraw();
while (1) {
//绘制黑线、黑色填充的圆
setcolor(BLACK);
setfillcolor(BLACK);
fillcircle(ball_x,ball_y,radius);
//更新小球的坐标
ball_x = ball_x + ball_vx;
ball_y = ball_y + ball_vy;
if (ball_x <= radius || ball_x >= (Width - radius))
{
ball_vx = -ball_vx;
}
if (ball_y <= radius || ball_y >= (High - radius))
{
ball_vy = -ball_vy;
}
//绘制黄线、绿色填充的圆
setcolor(YELLOW);
setfillcolor(GREEN);
fillcircle(ball_x, ball_y, radius);
FlushBatchDraw();
//延时
Sleep(3);
}
EndBatchDraw();
closegraph();
return 0;
}
运行结果:
上一篇: 面试官问你分析过Mybatis源码吗?
下一篇: 520最实用的两个Python表白程序