64,654
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<windows.h>
using namespace std;
int main()
{
int w1=10,w2=60;
while(w2)
{
system("cls");
cout.width(w1);
cout.setf(ios::right);
cout<<"⊙";
Sleep(50);
system("cls");
cout.width(w2);
cout.setf(ios::left);
cout<<"●";
w1++;w2--;
Sleep(50);
if(w1==w2)break;
}
while(w1)
{
system("cls");
cout.width(w1);
cout.setf(ios::left);
cout<<"●";
w1--;
Sleep(100);
}
}
我这个可行,可以试一试!那个黑点代表大球,中空的是小球
[code=C/C++]
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
struct Ball{
int x,y,r;
int mass;
int v;
};
void show(struct Ball* ball){
setcolor(BLUE);
circle(ball->x,ball->y,ball->r);
}
void move(struct Ball* ball, int slp){
delay(slp);
setcolor(BLACK);
circle(ball->x,ball->y,ball->r);
ball->x+=ball->v;
setcolor(BLUE);
circle(ball->x,ball->y,ball->r);
}
void setNewPos(struct Ball* ball, int xnew,int ynew){
setcolor(BLACK);
circle(ball->x,ball->y,ball->r);
ball->x= xnew, ball->y= ynew;
show(ball);
}
int main(){
int gdriver = EGA, gmode = EGAHI;
int x1=100,x2=300,y=100,v,i;
struct Ball b1,b2;
b1.x=x1, b1.y=y, b1.r=20, b1.mass=10, b1.v=8;
b2.x=x2, b2.y=y, b2.r=10, b2.mass=5, b2.v=-3;
initgraph(&gdriver, &gmode, "c:\\programs\\tc2\\bgi");//tc安装目录下,*.bgi 文件所在目录;
//可能是 tc2 目录下,也可能还有个bgi目录
show(&b1);
show(&b2);
v= (10*8+5*-3)/(10+5);//碰撞后速度
i=80;
while(i--){
move(&b1,80);
move(&b2,80);
if(b2.x-b1.x<20+10){
b1.v=b2.v=v;
setNewPos(&b2,b1.x+20+10,y);
break;
}
}
while(i--){
move(&b1,80);
move(&b2,80);
}
getch();
closegraph();
return 0;
}