分享

新手入门『五』使用结构体, 让多个图形同时运动 – Easy Graphics Engine

 我就是个蒟蒻 2020-08-08

很多萌新都会纠结同时让多个物体流畅运动的问题, 这个问题跟多线程可没关系, 如果遇到困惑, 看看这个demo怎么写的吧!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <graphics.h>
#include <math.h>
#define SCR_WIDTH      800
#define SCR_HEIGHT     600
struct Ball {
float x,y;
float dx,dy;
float radius;
color_t color;
};
int main() {
initgraph(SCR_WIDTH, SCR_HEIGHT, INIT_RENDERMANUAL);
Ball ball[500];
float dir = 0.0f;
float sp = 0.5f;
for(int i = 0; i != 500; ++i){
ball[i].x = SCR_WIDTH / 2;
ball[i].y = SCR_HEIGHT / 2;
randomize();
sp += 0.01f;
ball[i].dx = sp * cosf(dir);
ball[i].dy = sp * sinf(dir);
ball[i].radius = randomf() * 5 + 2;
ball[i].color = random(0xff0000) + 0xffff;
dir += 0.1f;
}
int n = 1;
double t = fclock();
for(; is_run(); delay_fps(60)) {
cleardevice();
if (kbhit()) {
if (getch() == 27)
break;
n += 63;
n %= 500;
}
if(fclock() - t > 0.2) {
t = fclock();
if (n < 500) ++n;
}
for(int i = 0; i != n; ++i) {
setfillcolor(ball[i].color);
setcolor(ball[i].color);
fillellipse(ball[i].x,ball[i].y,ball[i].radius,ball[i].radius);
ball[i].x += ball[i].dx;
ball[i].y += ball[i].dy;
if(ball[i].x < 0.0f || ball[i].x > SCR_WIDTH) ball[i].dx = -ball[i].dx;
if(ball[i].y < 0.0f || ball[i].y > SCR_HEIGHT) ball[i].dy = -ball[i].dy;
}
}
closegraph();
return 0;
}

下面是显示效果:

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多