很多萌新都会纠结同时让多个物体流畅运动的问题, 这个问题跟多线程可没关系, 如果遇到困惑, 看看这个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; } |
下面是显示效果:
近期评论