EGE 滚动太极

博客展示了如何用C++编程实现一个滚动的太极动画,通过一张gif动态图直观呈现了效果,适合对C++图形编程感兴趣的读者进一步探索。
摘要由CSDN通过智能技术生成

废话不多说,直接上代码

#include <graphics.h>
#include <cmath>
#include <stdlib.h>
#include<ctime>
using namespace std;
const int numCircle = 10;//小球个数
const int speed = 5;	//速度常量
const int SCREEN_WIDTH  = 640;	
const int SCREEN_HEIGHT = 480;
typedef struct Circle
{
	int r;			//半径
	int cx, cy;		//圆心
	int dx, dy;		//x, y方向上分速度
	float angle;
}Circle;

void initCircle(Circle* circle)
{
	//大小为[40, 60)
	circle->r = 40 + rand() % 20;
	
	//位置限制在窗口内
	circle->cx = circle->r + rand() % (getwidth() - 2 * circle->r);
	circle->cy = circle->r + rand() % (getheight() - 2 * circle->r);
	
	//速度为[-1, 1]
	circle->dx = rand() % 3 , circle->dy = rand() % 3 ;
	circle->angle=0;
}
void updateCircle(Circle* circle)
{
	//位置更新
	circle->cx += circle->dx;
	circle->cy += circle->dy;
	
	//碰撞检测
	if (circle->cx - 2*circle->r <= 0)				circle->dx = speed;		//碰左
	if (circle->cy - 2*circle->r <= 0)				circle->dy = speed;		//碰上
	if (circle->cx>= getwidth() - 1)	circle->dx = -speed;	//碰右
	if (circle->cy >= getheight() - 1)	circle->dy = -speed;	//碰下
	
}
void annulus(float cx, float cy, float outerRad, color_t outerColor, float innerRad, color_t innerColor)
{
	setfillcolor(outerColor);
	ege_fillellipse(cx - outerRad, cy - outerRad, 2 * outerRad, 2 * outerRad);
	setfillcolor(innerColor);
	ege_fillellipse(cx - innerRad, cy - innerRad, 2 * innerRad, 2 * innerRad);
}

//太极
void taichi(float cx, float cy, float radius, float angle)
{
	float left = cx - radius, top = cy - radius;
	float width = 2 * radius, height = 2 * radius;
	color_t colWhite = EGEACOLOR(0xFF, WHITE), colBlack = EGEACOLOR(0xFF, BLACK);
	
	//白半圆
	setfillcolor(colWhite);
	ege_fillpie(left, top, width, height, angle + 90, 180);
	
	//黑半圆
	setfillcolor(colBlack);
	ege_fillpie(left, top, width, height, angle - 90, 180);
	
	//鱼眼中心偏移位置
	float radian = (angle + 90) * PI / 180;
	float dx = radius / 2 * cos(radian), dy = radius / 2 * sin(radian);
	
	//黑鱼头部
	annulus(cx + dx, cy + dy, radius / 2, colBlack, radius / 6, colWhite);
	//白鱼头部
	annulus(cx - dx, cy - dy, radius / 2, colWhite, radius / 6, colBlack);
	
	//太极黑边框
	setlinewidth(2);
	setcolor(colBlack);
	ege_ellipse(left, top, 2 * radius, 2 * radius);
}
void drawCircle(Circle* circle)
{
	//计算颜色
	circle->angle+=PI;		
	taichi(circle->cx - circle->r, circle->cy - circle->r,circle->r,circle->angle);
}
int main()
{
	initgraph(640, 480, INIT_RENDERMANUAL);	//创建窗口,手动渲染
	setbkcolor(WHITE);						//背景色
	setcolor(WHITE);						//前景色(在这里就只和文字颜色有关)
	setbkmode(TRANSPARENT);					//文字背景色透明
	setfont(16, 0, "黑体");					//设置字体
	srand(time(NULL));						//随机数种子

	Circle circle[numCircle];

	//开启抗锯齿
	ege_enable_aa(true);
		
	//初始化
	for (int i = 0; i < numCircle; i++)
		initCircle(&circle[i]);
	
	for (; is_run(); delay_fps(60))
	{
		//更新
		for (int i = 0; i < numCircle; i++) {
			updateCircle(&circle[i]);
		}
		
		//清屏
		cleardevice();
		
		//绘制
		for (int i = 0; i < numCircle; i++) {
			drawCircle(&circle[i]);
		}
		
		xyprintf(0, 0, "FPS:%.3f", getfps());			//显示帧率
	}
	
	closegraph();
	
	return 0;
}
//
碰撞检测
//if (cx - 2*r <= 0) dx = speed;				//碰左
//if (cy - 2*r <= 0) dy = speed;				//碰上
//if (cx >= getwidth() - 1) dx = -speed;	//碰右
//if (cy >= getheight() - 1) dy = -speed; //碰下
//
//
//cleardevice();
//
//setfillcolor(BLACK);			//设置颜色
//taichi(cx-r,cy-r,r,angle);
//angle += PI ;

来一张gif:

在这里插入图片描述

有兴趣的童鞋可以在看看这篇文章

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江河湖海 

最近手头有点紧,感谢你给我鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值