⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bouncer.cpp

📁 Blitz-Ball-The bouncing ball demo
💻 CPP
字号:
//I did'nt use all the files
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<dos.h>
#include<bios.h>
#include<stdlib.h>
/*
	  //////////  /        //   //////////   //////////
	  /	  //  /        //       //              //
	  /	  //  /        //       //             //
	  //////////  /        //       //            //    BALL
	  /       //  /        //       //          //
	  /       //  /        //       //        //
	  //////////  ///////  //       //      //////////
       /*THE BOUNCING BALL PROGRAM-A simple trigonometry demo
*******************COMMENTED FOR YOUR CONVINIENCE*******************
	  -Abhishek.Bhrushundi aka CODEKID(codekid88@yahoo.co.in)
*/
//The array declarations
//recX is the array for X co-ordinates of the rectangles
double recX[1000]={20,80,140,200,260,320,380,440,500,560,620};
//recY is the array for Y co-ordiantes of the rectangles
double recY[1000]={20,40,60,80,100,120,140,160,180};
//hitXY contains co-ordiantes of those boxes which have been hit by the ball
double hitXY[1000];
//this variable keeps the count of the boxes which have been hit
int hl=2;

#define ESCAPE 27
#define INC 97
#define DEC 122

//XV is x vector and YV is y vector st is a collison detection variable
double XV=1,YV=1,st=0;

// This function returns whether the rectangle which is being hit by the ball curently , has been hit by the ball earlier or not
int isrep(int x, int y)
{

for(int i=0;i<hl;)
{
if((hitXY[i]==x)&&(hitXY[i+1]==y))
{

return 1;
}
i=i+2;
}
return 0;
}
//THE ULTIMATE FUNCTION(i took 1.5 hours to progarm this,it's quite a tricky thing to program )
//This function returns whether the ball has collided with any of the rectangles or not
// if the collison has taken place then it updates the vectors accordingly
int col_detect(int cx,int cy)
{
double dx1,dx2,dy1,dy2,i,j;
for( i=0;i<10;i++)
 {
	for( j=0;j<8;j++)
	{
		if(isrep(recX[i],recY[j])==0)
		{
		dx1=abs(cx-recX[i]);
		dx2=abs(cx-recX[i+1]);
		dy1=abs(cy-recY[j]);
		dy2=abs(cy-recY[j+1]);

		if((dy1<=4)||(dy2<=4))
		{

			if((cx>=recX[i])&&(cx<=recX[i+1]))
			{
			YV=-1*YV;
			goto found;
			}
		}


		if((dx1<=4)||(dx2<=4))
		{

			if((cy>=recY[j])&&(cy<=recY[j+1]))
			{
			XV=-1*XV;
			goto found;
			}
		}

		}
	}
 }
return 0;
found:
hitXY[hl]=recX[i];
hitXY[hl+1]=recY[j];
hl=hl+2;
setcolor(BLACK);
rectangle(recX[i],recY[j],recX[i+1],recY[j+1]);

return 1;
}
// THE "main" BODY OF THE PROGRAM
void main()
{
clrscr();
//input angle at which the ball should bounce
double th,sp=1.0;
cout<<"*************************BOUNCING BALL PROGRAM(95.99% BUG FREE)*****************\n\n\n";
cout<<"Press 'a' to increase ball speed ,'z' to decrease ball speed\n";
cout<<"Press 'ESC' key 2 times to exit....hehe..hehe!\n";
again:
cout<<"Enter angle(<90)\n";
cin>>th;
if(th>=90)
{
goto again;
}
//initialize the graphics mode
//NOTE: if you dont know about the various graphic drivers , just set gdriver to DETECT
int gdriver=EGALO,gmode;
initgraph(&gdriver,&gmode,"");
setcolor(6);
settextstyle(3,0,2);
outtextxy(getmaxx()/2-150,getmaxy()/2-25,"BLITZ BALL-by Abhishek aka Codekid");
setcolor(WHITE);
getch();
clearviewport();
//drawing the rectangles
/*for(int i=0;i<10;i++)
{
	for(int j=0;j<8;j++) rectangle(recX[i],recY[j],recX[i+1],recY[j+1]);
} */
// Some important variables :
double x=300,y=250,pi=M_PI,a,b,q,t=1,key=0;
char s[10];
// converting to radians from degrees
th=((2*pi)/360)*th;
// Drawing the main rectangle ( i mean the outer box in which the ball bounces!!)
rectangle(20,20,getmaxx()-20,getmaxy()-20);
setfillstyle(8,BLUE);
floodfill(10,10,getmaxcolor());
//drawing the ball
setcolor(BLUE);
circle(x,y,3);
setfillstyle(1,BLUE);
floodfill(x,y,BLUE);
a=x;
b=y;
q=200;


// The main loop
while(key!=ESCAPE)
{
 //Checking whether the ball is hitting the boundary
 if(x<=29) XV=1;
 if(x>=getmaxx()-29) XV=-1;
 if(y<=29) YV=1;
 if(y>=getmaxy()-29) YV=-1;
 // updating the BALL'S POSITION
 x=x+(XV*cos(th)*sp);
 y=y+(YV*sin(th)*sp);
 st=0;
 setcolor(WHITE);
 //The loop for changing the colors of the rectangles randomly
 for(int k=0;k<10;k++)
		{
			for(int l=0;l<8;l++)
			{
				colorchang:
				q=random(15);
				if(q==0) goto colorchang;
				setcolor(q);
				if(isrep(recX[k],recY[l])==0) rectangle(recX[k],recY[l],recX[k+1],recY[l+1]);
			}

		}
	//THIS IS THE BRAIN OF THE PROGRAM AND CONTROLS ALL FUNCTIONS AND VARIBALES
	//Checking for collison with any solid object
	while((x>=29) && (x<=getmaxx()-29) && (y>=29) && (y<=getmaxy()-29) &&(st!=1))
	{
		if(kbhit())
		{
			key=getch();
			if((key==INC)&&(sp<2.6))sp=sp+0.2;
			if((key==DEC)&&(sp>1.2))sp=sp-0.2;
			if(key==ESCAPE) break;
		}
		//updating ball's postion
		x=x+(XV*cos(th)*sp);
		y=y+(YV*sin(th)*sp);

		//drawing the ball at updated positions and erasing the old one
		setcolor(BLACK);
		circle(a,b,3);
		setfillstyle(1,BLACK);
		floodfill(a,b,BLACK);
		setcolor(BLUE);
		circle(x,y,3);
		setfillstyle(1,BLUE);
		floodfill(x,y,BLUE);
		setcolor(WHITE);
		rectangle(20,20,getmaxx()-20,getmaxy()-20);
		delay(2);
		setlinestyle(SOLID_LINE,1,3);
		setcolor(BLACK);
		setcolor(WHITE);
		setlinestyle(0,0,0);
		a=x;
		b=y;
		setcolor(WHITE);
	    //collison detection
	    st=col_detect(x,y);
	}
 //sound effects
 if( (x>=29) || (x>=getmaxx()-29) || (y>=29) || (y>=getmaxy()-29) )
 {
 sound(2000);
 delay(7);
 sound(300);
 delay(6);
 nosound();
 }
 if(st==1)
 {
 sound(500);
 delay(10);
 nosound();
 }
}
end:
getch();
closegraph();

}
//END OF CODE

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -