C語言實現(xiàn)彈跳小球動畫
更新時間:2022年05月12日 16:07:25 作者:輝小歌
這篇文章主要為大家詳細介紹了C語言實現(xiàn)彈跳小球動畫,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C語言實現(xiàn)彈跳小球動畫的具體代碼,供大家參考,具體內(nèi)容如下
一、項目描述和最終成果展示
項目描述: 一個球來回的跳動
效果圖如下:
二、實現(xiàn)一個移動的球
代碼如下:
#include<graphics.h> #include<conio.h> int main(void) { ?? ?int x; ?? ?initgraph(640,480); ?? ?for(x=100;x<540;x=x+20) ?? ?{ ?? ??? ?setcolor(YELLOW);//繪制黃線,綠色填充的圓 ?? ??? ?setfillcolor(GREEN); ?? ??? ?fillcircle(x,100,20); ?? ??? ?Sleep(320);//延時 ?? ??? ?/*setcolor(BLACK);//繪制黑線,黑色填充的圓 ?? ??? ?//目的清除以前的圓,達到小球動態(tài)移動的效果 ?? ??? ?//等價于清屏函數(shù) ?? ??? ?setfillcolor(BLACK); ?? ??? ?fillcircle(x,100,20);*/ ?? ??? ?cleardevice(); ?? ?} ?? ?closegraph(); ?? ?return 0; }
三、彈跳一個小球
代碼如下:
#include<graphics.h> #include<conio.h> #define High 480 //游戲畫布尺寸 #define Width 640 int main(void) { ?? ?float ball_x,ball_y;//小球的坐標 ?? ?float ball_vx,ball_vy;//小球的速度 ?? ?float radius;//小球的半徑 ?? ?initgraph(Width,High); ?? ?ball_x=Width/2; ?? ?ball_y=High/2; ?? ?ball_vx=1; ?? ?ball_vy=1; ?? ?radius=20; ?? ?while(1) ?? ?{ ?? ??? ?setcolor(BLACK);//繪制黑色,黑色填充的圓 ?? ??? ?//不用清屏函數(shù)的好處:不會閃屏 ?? ??? ?setfillcolor(BLACK); ?? ??? ?fillcircle(ball_x,ball_y,radius); ?? ??? ?//更新圓的坐標 ?? ??? ?ball_x=ball_x+ball_vx; ?? ??? ?ball_y=ball_y+ball_vy; ?? ??? ?if( (ball_x<=radius) || (ball_x>=Width-radius) ) ?? ??? ??? ?ball_vx=-ball_vx; ?? ??? ?if( (ball_y<=radius) || (ball_y>=High-radius) ) ?? ??? ??? ?ball_vy=-ball_vy; ?? ??? ?//繪制黃線,綠色填充的圓 ?? ??? ?setcolor(YELLOW); ?? ??? ?setfillcolor(GREEN); ?? ??? ?fillcircle(ball_x,ball_y,radius); ?? ??? ?Sleep(3); ?? ?} ?? ?closegraph(); ?? ?return 0; }
效果圖如下:
四、彈跳一個小球(改進版)
代碼如下:
#include<graphics.h> #include<conio.h> #define High 480 //游戲畫布尺寸 #define Width 640 int main(void) { ?? ?float ball_x,ball_y;//小球的坐標 ?? ?float ball_vx,ball_vy;//小球的速度 ?? ?float radius;//小球的半徑 ?? ?initgraph(Width,High); ?? ?ball_x=Width/2; ?? ?ball_y=High/2; ?? ?ball_vx=1; ?? ?ball_vy=1; ?? ?radius=20; ?? ?BeginBatchDraw(); ?? ?while(1) ?? ?{ ?? ??? ?setcolor(BLACK);//繪制黑色,黑色填充的圓 ?? ??? ?//不用清屏函數(shù)的好處:不會閃屏 ?? ??? ?setfillcolor(BLACK); ?? ??? ?fillcircle(ball_x,ball_y,radius); ?? ??? ?//更新圓的坐標 ?? ??? ?ball_x=ball_x+ball_vx; ?? ??? ?ball_y=ball_y+ball_vy; ?? ??? ?if( (ball_x<=radius) || (ball_x>=Width-radius) ) ?? ??? ??? ?ball_vx=-ball_vx; ?? ??? ?if( (ball_y<=radius) || (ball_y>=High-radius) ) ?? ??? ??? ?ball_vy=-ball_vy; ?? ??? ?//繪制黃線,綠色填充的圓 ?? ??? ?setcolor(YELLOW); ?? ??? ?setfillcolor(GREEN); ?? ??? ?fillcircle(ball_x,ball_y,radius); ?? ??? ?FlushBatchDraw(); ?? ??? ?Sleep(3); ?? ?} ?? ?EndBatchDraw(); ?? ?closegraph(); ?? ?return 0; }
效果圖如下:
五、多個球碰撞
代碼如下:
#include<graphics.h> #include<conio.h> #define High 480 //游戲畫布尺寸 #define Width 640 #define BallNum 5 //小球的個數(shù) int main(void) { ?? ?float ball_x[BallNum],ball_y[BallNum];//小球的坐標 ?? ?float ball_vx[BallNum],ball_vy[BallNum];//小球的速度 ?? ?float radius=20;//小球的半徑 ?? ?int i; ?? ?for(i=0;i<BallNum;i++) ?? ?{ ?? ??? ?ball_x[i]=(i+2)*radius*3; ?? ??? ?ball_y[i]=High/2; ?? ??? ?ball_vx[i]=1; ?? ??? ?ball_vy[i]=1; ?? ?} ?? ?initgraph(Width,High); ?? ?BeginBatchDraw(); ?? ?while(1) ?? ?{ ?? ??? ?setcolor(BLACK);//繪制黑色,黑色填充的圓 ?? ??? ?//不用清屏函數(shù)的好處:不會閃屏 ?? ??? ?setfillcolor(BLACK); ?? ??? ?for(i=0;i<BallNum;i++) ?? ??? ?{ ?? ??? ??? ?fillcircle(ball_x[i],ball_y[i],radius); ?? ??? ?} ?? ??? ?//更新圓的坐標 ?? ??? ?for(i=0;i<BallNum;i++) ?? ??? ?{ ?? ??? ??? ?ball_x[i]=ball_x[i]+ball_vx[i]; ?? ??? ??? ?ball_y[i]=ball_y[i]+ball_vy[i]; ?? ??? ?} ?? ??? ?//判斷是否和墻壁碰撞 ?? ??? ?for(i=0;i<BallNum;i++) ?? ??? ?{ ?? ??? ??? ?if( (ball_x[i]<=radius) || (ball_x[i]>=Width-radius) ) ?? ??? ??? ??? ?ball_vx[i]=-ball_vx[i]; ?? ??? ??? ?if( (ball_y[i]<=radius) || (ball_y[i]>=High-radius) ) ?? ??? ??? ??? ?ball_vy[i]=-ball_vy[i]; ?? ??? ?} ?? ??? ?//繪制黃線,綠色填充的圓 ?? ??? ?setcolor(YELLOW); ?? ??? ?setfillcolor(GREEN); ?? ??? ?for(i=0;i<BallNum;i++) ?? ??? ?{ ?? ??? ??? ?fillcircle(ball_x[i],ball_y[i],radius); ?? ??? ?} ?? ??? ?FlushBatchDraw(); ?? ??? ?Sleep(3); ?? ?} ?? ?EndBatchDraw(); ?? ?closegraph(); ?? ?return 0; }
效果圖如下:
六、多個球碰撞(升級版)
增加了球與球之間的碰撞
代碼如下:
#include<graphics.h> #include<conio.h> #include<math.h> #define High 480 ?//游戲畫面大小 #define Width 640 #define BallNum 15//小球的個數(shù) int main(void) { ?? ?float ball_x[BallNum],ball_y[BallNum];//小球的坐標 ?? ?float ball_vx[BallNum],ball_vy[BallNum];//小球的速度 ?? ?float radius;//小球的半徑 ?? ?int i,j; ?? ?radius=20; ?? ?for(i=0;i<BallNum;i++)//數(shù)據(jù)的初始化 ?? ?{ ?? ??? ?ball_x[i]=rand()%int(Width-4*radius)+2*radius; ?? ??? ?ball_y[i]=rand()%int(High-4*radius)+2*radius; ?? ??? ?ball_vx[i]=(rand()%2)*2-1; ?? ??? ?ball_vy[i]=(rand()%2)*2-1; ?? ?} ?? ?initgraph(Width,High); ?? ?BeginBatchDraw(); ?? ?while(1) ?? ?{ ?? ??? ?setcolor(BLACK);//繪制黑色,黑色填充的圓 ?? ??? ?//不用清屏函數(shù)的好處:不會閃屏 ?? ??? ?setfillcolor(BLACK); ?? ??? ?for(i=0;i<BallNum;i++) ?? ??? ?{ ?? ??? ??? ?fillcircle(ball_x[i],ball_y[i],radius); ?? ??? ?} ?? ??? ?//更新小球的坐標 ?? ??? ?for(i=0;i<BallNum;i++) ?? ??? ?{ ?? ??? ??? ?ball_x[i]=ball_x[i]+ball_vx[i]; ?? ??? ??? ?ball_y[i]=ball_y[i]+ball_vx[i]; ?? ??? ??? ?//把超出邊界的小球拉回來 ?? ??? ??? ?if(ball_x[i]<radius) ?? ??? ??? ??? ?ball_x[i]=radius; ?? ??? ??? ?if(ball_y[i]<radius) ?? ??? ??? ??? ?ball_y[i]=radius; ?? ??? ??? ?if(ball_x[i]>Width-radius) ?? ??? ??? ??? ?ball_x[i]=Width-radius; ?? ??? ??? ?if(ball_y[i]>High-radius) ?? ??? ??? ??? ?ball_y[i]=High-radius; ?? ??? ?} ?? ??? ?//判斷是否和墻壁碰撞 ?? ??? ?for(i=0;i<BallNum;i++) ?? ??? ?{?? ? ?? ??? ??? ?if( (ball_x[i]<=radius) || (ball_x[i]>=Width-radius) ) ?? ??? ??? ??? ?ball_vx[i]=-ball_vx[i]; ?? ??? ??? ?if( (ball_y[i]<=radius) || (ball_y[i]>=High-radius) ) ?? ??? ??? ??? ?ball_vy[i]=-ball_vy[i]; ?? ??? ?} ?? ??? ?float minDistances2[BallNum][2]; ?? ??? ?//記錄某個小球和與它最近小球的距離,以及這個小球的坐標 ?? ??? ?for(i=0;i<BallNum;i++) ?? ??? ?{ ?? ??? ??? ?minDistances2[i][0]=999999; ?? ??? ??? ?minDistances2[i][1]=-1; ?? ??? ?} ?? ??? ?//求所有小球兩兩之間的距離的平方 ?? ??? ?for(i=0;i<BallNum;i++) ?? ??? ?{ ?? ??? ??? ?for(j=0;j<BallNum;j++) ?? ??? ??? ?{ ?? ??? ??? ??? ?if(i!=j)//和自己不比 ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?float dist2; ?? ??? ??? ??? ??? ?dist2=(ball_x[i]-ball_x[j])*(ball_x[i]-ball_x[j]) ?? ??? ??? ??? ??? ??? ?+(ball_y[i]-ball_y[j])*(ball_y[i]-ball_y[j]); ?? ??? ??? ??? ??? ?if(dist2<minDistances2[i][0]) ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?minDistances2[i][0]=dist2; ?? ??? ??? ??? ??? ??? ?minDistances2[i][1]=j; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ??? ?//判斷小球之間是否碰撞 ?? ??? ?for(i=0;i<BallNum;i++) ?? ??? ?{ ?? ??? ??? ?if(minDistances2[i][0]<=4*radius*radius) ?? ??? ??? ?//若最小距離小于閾值,發(fā)生碰撞 ?? ??? ??? ?{ ?? ??? ??? ??? ?j=minDistances2[i][1]; ?? ??? ??? ??? ?//交換速度 ?? ??? ??? ??? ?int temp; ?? ??? ??? ??? ?temp=ball_vx[i];ball_vx[i]=ball_vx[j];ball_vx[j]=temp; ?? ??? ??? ??? ?temp=ball_vy[i];ball_vy[i]=ball_vy[j];ball_vy[j]=temp; ?? ??? ??? ??? ?minDistances2[j][0]=9999999;//避免交換兩次速度,又回去了 ?? ??? ??? ??? ?minDistances2[j][1]=-1; ?? ??? ??? ?} ?? ??? ?}?? ? ?? ??? ?//繪制黃線,綠色填充的圓 ?? ??? ?setcolor(YELLOW); ?? ??? ?setfillcolor(GREEN); ?? ??? ?for(i=0;i<BallNum;i++) ?? ??? ?{ ?? ??? ??? ?fillcircle(ball_x[i],ball_y[i],radius); ?? ??? ?} ?? ??? ?FlushBatchDraw(); ?? ??? ?Sleep(3); ?? ?} ?? ?EndBatchDraw(); ?? ?closegraph(); ?? ?return 0; }
效果圖如下:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++中常見容器類的使用方法詳解(vector/deque/map/set)
C++中常見的容器類有vector、list、deque、map、set、unordered_map和unordered_set。下面將舉例直接說明各個容器的使用方法,希望對大家有所幫助2023-03-03