跳动的小球

0.用俩for循环输出小球o

                for(i = 0; i <= x; i++)
			printf("\n");
		for(j = 0; j <= y; j++)
			printf(" ");   
	        printf("o\n");   //输出小球o

1.定义坐标x,y,定义x和y方向的速度,定义各边界

2.x,y不断增加;当小球触及边界时,用if语句使速度反向

                x += velocity_x;
		y += velocity_y;
		if(x == top || x == bottom)
			velocity_x = -velocity_x;
		if(y == left || y == right)
			velocity_y = -velocity_y;

3.while(1)可以使小球不停的跳动

4.system("cls")是清屏,Sleep(50)可以使小球慢下来

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
int main()
{
	int i,j;
	int x = 10;
	int y = 10;
	int velocity_x = 1;
	int velocity_y = 1;
	int top = 1;
	int bottom = 20;
	int left = 1;
	int right = 30;
	// for(x = 1; x <= 10; x++)
	while(1)
	{
		x += velocity_x;
		y += velocity_y;
		if(x == top || x == bottom)
			velocity_x = -velocity_x;
		if(y == left || y == right)
			velocity_y = -velocity_y;	
		system("cls"); //使小球慢下来 
		for(i = 0; i <= x; i++)
			printf("\n");
		for(j = 0; j <= y; j++)
			printf(" ");   
	printf("o\n");   //输出小球o 
	
	Sleep(50);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/netbar4/article/details/79394124