弹力球小程序C语言实现
发布日期:2021-05-10 20:12:18 浏览次数:19 分类:精选文章

本文共 2694 字,大约阅读时间需要 8 分钟。

静止小球

#include
int main(){ int i, j; int x = 5; int y = 10; for(i = 0; i < x; i++) printf("\n"); for(j = 0; j < y; j++) printf(" "); printf("o\n"); return 0; }

运行结果如下:

在这里插入图片描述

下落小球

#include
#include
int main(){ int i, j; int x; int y = 10; for(x = 1; x < 10; x++) { system("cls"); //清屏函数 for(i = 0; i < x; i++) printf("\n"); for(j = 0; j < y; j++) printf(" "); printf("o\n"); } return 0;}

上下弹跳小球

这里加入了速度变量velocity,如果小球到达上或下边界,则改变velocity正负号

#include
#include
int main(){ int i, j; int x = 5; int y = 10; int height = 30; int velocity = 1; while(1) { x += velocity; system("cls"); for(i = 0; i < x; i++) printf("\n"); for(j = 0; j < y; j++) printf(" "); printf("o"); printf("\n"); if(x == height) velocity = -velocity; if(x == 0) velocity = -velocity; } return 0;}

这是弹跳效果,传送门——

上下弹跳小球

斜着跳动的小球

这里我们分别添加了x,y方向上的速度变量,使得模型更加科学

#include
#include
int main(){ int i, j; int x = 0; int y = 5; int velocity_x = 1; int velocity_y = 1; int left = 0; int right = 50; int top = 0; int bottom = 30; while(1) { x += velocity_x; y += velocity_y; system("cls"); for(i = 0; i < x; i++) printf("\n"); for(j = 0; j < y; j++) printf(" "); printf("o\n"); if((x == top) || (x == bottom)) velocity_x = -velocity_x; if((y == left) || (y == right)) velocity_y = -velocity_y; } return 0; }

如果想看运行状态,这里是传送门:

斜着弹跳的小球

同时,如果添加sleep()函数可以控制小球弹跳的速度

#include
#include
#include
int main(){ int i, j; int x = 0; int y = 5; int velocity_x = 1; int velocity_y = 1; int left = 0; int right = 50; int top = 0; int bottom = 30; while(1) { x += velocity_x; y += velocity_y; system("cls"); for(i = 0; i < x; i++) printf("\n"); for(j = 0; j < y; j++) printf(" "); printf("o\n"); sleep(1); if((x == top) || (x == bottom)) velocity_x = -velocity_x; if((y == left) || (y == right)) velocity_y = -velocity_y; } return 0; }

为了生动点我们可以添加响铃效果

#include
#include
#include
int main(){ int i, j; int x = 0; int y = 5; int velocity_x = 1; int velocity_y = 1; int left = 0; int right = 50; int top = 0; int bottom = 30; while(1) { x += velocity_x; y += velocity_y; system("cls"); for(i = 0; i < x; i++) printf("\n"); for(j = 0; j < y; j++) printf(" "); printf("o\n"); //sleep(1); if((x == top) || (x == bottom)) { velocity_x = -velocity_x; printf("\a"); } if((y == left) || (y == right)) { velocity_y = -velocity_y; printf("\a"); } } return 0; }

传送门

模拟小球弹跳

如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持,明天我们不见不散!!!

上一篇:打飞机小游戏C语言实现
下一篇:2020Java程序设计基础(华东交通大学)章节测试免费满分答案

发表评论

最新留言

感谢大佬
[***.8.128.20]2025年04月10日 20时45分57秒