主题:turboc2.0下的台球游戏
jackin0627
[专家分:1270] 发布于 2004-04-26 18:07:00
/*运行环境 TC2.0
w,s,a,d控制方向, 逗号和句号控制力量,空格发杆
本来还可用鼠标控制,因为要用一个"mouse.h",所以去掉了,
如有人想要带鼠标的可与我连系*/
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#define PATH "d:\\学习\\turboc2"/*改为你的TC目录*/
#define pi 3.141593
#define r 10.0
#define zero 0.01
#define BC 0
struct balltype
{
float x,y;
float dx,dy;
int flag;
}ball[16];
int cx=320-5*r,cy=240;
float power=15.0;
int flag=1,player=0,chang=0;
int hole[6][2]=
{
{17,92}, {320,92}, {623,92},
{17,388}, {320,388}, {623,388}
};
int plball[2]={0,0},ex=1;
/**************************/
void drawtable();
/********************************/
void drawpow()
{
int i;
gotoxy(27,4);
printf("power==[");
for(i=0;i<power;i++)printf(">");
for(i=20-power;i>0;i--)printf("-");
printf("]");
}
/********************************/
void drawpl()
{
gotoxy(2,4);
printf("[Player %d]",player+1);
if(plball[player])printf("<%d--%d>",plball[player],plball[player]+6);
}
/*************/
void bound(int b1,int b2)
{
float s1=100,s2=100,s,x,y,dx1,dx2,dy1,dy2,d1,d2;
if(b2==16)
{
ball[b1].dx=-ball[b1].dx;
}
else if(b2==17)
{
ball[b1].dy=-ball[b1].dy;
}
else
{
x=ball[b2].x-ball[b1].x;
y=ball[b2].y-ball[b1].y;
if(!(fabs(ball[b1].dx)<zero&&fabs(ball[b1].dy)<zero))
{
s1=atan2(ball[b1].dy,ball[b1].dx);
}
if(!(fabs(ball[b2].dx)<zero&&fabs(ball[b2].dy)<zero))
{
s2=atan2(ball[b2].dy,ball[b2].dx);
}
s=atan2(y,x);
d1=sqrt(pow(ball[b1].dx,2)+pow(ball[b1].dy,2))*cos(s-s1);
d2=sqrt(pow(ball[b2].dx,2)+pow(ball[b2].dy,2))*cos(pi-(s-s2));
dx1=d1*cos(s);
dx2=d2*cos(pi-s);
dy1=d1*sin(s);
dy2=d2*sin(pi-s);
if(s1!=100&&fabs(s-s1)<pi/2)
{
ball[b1].dx-=dx1;
ball[b1].dy-=dy1;
ball[b2].dx+=dx1;
ball[b2].dy+=dy1;
}
if(s2!=100&&fabs(s-s2)>pi/2)
{
ball[b1].dx+=dx2;
ball[b1].dy+=dy2;
ball[b2].dx-=dx2;
ball[b2].dy-=dy2;
}
}
ball[b1].dx*=0.97;
ball[b1].dy*=0.97;
ball[b2].dx*=0.97;
ball[b2].dy*=0.97;
}
/*******************/
void close()
{
int i;
for(i=0;i<16;i++)
{
}
}
/*******************/
int getkey()
{
char ch=0;
if(kbhit())
ch=getch();
return(ch);
}
/************/
void drawball(int i,int flag)
{
char nc[5];
if(flag==0)
setcolor(BC);
else
{
if(i==0)setcolor(WHITE);
else if(i<8)setcolor(4);
else if(i==8)setcolor(8);
else setcolor(2);
}
circle(ball[i].x,ball[i].y,r);
itoa(i,nc,10);
outtextxy(ball[i].x-6,ball[i].y-3,nc);
}
/*********************/
void drawtable()
{ char p[10],pp[20];
int i;
setcolor(14);
rectangle(5,80,635,400);
setfillstyle(8,2);
for(i=0;i<6;i++)
{
circle(hole[i][0],hole[i][1],12);
floodfill(hole[i][0],hole[i][1],14);
}
for(i=0;i<16;i++)
{
if(ball[i].flag==1)continue;
else
{
ball[i].x=90+i*30;
ball[i].y=440;
drawball(i,1);
}
}
drawpow();
drawpl();
}
/******************/
void go()
{
float s;
flag=0;
chang=1;
setcolor(BC);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
s=atan2(cy-ball[0].y,cx-ball[0].x);
ball[0].dx=(power/2.5)*cos(s);
ball[0].dy=(power/2.5)*sin(s);
}
/***************************/
void win(int i)
{
gotoxy(30,13);
printf("Player %d win the game!",i+1);
gotoxy(30,14);
printf("Press anykey to exit.");
ex=0;
}
/************************/
void ok(int b)
{
int i,m=0;
if(b==8)
{
if(plball[player]==0)win(!player);
else
{
for(i=plball[player];i<plball[player]+7;i++)
{
if(ball[i].flag!=1)
{
m=1;
break;
}
}
if(!m)win(!player);
else win(player);
}
}
else if(b==0)
{
chang=2;
}
else if(plball[player]==0)
{
if(b>0&&b<8){plball[player]=1;plball[!player]=9;}
else if(b>8&&b<16){plball[player]=9;plball[!player]=1;}
chang=0;
}
}
/************************/
void main()
{
int gdriver=DETECT,gmode;
int i,j,ff=1,k;
char nc[5];
initgraph(&gdriver,&gmode,PATH);
setbkcolor(BC);
ball[0].x=160;
ball[0].y=240;
ball[0].dx=0;
ball[0].dy=0;
ball[2].x=480+(r+1);
ball[2].y=240-3*(r+1);
ball[2].dx=0;
ball[2].dy=0;
ball[1].x=480+3*(r+1);
ball[1].y=240+4*(r+1);
ball[1].dx=0;
ball[1].dy=0;
ball[3].x=480-(r+1);
ball[3].y=240+2*(r+1);
ball[3].dx=0;
ball[3].dy=0;
ball[4].x=480-3*(r+1);
ball[4].y=240-(r+1);
ball[4].dx=0;
ball[4].dy=0;
ball[5].x=480-5*(r+1);
ball[5].y=240;
ball[5].dx=0;
ball[5].dy=0;
ball[6].x=480+3*(r+1);
ball[6].y=240-2*(r+1);
ball[6].dx=0;
ball[6].dy=0;
ball[7].x=480+(r+1);
ball[7].y=240-(r+1);
ball[7].dx=0;
ball[7].dy=0;
ball[8].x=480-(r+1);
ball[8].y=240;
ball[8].dx=0;
ball[8].dy=0;
ball[9].x=480+(r+1);
ball[9].y=240+(r+1);
ball[9].dx=0;
ball[9].dy=0;
ball[10].x=480+3*(r+1);
ball[10].y=240+2*(r+1);
ball[10].dx=0;
ball[10].dy=0;
ball[11].x=480+3*(r+1);
ball[11].y=240;
ball[11].dx=0;
ball[11].dy=0;
ball[12].x=480-3*(r+1);
ball[12].y=240+(r+1);
ball[12].dx=0;
ball[12].dy=0;
ball[13].x=480-(r+1);
ball[13].y=240-2*(r+1);
ball[13].dx=0;
ball[13].dy=0;
ball[14].x=480+(r+1);
ball[14].y=240+3*(r+1);
ball[14].dx=0;
ball[14].dy=0;
ball[15].x=480+3*(r+1);
ball[15].y=240-4*(r+1);
ball[15].dx=0;
ball[15].dy=0;
for(i=0;i<16;i++)
{
drawball(i,1);
ball[i].flag=1;
}
drawtable();
for(;ex;)
{
drawtable();
for(i=0;i<5;i++)
{
switch(getkey())
{
case 27:
goto end;
case 'w':if(flag==1)
{
setcolor(BC);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
cy--;
}
break;
case 'W':if(flag==1)
{
setcolor(BC);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
cy-=5;
}
break;
case 'a':if(flag==1)
{
setcolor(BC);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
cx--;
}
break;
case 'A':
if(flag==1)
{
setcolor(BC);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
cx-=5;
}
break;
case 's':if(flag==1)
{
setcolor(BC);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
cy++;
}
break;
case 'S':if(flag==1)
{
setcolor(BC);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
cy+=5;
}
break;
case 'd':if(flag==1)
{
setcolor(BC);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
cx++;
}
break;
case 'D':if(flag==1)
{
setcolor(BC);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
cx+=5;
}
break;
case ' ': if(flag==1)
{
go();
}
break;
case '.': power++;
if(power>20)power=20;
break;
case ',': power--;
if(power<0)power=0;
break;
default:break;
}
}
if(flag==1)
{
setcolor(8);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
}
if(ball[0].flag==0)
{
ball[0].flag=1;
drawball(0,0);
ball[0].x=160;
ball[0].y=240;
ball[0].dx=ball[0].dy=0;
aa: for(i=1;i<16;i++)
if(ball[i].flag==1)
{
if((pow(fabs(ball[i].x-ball[0].x),2)+pow(fabs(ball[i].y-ball[0].y),2))<=4*r*r)
{
ball[0].x++;
goto aa;
}
}
}
for(i=0;i<16;i++)
{
if(ball[i].flag==0)continue;
drawball(i,0);
ball[i].x+=ball[i].dx;
if(ball[i].x<=5+r)
{
ball[i].x=5+r ;
bound(i,16);
}
if(ball[i].x>=635-r)
{
ball[i].x=635-r;
bound(i,16);
}
ball[i].y+=ball[i].dy;
if(ball[i].y<=80+r)
{
ball[i].y=80+r ;
bound(i,17);
}
if(ball[i].y>=400-r)
{
ball[i].y=400-r ;
bound(i,17);
}
for(j=0;j<16;j++)
if(j!=i&&ball[j].flag==1)
{
if((pow(fabs(ball[i].x-ball[j].x),2)+pow(fabs(ball[i].y-ball[j].y),2))<=4*(r+1)*(r+1))
{
if(!(fabs(ball[i].dx)<zero&&fabs(ball[i].dy)<zero&&fabs(ball[j].dx)<zero&&fabs(ball[j].dy)<zero))
{
bound(i,j);
}
}
}
if(ball[i].flag==1)drawball(i,1);
ball[i].dx*=0.994;
ball[i].dy*=0.994;
if(fabs(ball[i].dx)<0.1&&fabs(ball[i].dy)<0.1)
{
ball[i].dx=ball[i].dy=0;
}
for(j=0;j<6;j++)
{
if((pow(fabs(ball[i].x-hole[j][0]),2)+pow(fabs(ball[i].y-hole[j][1]),2))<50)
{
ball[i].flag=0;
drawball(i,0);
if(plball[player]!=0&&chang!=2)
{
if(i>=plball[player]&&i<=plball[player]+6)chang=0;
}
ok(i);
break;
}
}
}
ff=1;
for(i=0;i<16;i++)
{
if(!(fabs(ball[i].dx)<0.05&&fabs(ball[i].dy)<0.05)&&ball[i].flag==1)
{
ff=0;
break;
}
}
if(ff)
{
flag=1;
if(chang)player=!player;
chang=0;
}
}
end: getch();
close();
closegraph();
}
回复列表 (共52个回复)
沙发
有事找我请烧钱 [专家分:500] 发布于 2004-04-26 21:18:00
真强!
板凳
xqmbest [专家分:0] 发布于 2004-04-26 21:34:00
呵,我还正在想编个这个玩玩呢,谢谢了啊
3 楼
kefengxw [专家分:110] 发布于 2004-04-26 21:56:00
厉害~~~
4 楼
jackin0627 [专家分:1270] 发布于 2004-04-28 09:22:00
以下是鼠标控制的源程序,
左键发杆,单击右改变力量,
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include"mouse.h"
#include<math.h>
#define PATH "d:\\学习\\turboc2"
#define pi 3.14159265358979323846
#define r 10.0
#define zero 0.0001
#define BC 0
struct balltype
{
double x,y;
double dx,dy;
int flag;
}ball[16];
int cx=320-5*r,cy=240;
double power=15.0;
int flag=1,player=0,chang=0;
int hole[6][2]=
{
{17,92}, {320,92}, {623,92},
{17,388}, {320,388}, {623,388}
};
int plball[2]={0,0},ex=1;
int ob1=0,ob2=0,cc=10,oc=0;
/**************************/
void drawtable();
/********************************/
void drawpow()
{
int i;
gotoxy(27,4);
printf("power==[");
for(i=0;i<power;i++)printf(">");
for(i=20-power;i>0;i--)printf("-");
printf("]");
}
/********************************/
void drawpl()
{
gotoxy(2,4);
printf("[Player %d]",player+1);
if(plball[player])printf("<%2d--%2d>",plball[player],plball[player]+6);
}
/*************/
void bound(int b1,int b2)
{
double s1=100,s2=100,s,x,y,dx1,dx2,dy1,dy2,d1,d2;
if(b2==16)
{
ball[b1].dx=-ball[b1].dx;
}
else if(b2==17)
{
ball[b1].dy=-ball[b1].dy;
}
else
{
if((b1==ob1||b1==ob2)&&(b2==ob2||b2==ob1))
{
if(abs(oc-cc)<2)
{oc=cc; goto exit;}
else goto start;
}
else {ob1=b1,ob2=b2;oc=cc;}
start:
x=ball[b2].x-ball[b1].x;
y=ball[b2].y-ball[b1].y;
if(!(fabs(ball[b1].dx)<zero&&fabs(ball[b1].dy)<zero))
{
s1=atan2(ball[b1].dy,ball[b1].dx);
}
if(!(fabs(ball[b2].dx)<zero&&fabs(ball[b2].dy)<zero))
{
s2=atan2(ball[b2].dy,ball[b2].dx);
}
s=atan2(y,x);
d1=sqrt(pow(ball[b1].dx,2)+pow(ball[b1].dy,2))*cos(s-s1);
d2=sqrt(pow(ball[b2].dx,2)+pow(ball[b2].dy,2))*cos(pi-(s-s2));
dx1=d1*cos(s);
dx2=d2*cos(pi-s);
dy1=d1*sin(s);
dy2=d2*sin(pi-s);
if(s1!=100&&fabs(s-s1)<pi/2.0)
{
ball[b1].dx-=dx1;
ball[b1].dy-=dy1;
ball[b2].dx+=dx1;
ball[b2].dy+=dy1;
}
if(s2!=100&&fabs(s-s2)>pi/2.0)
{
ball[b1].dx+=dx2;
ball[b1].dy+=dy2;
ball[b2].dx-=dx2;
ball[b2].dy-=dy2;
}
}
ball[b1].dx*=0.97;
ball[b1].dy*=0.97;
ball[b2].dx*=0.97;
ball[b2].dy*=0.97;
exit: ;
}
/*******************/
void close()
{
int i;
for(i=0;i<16;i++)
{
}
}
/*******************/
int getkey()
{
char ch=0;
if(kbhit())
ch=getch();
return(ch);
}
/************/
void drawball(int i,int flag)
{
char nc[5];
if(flag==0)
setcolor(BC);
else
{
if(i==0)setcolor(WHITE);
else if(i<8)setcolor(4);
else if(i==8)setcolor(8);
else setcolor(2);
}
circle(ball[i].x,ball[i].y,r);
itoa(i,nc,10);
outtextxy(ball[i].x-6,ball[i].y-3,nc);
}
/*********************/
void drawtable()
{ char p[10],pp[20];
int i;
setcolor(14);
rectangle(5,80,635,400);
setfillstyle(8,2);
for(i=0;i<6;i++)
{
circle(hole[i][0],hole[i][1],12);
floodfill(hole[i][0],hole[i][1],14);
}
for(i=0;i<16;i++)
{
if(ball[i].flag==1)continue;
else
{
ball[i].x=90+i*30;
ball[i].y=440;
drawball(i,1);
}
}
drawpow();
drawpl();
}
/******************/
void go()
{
double s;
flag=0;
chang=1;
setcolor(BC);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
s=atan2(cy-ball[0].y,cx-ball[0].x);
ball[0].dx=(power/3.0)*cos(s);
ball[0].dy=(power/3.0)*sin(s);
}
/***************************/
void win(int i)
{
gotoxy(30,13);
printf("Player %d win the game!",i+1);
gotoxy(30,14);
printf("Press anykey to exit.");
ex=0;
}
/************************/
void ok(int b)
{
int i,m=0;
if(b==8)
{
if(plball[player]==0)win(!player);
else
{
for(i=plball[player];i<plball[player]+7;i++)
{
if(ball[i].flag!=0)
{
m=1;
break;
}
}
if(!m)win(player);
else win(!player);
}
}
else if(b==0)
{
chang=2;
}
else if(plball[player]==0)
{
if(b>0&&b<8){plball[player]=1;plball[!player]=9;}
else if(b>8&&b<16){plball[player]=9;plball[!player]=1;}
chang=0;
}
}
/************************/
void main()
{
int gdriver=DETECT,gmode;
int i,j,ff=1,k,n;
char nc[5];
initgraph(&gdriver,&gmode,PATH);
setbkcolor(BC);
InitMouse();
SetYRange(0,480);
SetXRange(0,640);
ball[0].x=160;
ball[0].y=240;
ball[0].dx=0;
ball[0].dy=0;
ball[2].x=480+(r-1);
ball[2].y=240-3*(r+1);
ball[2].dx=0;
ball[2].dy=0;
ball[1].x=480+3*(r-1);
ball[1].y=240+4*(r+1);
ball[1].dx=0;
ball[1].dy=0;
ball[3].x=480-(r-1);
ball[3].y=240+2*(r+1);
ball[3].dx=0;
ball[3].dy=0;
ball[4].x=480-3*(r-1);
ball[4].y=240-(r+1);
ball[4].dx=0;
ball[4].dy=0;
ball[5].x=480-5*(r-1);
ball[5].y=240;
ball[5].dx=0;
ball[5].dy=0;
ball[6].x=480+3*(r-1);
ball[6].y=240-2*(r+1);
ball[6].dx=0;
ball[6].dy=0;
ball[7].x=480+(r-1);
ball[7].y=240-(r+1);
ball[7].dx=0;
ball[7].dy=0;
ball[8].x=480-(r-1);
ball[8].y=240;
ball[8].dx=0;
ball[8].dy=0;
ball[9].x=480+(r-1);
ball[9].y=240+(r+1);
ball[9].dx=0;
ball[9].dy=0;
ball[10].x=480+3*(r-1);
ball[10].y=240+2*(r+1);
ball[10].dx=0;
ball[10].dy=0;
ball[11].x=480+3*(r-1);
ball[11].y=240;
ball[11].dx=0;
ball[11].dy=0;
ball[12].x=480-3*(r-1);
ball[12].y=240+(r+1);
ball[12].dx=0;
ball[12].dy=0;
ball[13].x=480-(r-1);
ball[13].y=240-2*(r+1);
ball[13].dx=0;
ball[13].dy=0;
ball[14].x=480+(r-1);
ball[14].y=240+3*(r+1);
ball[14].dx=0;
ball[14].dy=0;
ball[15].x=480+3*(r-1);
ball[15].y=240-4*(r+1);
ball[15].dx=0;
ball[15].dy=0;
for(i=0;i<16;i++)
{
drawball(i,1);
ball[i].flag=1;
}
drawtable();
for(;ex;)
{
drawtable();
for(i=0;i<5;i++)
{
switch(getkey())
{
case 27:
goto end;
case 'w':if(flag==1)
{
setcolor(BC);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
cy--;
}
break;
case 'W':if(flag==1)
{
setcolor(BC);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
cy-=5;
}
break;
case 'a':if(flag==1)
{
setcolor(BC);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
cx--;
}
break;
case 'A':
if(flag==1)
{
setcolor(BC);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
cx-=5;
}
break;
case 's':if(flag==1)
{
setcolor(BC);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
cy++;
}
break;
case 'S':if(flag==1)
{
setcolor(BC);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
cy+=5;
}
break;
case 'd':if(flag==1)
{
setcolor(BC);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
cx++;
}
break;
case 'D':if(flag==1)
{
setcolor(BC);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
cx+=5;
}
break;
case ' ': if(flag==1)
{
go();
}
break;
case '.': power++;
if(power>20)power=20;
break;
case ',': power--;
if(power<0)power=0;
break;
default:break;
}
if(flag==1)
{
setcolor(BC);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
GetMouseXY(&cx,&cy);
if(GetPressInfo(2)==1)
{
go();
}
if(GetReleaseInfo(1)==1)
{
power++;
power=(int)power%21;
}
}
}
if(flag==1&&ball[0].flag!=0)
{
if(plball[player]==0)setcolor(8);
else if(plball[player]==1)setcolor(4);
else if(plball[player]==9)setcolor(2);
setlinestyle(2,0,1);
line(ball[0].x,ball[0].y,cx,cy);
circle(cx,cy,r);
setlinestyle(0,0,1);
line(cx-r,cy,cx+r,cy);
line(cx,cy-r,cx,cy+r);
}
if(ball[0].flag==0)
{
ball[0].flag=1;
drawball(0,0);
ball[0].x=160;
ball[0].y=240;
ball[0].dx=ball[0].dy=0;
aa: for(i=1;i<16;i++)
if(ball[i].flag==1)
{
if((pow(fabs(ball[i].x-ball[0].x),2)+pow(fabs(ball[i].y-ball[0].y),2))<=4*r*r)
{
ball[0].x++;
goto aa;
}
}
}
cc++;
for(n=0;n<3;n++)
for(i=0;i<16;i++)
{
if(ball[i].flag==0)continue;
drawball(i,0);
ball[i].x+=ball[i].dx;
if(ball[i].x<=5+r)
{
ball[i].x=5+r ;
bound(i,16);
}
if(ball[i].x>=635-r)
{
ball[i].x=635-r;
bound(i,16);
}
ball[i].y+=ball[i].dy;
if(ball[i].y<=80+r)
{
ball[i].y=80+r ;
bound(i,17);
}
if(ball[i].y>=400-r)
{
ball[i].y=400-r ;
bound(i,17);
}
for(j=0;j<16;j++)
if(j!=i&&ball[j].flag==1)
{
if((pow(fabs(ball[i].x-ball[j].x),2)+pow(fabs(ball[i].y-ball[j].y),2))<=4*(r+1)*(r+1))
{
if(!(fabs(ball[i].dx)<zero&&fabs(ball[i].dy)<zero&&fabs(ball[j].dx)<zero&&fabs(ball[j].dy)<zero))
{
bound(i,j);
}
}
}
if(ball[i].flag==1)drawball(i,1);
ball[i].dx*=0.9945;
ball[i].dy*=0.9945;
if(fabs(ball[i].dx)<0.1&&fabs(ball[i].dy)<0.1)
{
ball[i].dx=ball[i].dy=0;
}
for(j=0;j<6;j++)
{
if((pow(fabs(ball[i].x-hole[j][0]),2)+pow(fabs(ball[i].y-hole[j][1]),2))<50)
{
ball[i].flag=0;
drawball(i,0);
if(plball[player]!=0&&chang!=2)
{
if(i>=plball[player]&&i<=plball[player]+6)chang=0;
}
ok(i);
break;
}
}
}
ff=1;
for(i=0;i<16;i++)
{
if(!(fabs(ball[i].dx)<0.05&&fabs(ball[i].dy)<0.05)&&ball[i].flag==1)
{
ff=0;
break;
}
}
if(ff)
{
flag=1;
if(chang)player=!player;
chang=0;
}
}
end: getch();
close();
closegraph();
}
5 楼
jackin0627 [专家分:1270] 发布于 2004-04-28 09:29:00
上面的程序需要一个mouse.h的头文件,
将以下内容保存为 mouse.h放在TC的include目录下即可,
/*************mouse.h*****************/
# include <dos.h>
# include <graphics.h>
# define TRUE 1
# define FALSE 0
int mouse_installed = FALSE;
/*=========================================================================*\
功能 入口参数 出口参数
初始化mouse ax = 0x0000h ax = mouse状态(-1=已安装,0=未安装)
bx = mouse按扭数目
\*=========================================================================*/
int InitMouse()
{
union REGS r;
r.x.ax = 0;
int86(0x33,&r,&r);
if(r.x.ax == 0xffff)
mouse_installed = TRUE;
else
mouse_installed = FALSE;
return(r.x.bx);
}
/*======================================================================*\
显示鼠标 ax=0x0001h
\*======================================================================*/
int ShowMouse()
{
union REGS r;
if(mouse_installed == FALSE)return(-1);
r.x.ax = 0x0001;
int86(0x33,&r,&r);
return(0);
}
/*======================================================================*\
隐藏鼠标 ax=0x0002h
\*======================================================================*/
int HideMouse()
{
union REGS r;
if(mouse_installed == FALSE) return(-1);
r.x.ax = 0x0002;
int86(0x33,&r,&r);
return(0);
}
/*======================================================================*\
读取鼠标位置 ax=0x0003h cx=x
dx=y
\*======================================================================*/
int GetMouseXY(int *x,int *y)
{
union REGS r;
if(mouse_installed == FALSE)return(-1);
r.x.ax = 0x0003;
int86(0x33,&r,&r);
*x = r.x.cx;
*y = r.x.dx;
return(0);
}
/*======================================================================*\
设置鼠标位置 ax=0x0004h
cx=x
dx=y
\*======================================================================*/
int SetMouseXY(int x,int y)
{
union REGS r;
if(mouse_installed == FALSE)return(-1);
r.x.ax = 0x0004;
r.x.cx = x;
r.x.dx = y;
int86(0x33,&r,&r);
return(0);
}
/*======================================================================*\
读取按下信息 ax = 0x0005h ax = 读取按下信息
bx = 按键序号
\*======================================================================*/
int GetPressInfo(int button)
{
union REGS r;
if(mouse_installed == FALSE)return(-1);
r.x.ax = 0x0005;
r.x.bx = button;
int86(0x33,&r,&r);
return(r.x.bx);
}
/*======================================================================*\
读取放开信息 ax = 0x0006h ax = 读取放开信息
bx = 按键序号
\*======================================================================*/
int GetReleaseInfo(int button)
{
union REGS r;
if(mouse_installed == FALSE)return(-1);
r.x.ax = 0x0006;
r.x.bx = button;
int86(0x33,&r,&r);
return(r.x.bx);
}
/*======================================================================*\
设置mouse
的x方向范围 ax = 0x0007h
cx = x(min)
dx = x(max)
\*======================================================================*/
int SetXRange(int min,int max)
{
union REGS r;
if(mouse_installed == FALSE)return(-1);
r.x.ax = 0x0007;
r.x.cx = min;
r.x.dx = max;
int86(0x33,&r,&r);
return(0);
}
/*======================================================================*\
设置mouse
的y方向范围 ax = 0x0008h
cx = y(min)
dx = y(max)
\*======================================================================*/
int SetYRange(int min,int max)
{
union REGS r;
if(mouse_installed == FALSE)return(-1);
r.x.ax = 0x0008;
r.x.cx = min;
r.x.dx = max;
int86(0x33,&r,&r);
return(0);
}
6 楼
lheng [专家分:1490] 发布于 2004-04-28 10:47:00
厉害,不过完全看不明白.但是都要说声多谢
7 楼
铁血民工 [专家分:250] 发布于 2004-04-29 13:10:00
在判断两球是否相撞时用了for(i=0;i<16;i++)=>for(j=0;j<16;j++)为什么不是for(j=i+1;j<16;j++)。bound里面用了速度和两球连线的角度来判断是否+ -dx,dy 如果用j=i+1,不是不用判断了吗?我只看了一会儿,也许没看清楚。请你说说你的想法。
8 楼
jackin0627 [专家分:1270] 发布于 2004-04-29 16:34:00
不能用for(j=i+1;j<16;j++)。
因为先让i球的位置加的dx,dy,后,如果正好与一个小于i的球(设为m球)相撞,若用for(j=i+1;j<16;j++)。则不执行bound();只有等到下一轮判断m球时才能与i球相撞.
这样效果就不好了
9 楼
铁血民工 [专家分:250] 发布于 2004-04-30 11:41:00
谢谢,原来如此。
10 楼
aliu [专家分:320] 发布于 2004-05-01 23:10:00
上面的程序需要一个mouse.h的头文件,
将以下内容保存为 mouse.h放在TC的include目录下即可.楼主我按你说的方法去做怎么提示打不开文件mouse.h.请楼主回复一下,麻烦你了,谢谢!
我来回复