赞
踩
#include<graphics.h> #include<conio.h> #include<windows.h> int main() { initgraph(640, 480); for(int x = 100; x < 540; x += 20) { setcolor(RED); setfillcolor(YELLOW); fillcircle(x, 100, 20); Sleep(200); setcolor(BLACK); //擦掉之前的球 setfillcolor(BLACK); fillcircle(x, 100, 20); } closegraph(); return 0; }
移动的间距小、延时短,动画就会越细腻。但当画面较复杂时,会带来画面的闪烁(怎样消除闪烁是以后的话题)。
#include<graphics.h> #include<conio.h> void main() { initgraph(640, 480); setlinestyle(PS_SOLID, 5); //画实线,宽度为两像素 for(int y = 0; y < 480; y++) { // 绘绿色线 setcolor(GREEN); line(0, y, 639, y); // 延时 Sleep(10); // 绘黑色线(即擦掉先前画的绿线) setcolor(BLACK); line(0, y, 639, y); } closegraph(); }
#include<graphics.h> #include<conio.h> #include<windows.h> #define high 480.0 #define width 640.0 int main() { float x, y; float vx, vy; float radius; initgraph(width, high); x = width / 2.0; y = high / 2.0; vx = 1.0; vy = 1.0; radius = 20.0; while(1) { setcolor(BLACK); setfillcolor(BLACK); fillcircle(x, y, radius); x = x + vx; y = y + vy; if((x < radius) || (x >= width - radius)) vx = -vx; if((y <= radius) || (y >= high - radius)) vy = -vy; setcolor(YELLOW); setfillcolor(RED); fillcircle(x, y, radius); Sleep(3); } closegraph(); return 0; }
sleep()函数延时越小,动画越细腻!!!
#include<graphics.h> #include<conio.h> #include<windows.h> #define high 480.0 #define width 640.0 int main() { float x, y; float vx, vy; float radius; initgraph(width, high); x = width / 2.0; y = high / 2.0; vx = 1.0; vy = 1.0; radius = 20.0; BeginBatchDraw(); //开始批量绘 while(1) { setcolor(BLACK); setfillcolor(BLACK); fillcircle(x, y, radius); x = x + vx; y = y + vy; if((x < radius) || (x >= width - radius)) vx = -vx; if((y <= radius) || (y >= high - radius)) vy = -vy; setcolor(YELLOW); setfillcolor(RED); fillcircle(x, y, radius); FlushBatchDraw(); //批量绘 Sleep(2); } EndBatchDraw(); //结束批量绘 closegraph(); return 0; }
#include<graphics.h> #include<conio.h> #include<windows.h> #define high 480 #define width 640 #define Num 5 int main() { float x[Num], y[Num]; float vx[Num], vy[Num]; float radius; int i; radius = 20; for(i = 0; i < Num; i++) { x[i] = (i + 2) * radius * 3; y[i] = high / 2; vx[i] = 1; vy[i] = 1; } initgraph(width, high); BeginBatchDraw(); while(1) { setcolor(BLACK); setfillcolor(BLACK); for(i = 0; i < Num; i++) fillcircle(x[i], y[i], radius); for(i = 0; i < Num; i++) { x[i] = x[i] + vx[i]; y[i] = y[i] + vy[i]; } for(i = 0; i < Num; i++) { if(x[i] <= radius || x[i] >= width - radius) vx[i] = -vx[i]; if(y[i] <= radius || y[i] >= high - radius) vy[i] = -vy[i]; } setcolor(YELLOW); setfillcolor(RED); for(i = 0; i < Num; i++) fillcircle(x[i], y[i], radius); FlushBatchDraw(); Sleep(2); } EndBatchDraw(); closegraph(); return 0; }
#include<graphics.h> #include<conio.h> #include<math.h> #include<windows.h> #define high 480 //长 #define width 640 //宽 #define Num 5 //球的数目 int main() { float x[Num], y[Num]; //球的方位 float vx[Num], vy[Num]; //球的速度 float radius; //球的半径 int i, j; radius = 20; for(i = 0; i < Num; i++) { x[i] = rand() % int(width - 4 * radius) + 2 * radius; y[i] = rand() % int(high - 4 * radius) + 2 * radius; vx[i] = (rand() % 2) * 2 - 1; vy[i] = (rand() % 2) * 2 - 1; } initgraph(width, high); BeginBatchDraw(); while(1) { setcolor(BLACK); setfillcolor(BLACK); for(i = 0; i < Num; i++) fillcircle(x[i], y[i], radius); for(i = 0; i < Num; i++) //不让小球超出边界 { x[i] += vx[i]; y[i] += vy[i]; if(x[i] < radius) x[i] = radius; if(y[i] < radius) y[i] = radius; if(x[i] > width - radius) x[i] = width - radius; if(y[i] > high - radius) y[i] = high - radius; } for(i = 0; i < Num; i++) //是否跟墙碰撞 { if(x[i] <= radius || x[i] >= width - radius) vx[i] = -vx[i]; if(y[i] <= radius || y[i] >= high - radius) vy[i] = -vy[i]; } float min[Num][2]; //两小球间的min距离,以及几号球 for(i = 0; i < Num; i++) { min[i][0] = 9999999; min[i][1] = -1; } for(i = 0; i < Num; i++) { for(j = 0; j < Num; j++) { if(i != j) { float dist2; //两球距离平方和 dist2 = (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]); if(dist2 < min[i][0]) { min[i][0] = dist2; //i球和j球min距离 min[i][1] = j; } } } } for(i = 0; i < Num; i++) { if(min[i][0] <= 2 * radius * radius) { //min距离小于阈value,就发生碰撞 //由于编辑器乱码,所以有些字我不得不用英文代替,还请见谅 j = min[i][1]; int tmp; tmp = vx[i]; vx[i] = vx[j]; vx[j] = tmp; tmp = vy[i]; vy[i] = vy[j]; vy[j] = tmp; min[j][0] = 9999999; //防速度又交换回来 min[j][1] = -1; } } setcolor(YELLOW); setfillcolor(RED); for(i = 0; i < Num; i++) fillcircle(x[i], y[i], radius); FlushBatchDraw(); Sleep(2); } EndBatchDraw(); closegraph(); return 0; }
#include<graphics.h> #include<conio.h> #include<math.h> #include<windows.h> #define high 480 //长 #define width 640 //宽 #define Num 5 //球的数目 int main() { float x[Num], y[Num]; //球的方位 float vx[Num], vy[Num]; //球的速度 float radius; //球的半径 int i, j; radius = 20; for(i = 0; i < Num; i++) { x[i] = rand() % int(width - 4 * radius) + 2 * radius; y[i] = rand() % int(high - 4 * radius) + 2 * radius; vx[i] = (rand() % 2) * 2 - 1; vy[i] = (rand() % 2) * 2 - 1; } initgraph(width, high); BeginBatchDraw(); setbkcolor(YELLOW); while(1) { setcolor(YELLOW); setfillcolor(YELLOW); for(i = 0; i < Num; i++) fillcircle(x[i], y[i], radius); for(i = 0; i < Num; i++) //不让小球超出边界 { x[i] += vx[i]; y[i] += vy[i]; if(x[i] < radius) x[i] = radius; if(y[i] < radius) y[i] = radius; if(x[i] > width - radius) x[i] = width - radius; if(y[i] > high - radius) y[i] = high - radius; } for(i = 0; i < Num; i++) //是否跟墙碰撞 { if(x[i] <= radius || x[i] >= width - radius) vx[i] = -vx[i]; if(y[i] <= radius || y[i] >= high - radius) vy[i] = -vy[i]; } float min[Num][2]; //两小球间的min距离,以及几号球 for(i = 0; i < Num; i++) { min[i][0] = 9999999; min[i][1] = -1; } for(i = 0; i < Num; i++) { for(j = 0; j < Num; j++) { if(i != j) { float dist2; //两球距离平方和 dist2 = (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]); if(dist2 < min[i][0]) { min[i][0] = dist2; //i球和j球min距离 min[i][1] = j; } } } } for(i = 0; i < Num; i++) { if(min[i][0] <= 2 * radius * radius) { //min距离小于阈value,就发生碰撞 //由于编辑器乱码,所以有些字我不得不用英文代替,还请见谅 j = min[i][1]; int tmp; tmp = vx[i]; vx[i] = vx[j]; vx[j] = tmp; tmp = vy[i]; vy[i] = vy[j]; vy[j] = tmp; min[j][0] = 9999999; //防速度又交换回来 min[j][1] = -1; } } setcolor(CYAN); setfillcolor(GREEN); for(i = 0; i < Num; i++) fillcircle(x[i], y[i], radius); FlushBatchDraw(); Sleep(2); } EndBatchDraw(); closegraph(); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。