你是在说弹跳小球的边框吗?为了不受清屏影响我每个while循环一次就重画一次,这样就会随着小球弹跳边框不停闪烁
你有找到什么好方法吗?
我的代码在下面,大家可以找找办法如何简化?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <windows.h>
#include <time.h>
void delay(int number_of_seconds)
{
// Converting time into centi_seconds
int centi_seconds = 100 * number_of_seconds;
// Storing start time
clock_t start_time = clock();
// looping till required time is not achieved
while (clock() < start_time + centi_seconds);
}
int main()
{
int i,j;
int x = 10;
int y = 15;
int velocity_x = 1;
int velocity_y = 1;
int left = 1;
int right = 20;
int top = 1;
int bottom = 20;
while (1)
{
x = x + velocity_x;
y = y + velocity_y;
system("cls");
for(int i=0; i<=right; i++) // print first"---------"
{
printf("-");
}
printf("\n");
for (i=1; i<x; i++) //print"| |"for lines before the ball
{
printf("|");
for (j=1; j<right+1; j++)
{
printf(" ");
}
printf("|\n");
}
printf("|"); // print"| o |"
for (j=1; j<y; j++)
{
printf(" ");
}
printf("o"); // print ball at line (y,x)
delay(1); // delay for 100 cs after each print of the ball
for (j=y; j<right; j++)
{
printf(" ");
}
printf("|\n");
for (i=x; i<bottom; i++) //print"| |"for lines after the ball
{
printf("|");
for (j=1; j<right+1; j++)
{
printf(" ");
}
printf("|\n");
}
for(int i=0; i<=right; i++) // print last"---------"
{
printf("-");
}
printf("\n");
if ((x==bottom) || (x==top))
velocity_x = -velocity_x;
printf("\a");
if ((y==right) || (y==left))
velocity_y = -velocity_y;
printf("\a");
}
return 0;
}