弹力球小程序C语言实现

静止小球

#include<stdio.h>

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<stdio.h>
#include<stdlib.h>

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<stdio.h>
#include<stdlib.h>

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<stdio.h>
#include<stdlib.h>

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<stdio.h>
#include<stdlib.h>
#include<windows.h>

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<stdio.h>
#include<stdlib.h>
#include<windows.h>

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;
 } 

传送门

模拟小球弹跳

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

  • 7
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值