新手上路之小球弹框框
# include <graphics.h># include <conio.h>
# include <stdlib.h>
# include <math.h>
# include <stdio.h>
int main()
{
int k;//k-表示运动路径
int x0, y0;//(x0,y0)当前路径的始发点
int x1, y1;//(x1,y1)当前路径的终止点
int x00,y00;//(x00,y00)上一路径的始发点
int x11,y11;//(x11,y11)一一路径的终止点
int b;//x轴上的截距
int kk;
int x, y;//画圆用
for (k=1;k<=100;k++)
{
if (k%2==0)
kk = -1;
else
kk = 1;
/*-----------------*/
//将上一步的起始点保存到x00,y00,x11,y11里
if (k==1)
{
x00 = 50;y00 = 50;
x11 = 60;y11 = 60;
}
else
{
x00 = x0;y00 = y0;
x11 = x1;y11 = y1;
}
/*-----------------*/
//k==2时,将上一路径的终点
//作为当前路径的起点
if (k==1)
{
x0 = 50;y0 = 50;
}
else
{
x0 = x1;y0 = y1;
}
/*-----------------*/
//判断当前步前进方向
if (x11>x00)
{
/*-----------------*/
//50<x1<589当前路径走向右边
if (x11>50 && x11<589) goto a11;
//x1==589当前路径走向左边
if (x11==589) goto a12;
/*-----------------*/
}
if (x11<x00)
{
/*-----------------*/
//50<x1<589当前路径走向左边
if (x11>50 && x11<589) goto a12;
//x1==0当前路径走向右边
if (x11==0) goto a11;
}
/*-----------------*/
a11:;//当前步走向右边,求当前步的接触点
b = y0-kk*x0;//先通过y=kk*x+b求b
x1 = 589;//令x1=589
y1 = kk*x1+b;//求y1
if (y1>=429)
{
y1 = 429;x1 = (y1-b)/kk;
}
if (y1>50 && y1<429)
{
x1 = 589;y1 = kk*x1+b;
}
if (y1<=50)
{
y1 = 50;x1 = (y1-b)/kk;
}
goto a13;
a12:;//当前步走向左边
b = y0-kk*x0;
x1 = 50;
y1 = kk*x1+b;
if (y1>=429)
{
y1 = 429;x1 = (y1-b)/kk;
}
if (y1>50 && y1<429)
{
x1 = 50;y1 = kk*x1+b;
}
if (y1<=50)
{
y1 = 50;x1 = (y1-b)/kk;
}
a13:;//至此求得了当前路径的起始点(x0,y0)和终点(x1,y1)
initgraph(640,480);
if (x1>x0)
{
for (x=x0;x<=x1;x+=1)
{
b = y0 - kk*x0;
y = kk*x+b;
//画图
setlinecolor(GREEN);
setfillcolor(GREEN);
fillcircle(x,y,50);
//延时
Sleep(1);
//擦图
setlinecolor(BLACK);
setfillcolor(BLACK);
fillcircle(x,y,50);
}
}
if (x1<x0)
{
for (x=x0;x>=x1;x-=1)
{
b = y0 - kk*x0;
y = kk*x+b;
//画图
setlinecolor(GREEN);
setfillcolor(GREEN);
fillcircle(x,y,50);
//延时
Sleep(1);
//擦图
setlinecolor(BLACK);
setfillcolor(BLACK);
fillcircle(x,y,50);
}
}
//printf("路径:%d ",k);
//printf("起点:%d %d ",x0,y0);
//printf("终点:%d %d ",x1,y1);
//printf("\n");
}
system("pause");
return 0;
}
希望对新手有帮助