发布时间:2023-03-06 16:00
本文实例为大家分享了C语言实现弹跳小球动画的具体代码,供大家参考,具体内容如下
项目描述: 一个球来回的跳动
效果图如下:
代码如下:
#include#include 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);//绘制黑线,黑色填充的圆 //目的清除以前的圆,达到小球动态移动的效果 //等价于清屏函数 setfillcolor(BLACK); fillcircle(x,100,20);*/ cleardevice(); } closegraph(); return 0; }
代码如下:
#include#include #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);//绘制黑色,黑色填充的圆 //不用清屏函数的好处:不会闪屏 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#include #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);//绘制黑色,黑色填充的圆 //不用清屏函数的好处:不会闪屏 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#include #define High 480 //游戏画布尺寸 #define Width 640 #define BallNum 5 //小球的个数 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 =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
效果图如下:
增加了球与球之间的碰撞
代码如下:
#include#include #include #define High 480 //游戏画面大小 #define Width 640 #define BallNum 15//小球的个数 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 Width-radius) ball_x[i]=Width-radius; if(ball_y[i]>High-radius) ball_y[i]=High-radius; } //判断是否和墙壁碰撞 for(i=0;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
效果图如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。