Easyx 学习记录
1. 创建小球
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xgq8oN2i-1655694620229)(https://i.loli.net/2021/10/23/8nHWEmclvKD6CFr.png)]
#include <iostream>
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
initgraph(600, 600);
fillcircle(400, 120,50);//北
fillcircle(320, 200, 50);//西
fillcircle(400,200, 25);//中
fillcircle(480, 200, 50);//东
fillcircle(400, 280, 50);//南
_getch();//和getchar一样
closegraph();
return 0;
}
采用常量可以使我们的代码的健壮性增强,易于维护和修改
#include <iostream>
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
const int x = 300, y = 200, mid_radius = 25,others_radius = 50;
int main()
{
initgraph(600, 600);
fillcircle(x, y-80,others_radius);//北
fillcircle(x-80, y, others_radius);//西
fillcircle(x,y, mid_radius);//中
fillcircle(x+80, y, others_radius);//东
fillcircle(x, y+80, others_radius);//南
_getch();
closegraph();
return 0;
}
2. 实现小球匀速下滑
EasyX 白色小球 匀速下滑动画_哔哩哔哩_bilibili
#include <iostream>
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
initgraph(600, 600);
int cnt = 0;
int x = 300, y = 100, r = 50;
while (++cnt < 500) {
fillcircle(x, y, r);
y += 10;
Sleep(200);
cleardevice();
}
_getch();
closegraph();
return 0;
}
3. 小球往返运动
#include <iostream>
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
initgraph(600, 600);
int cnt = 0;
int x = 300, y = 100, r = 50;
int step = 10;
while (true) {
fillcircle(x, y, r);
if (y >= 550) step = -10;
if (y <= 50) step = 10;
y += step;//实现加速运动的话,我们只需要每次加上一个变量即可
Sleep(30);
cleardevice();
}
_getch();
closegraph();
return 0;
}
4. 小球抛物线运动
#include <iostream>
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <cmath>
int main()
{
double width = 900, height = 600;
initgraph(width, height);
double x = 300, y = 100, r = 10;
double step = 5;
/*(1)水平方向:s = v₀×t
(2)竖直方向:h = (1 / 2)gt²
*/
double init_v = 10, t = 0, g = 5;
while (true) {
fillcircle(x, y, r);
double s = init_v * t;
double h = 0.5 * g *pow(t,2);
x += s;
y += h;
t += 0.5;
if (x >= width - 20 || y >= height - 20) break;
Sleep(100);
cleardevice();
}
_getch();
closegraph();
return 0;
}
5. 按空格小球弹起一次
#include <iostream>
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int width = 900, height = 600;
initgraph(width, height);
int cir_x = 300, cir_y = 570, cir_r = 30;
fillcircle(cir_x, cir_y, cir_r);
int rec_left = 400, rec_top = 500, rec_right = 430, rec_bottom = height;
fillrectangle(rec_left, rec_top, rec_right, rec_bottom);
while (1) {
if (_kbhit()) {//检测键盘是否有输入
char input = _getch();
if (input == ' ') {
auto ball_jump = [=](int x, int y) mutable {
int step = 0;
int ty = y;//记录一下原来的位置,我们只跳起一次即可
while (1) {
if (y >= 570) step = -5;
if (y <= 350) step = 5;
y += step;
fillcircle(x, y, cir_r);
fillrectangle(rec_left, rec_top, rec_right, rec_bottom);
if (y == ty) break;
Sleep(20);
cleardevice();
}
};
ball_jump(cir_x, cir_y);
}
}
}
_getch();
closegraph();
return 0;
}
6. 小球躲避方块游戏演示
#include <cmath>
#include <time.h>
#include <easyx.h>
#include <conio.h>
#include <graphics.h>
using namespace std;
int main() {
double width, height, gravity;
double ball_x, ball_y, ball_vy, radius;
double rect_left_x, rect_top_y, rect_width, rect_height, rect_vx;
width = 600, height = 400, gravity = 0.6;
initgraph(width, height);
int score = 0;
int isBallOnFloor = 1;
radius = 20;
ball_x = width / 4, ball_y = height - radius;
ball_vy = 0;
rect_height = 100, rect_width = 20;
rect_left_x = width * 3 / 4, rect_top_y = height - radius;
rect_vx = -3;
while (1) {
if (_kbhit()) {
char input = _getch();
if (input == ' '&& isBallOnFloor == 1) {//如果按下空格,且小球在地面就弹起,赋予向上初速度
ball_vy = -17;
isBallOnFloor = 0;
}
}
ball_vy += gravity;
ball_y += ball_vy; // 小球匀速上升运动
if (ball_y >= height - radius ) {//如果出界
ball_vy = 0;
ball_y = height - radius;//调整位置,避免超出地图
isBallOnFloor = 1;
}
rect_left_x += rect_vx;
if (rect_left_x <= 0) {//如果方块到达了最左边
rect_left_x = width;//出现在最右边
score += 1;
rect_height = rand() % int(height / 4) + height / 4;//随机方块高度,且最低height / 4
rect_vx = rand() / float(RAND_MAX) * 4 - 7;//随机速度 且最低速度为 -3
//rand () 产生 0 - RAND_MAX范围的数
//short范围 -32768~32767 RAND_MAX = 0x7fff = 32767
}
//碰撞检测
if ((rect_left_x <= ball_x + radius)
&& (rect_left_x + rect_width >= ball_x - radius)
&& (height - rect_height <= ball_y + radius)
) {
Sleep(100);
score = 0;
}
cleardevice();
fillcircle(ball_x, ball_y, radius);
fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width, height);
TCHAR s[20];
_stprintf_s(s, _T("%d"), score);
settextstyle(40,0,_T("宋体"));
outtextxy(50, 30, s);
Sleep(10);
}
closegraph();
return 0;
}
7. 扇形绘制与颜色设置
#include <cmath>
#include <time.h>
#include <easyx.h>
#include <conio.h>
#include <graphics.h>
using namespace std;
int main() {
double width, height;
width = 900, height = 600;
initgraph(width, height);
double radius = 100, x = 300, y = 300;// 画圆形的框
int left = x - radius, top = y-radius, right = x + radius, bottom = y + radius;
double PI = 3.14159265358;
double stangle = 0, endangle = PI / 4;//1rad = 180 / π = 57.30°
// 起始弧度 和 终止弧度
setbkcolor(WHITE);//先设置颜色、再绘图,才能得到对应颜色的绘制效果。
//setbkcolor(RGB(255,255,255)); // 设置背景颜色为白色 RGB模式
setlinecolor(RED);
setfillcolor(GREEN);
cleardevice();//清空背景 才能给背景上色
circle(y, y, radius);
solidpie(left, top, right, bottom, stangle,endangle);
_getch();
closegraph();
return 0;
}
可以绘制一个彩色拼盘
- 旋转圆盘——动态视觉图
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wJXlne14-1655694620232)(https://i.loli.net/2021/11/01/5sEYRMVNd9mwqXj.png)]
#include <cmath>
#include <time.h>
#include <easyx.h>
#include <conio.h>
#include <graphics.h>
int main() {
double width, height;
width = 900, height = 600;
initgraph(width, height);
double radius = 100, x = 300, y = 300;// 画圆形的框
int left = x - radius, top = y-radius, right = x + radius, bottom = y + radius;
double PI = 3.14159265358;
double stangle = PI / 30, endangle = PI / 20;//黑白的弧度 PI / 60
double stangle_2 = 0, endangle_2 = PI / 30;//红青的弧度
setbkcolor(WHITE);
cleardevice();//清空才能上色
// 起始弧度 和 终止弧度
//画20组即可达成效果
for(int i = 0; i < 20; i++){
setfillcolor(GREEN);
fillpie(left, top, right, bottom, stangle_2, endangle_2);
stangle_2 += PI / 20;
endangle_2 += PI / 20;
setfillcolor(WHITE);
fillpie(left, top, right, bottom, stangle, endangle);
stangle += PI / 20;
endangle += PI / 20;
setfillcolor(RED);
fillpie(left, top, right, bottom, stangle_2, endangle_2);
stangle_2 += PI / 20;
endangle_2 += PI / 20;
setfillcolor(BLACK);
fillpie(left, top, right, bottom, stangle, endangle);
stangle += PI / 20;
endangle += PI / 20;
}
_getch();
closegraph();
return 0;
}
还有同心圆
以及这个
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8taPrO5t-1655694620234)(C:\Users\86187\AppData\Roaming\Typora\typora-user-images\image-20211101193325429.png)]
8. HSV颜色模型
第二个参数是模糊程度,第三个参数是灰暗程度
#include <cmath>
#include <time.h>
#include <easyx.h>
#include <conio.h>
#include <graphics.h>
#include <iostream>
#include <typeinfo>
using namespace std;
int main() {
double width, height;
width = 800, height = 500;
initgraph(width, height);
setbkcolor(WHITE);
cleardevice();
double PI = 3.141592653589793;
int x = 300, y = 300, r = 100;//圆弧的圆心
int left = x - r, top = y - r, right = x + r, bottom = y + r;//外切矩形的左上角和右下角坐标
int step = 10;
COLORREF color;
for (int i = 0; i < 360; i += step) {
color = HSVtoRGB(i, 1, 1);
setfillcolor(color);
fillpie(left, top, right, bottom,
i * PI / 180, (i + step) * PI / 180);
}
_getch();
closegraph();
return 0;
}
9. line 线条的绘制
- 直线逼近曲线,xy对应坐标等距连接即可
#include <cmath>
#include <time.h>
#include <easyx.h>
#include <conio.h>
#include <graphics.h>
int main() {
double width, height;
width = 800, height = 500;
initgraph(width, height);
setbkcolor(YELLOW);
cleardevice();
int len = 300;
int col_x = 100, col_y = 10, col_endx = len + col_x, col_endy = col_y;//横线
int row_x = col_x, row_y = col_y, row_endx = row_x, row_endy = len + row_y;//竖线
setlinecolor(BLACK);
int line_cnt = 10;
for (int i = 1; i < line_cnt; i++) {
line(col_x, col_y, col_endx, col_endy);
col_x += 30;
col_endy += 30;
}
_getch();
closegraph();
return 0;
}
- 然后是 11 ∗ 11 11*11 11∗11棋盘的绘制
#include <cmath>
#include <time.h>
#include <easyx.h>
#include <conio.h>
#include <graphics.h>
int main() {
double width, height;
width = 800, height = 500;
initgraph(width, height);
setbkcolor(YELLOW);
cleardevice();
int len = 300;
int col_x = 100, col_y = 10, col_endx = len + col_x, col_endy = col_y;//竖线
int row_x = col_x, row_y = col_y, row_endx = row_x, row_endy = len + row_y;//横线
setlinecolor(BLACK);
int line_cnt = 10;
for (int i = 0; i <= line_cnt; i++) {
line(col_x, col_y, col_endx, col_endy);
line(row_x, row_y, row_endx, row_endy);
col_y += 30;
col_endy += 30;
row_x += 30;
row_endx += 30;
}
_getch();
closegraph();
return 0;
}