代码有些简陋,后期还要修改一下
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<graphics.h>
#include<easyx.h>
main()
{
int x=0;
int y=5;
int v_x=1;
int v_y=1;
int left=0;
int right=20;
int top=0;
int bottom=20;
while(1)
{
x=x+v_x;
y=y+v_y;
system("cls");
for(int i=0;i<=right;i++)
printf("-");
printf("\n");
for(int i=0;i<x;i++)
{
for(int j=0;j<right;j++)
{
if(j==0||j==right-1)
printf("||");
else
{
printf(" ");
}
}
printf("\n");
}
for(int j=0;j<y;j++)
{
if(j==0||j==right-2)
printf("||");
else
{
printf(" ");
}
}
printf("o");
printf("\n");
for(int i=0;i<bottom-x;i++)
{
for(int j=0;j<right;j++)
{
if(j==0||j==right-1)
{
printf("||");
}
else
{
printf(" ");
}
}
printf("\n");
}
for(int j=0;j<=right;j++)
printf("-");
if(x==top||x==bottom)
{
// printf("\a");
v_x=-v_x;
}
if(y==left||y==right)
{
// printf("\a");
v_y=-v_y;
}
Sleep(100);
}
}
发现原来代码太复杂,所以有精简了一下
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
main()
{
int x=5;
int y=5;
int left=1;
int right=20;
int top=1;
int bottom=20;
int v=1;
int v2=2;
while(1)
{
x=x+v;
y=y+v2;
printf("%d,%d\n",x,y);
system("cls");
for(int i=1;i<=bottom;i++)
{
for(int j=1;j<=right;j++)
{
if(i==x&&j==y)
{
printf("o");
}
else if((j==1&&(i!=1&&i!=bottom))||(j==right&&(i!=1&&i!=bottom)))
printf("||");
else if(i==1||i==bottom)
printf("-");
else
printf(" ");
}
printf("\n");
}
printf("\n");
if(x>=bottom-1||x<=top+1)
v=-v;
if(y>=right-1||y<=left+1)
v2=-v2;
}
}