1.上下弹跳
#include<stdio.h>
#include<stdlib.h>
int main()
{
int position_x,position_y;//小球坐标
int i,j;
int height,width;//画布的大小
int velocity=1;
height=20;
width=30;
position_x=10;
position_y=0;
j=0;
while(position_y<height)
{
system("cls");
for(i=0;i<position_y;i++)
printf("\n");
for(i=0;i<position_x;i++)
printf(" ");
printf("O\n");
j++;
position_y++;
}
system("pause");
return 0;
}
2.上下左右弹跳
#include<stdio.h>
#include<stdlib.h>
int main()
{
int position_x,position_y;//小球坐标
int i;
int height,width;//画布的大小
//int left,right;
int velocity_x,velocity_y;
height=20;
width=30;
//position_x=10;
position_x=1;
position_y=1;
velocity_x=1;
velocity_y=1;
while(1)
{
if(position_y>height||position_y<1)
velocity_y=(-1)*velocity_y;
if(position_x>width||position_x<1)
velocity_x=(-1)*velocity_x;
position_y=position_y+velocity_y;
position_x=position_x+velocity_x;
system("cls");
for(i=0;i<position_y;i++)
printf("\n");
for(i=0;i<position_x;i++)
printf(" ");
printf("O\n");
}
system("pause");
return 0;
}
用到了分解的思想:先画一个小球,然后让他上下弹跳,最后让他上下左右弹跳。