C and C++ Programming Resources

Custom Search

SWAT GAME

SWAT GAME

/*******************************************************
*     MYCPLUS Sample Code - http://www.mycplus.com     *
*                                                     *
*   This code is made available as a service to our   *
*      visitors and is provided strictly for the      *
*               purpose of illustration.              *
*                                                     *
* Please direct all inquiries to saqib at mycplus.com *
*******************************************************/

#include 
#include 
#include 
#include 

class swat{
	public:
	void draw_swat( int x ){
		setcolor( LIGHTCYAN );
		setlinestyle( SOLID_LINE,0,3 );
		line( x,470,x,275);
		setfillstyle( XHATCH_FILL, LIGHTCYAN );
		rectangle( x-80,50,x+80,275);
		floodfill( x,200, LIGHTCYAN );
	}
} swats;

int final_x, final_y;

class fly{
	private:
	int move_x, move_y, current_x, current_y;

	public:
	void draw_fly( int& );

	void show_fly(){
		setcolor( WHITE );
		setfillstyle( SOLID_FILL, getcolor() );
		circle( final_x, final_y, 4 );
		floodfill( final_x, final_y, getcolor() );
	}
} fly;


void fly :: draw_fly( int &time )
{

	randomize();

	current_x = random(600);
	current_y = random(275);

	for( int i=0; i<10; i++ ){
		setcolor( WHITE );
		setfillstyle( SOLID_FILL, getcolor() );
		circle( current_x, current_y, 4 );
		floodfill( current_x, current_y, getcolor() );
		sound(200);

		delay( time );
		setcolor( BLACK );
		setfillstyle( SOLID_FILL, getcolor() );
		circle( current_x, current_y, 4 );
		floodfill( current_x, current_y, getcolor() );
		nosound();

		final_x = current_x;
		final_y = current_y;

		move_x = random(300);
		move_y = random(175);

		if ( current_x + move_x > 600 )
			current_x -= 1.25 * move_x;
		else current_x += move_x;
		if ( current_x <= 0 )
			current_x += 0.75 * move_x;

		if ( current_y + move_y > 275 )
			current_y -= move_y;

		else if( current_y + move_y < 50 )
			current_y += 2 * move_y;
		else current_y += move_y;
	}
}


void main()
{
	int pos;
	int swat_number;
	int time = 700;

	enum state{ WIN,LOSE };
	state user;

	int driver=DETECT;
	int mode=DETECT;
	initgraph(&driver,&mode,"\\borlandc\\bgi ");
	highvideo();

	do{
		user = LOSE;
		pos = 120;

		cleardevice();
		fly.draw_fly( time );
		time-= 50;
		for( int i=0; i<3; i++ ){

			swats.draw_swat( pos );
			pos+=200;
		}

		settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
		outtextxy( 10,5, "Enter swat number?" );
		swat_number = getch();

		cleardevice();

		switch ( swat_number ){
			case '1' : if( ( final_x < 220 && final_x > 0 ))
			user = WIN;
			break;

			case '2' : if( ( final_x < 420 && final_x > 220 ) )
			user = WIN;
			break;

			case '3' : if( ( final_x < 620 && final_x > 420 ) )
			user = WIN;
			break;
		}

		cleardevice();
		if( time <= 70 && user == WIN ){
			settextstyle(TRIPLEX_FONT,HORIZ_DIR,11);
			for( int i=1; i<15; i++ ){
				setcolor(i);
				outtextxy( 180,10,"FLY" );
				outtextxy( 30,150,"SWATTING" );
				outtextxy( 10,300,"CHAMPION" );
				delay(1000);
			}
			exit( EXIT_SUCCESS );
		}

		fly.show_fly();

		settextstyle(TRIPLEX_FONT,HORIZ_DIR,9);

		if( user == WIN ){
			setcolor(LIGHTGREEN);
			outtextxy( 40,350,"YOU WON !!!!" );
		}
		else{
			setcolor(LIGHTRED);
			outtextxy( 40,350,"YOU LOSE !!!!" );
		}

		getch();
	}while( user == WIN );
}

Tags: , ,

There are No Comments to this post. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response or TrackBack from your own site.


Leave a Reply

You must be logged in to post a comment.