#include<stdio.h> #include<stdlib.h> #include<windows.h> void main(){ int i, j; int x = 0; int y = 5; int velocity_x = 1; int velocity_y = 1; int left = 0; int right = 20; int top = 0; int bottom = 10; while(1){ x += velocity_x; y += velocity_y; system("cls"); //清屏函数,在stdlib.h中 for(i = 0; i < y; i++) //输出小球前面的空行和空格 printf("\n"); for(j = 0; j < x; j++) printf(" "); printf("o"); //输出小球 printf("\n"); Sleep(50); //等待若干毫秒,控制小球弹跳的速度,windows.harderr if((y <= top) || (y >= bottom)){ velocity_y = -velocity_y; printf("\a"); //碰到边界,实现响铃 } if((x <= left) || (x >= right)){ velocity_x = -velocity_x; printf("\a"); } } }