EGE 滚动太极
废话不多说,直接上代码
#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:
有兴趣的童鞋可以在看看这篇文章
本文来自博客园,作者:I'm_江河湖海,转载请注明原文链接:https://www.cnblogs.com/jhhh/p/16760636.html