SWAT GAME
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | /******************************************************* * MYCPLUS Sample Code - https://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 <graphics.h> #include <conio.h> #include <stdlib.h> #include <dos.h> 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 <file://\\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 ); } |