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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
#include "GameConsts.h"
using namespace conio;
bool gameRunning;
bool levelRunning;
struct ConioChar
{
unsigned char ch;
COLORS bgColour;
COLORS textColour;
int x;
int y;
int lastX;
int lastY;
bool visible;
};
ConioChar enemy;
const int ENEMY_ROW = SCREEN_ROWS - 2;
ConioChar plane;
const int PLANE_ROW = 2;
ConioChar bomb;
const int BOMB_ROW = PLANE_ROW + 1;
int bombHit = 0;
int bombMiss = 0;
bool introduction();
void initGameData();
void drawGameScreen();
void handleEvents();
void update();
void draw();
bool check4Win();
bool playAgain();
void drawChar( ConioChar cch );
int main()
{
srand( time( NULL ) );
gameRunning = introduction();
while ( gameRunning )
{
initGameData();
drawGameScreen();
levelRunning = true;
while( levelRunning )
{
handleEvents();
update();
if ( check4Win() )
{
}
Sleep( 32 );
}
gameRunning = ( playAgain() );
}
cout << setbk( BLACK ) << setclr( WHITE ) << clrscr;
return EXIT_SUCCESS;
}
void initGameData()
{
enemy = { 'E', BLUE, WHITE, 1, ENEMY_ROW, 1, ENEMY_ROW, true };
plane = { 'W', BLUE, YELLOW, SCREEN_COLS / 2, 1, SCREEN_COLS / 2, 1, true };
bomb = { 'B', BLUE, YELLOW, 0, 0, 0, 0, false };
}
void handleEvents()
{
if( kbhit() )
{
unsigned char ch = getch();
if( ch == ARROW_KEY )
{
ch = getch();
if( ch == KEY_LEFT && plane.x > 1 )
{
plane.lastX = plane.x;
// move to the left
plane.x -= 1;
}
else if ( ch == KEY_RIGHT && plane.x < SCREEN_COLS )
{
plane.lastX = plane.x;
plane.x += 1;
}
}
else if ( ch == SPACE )
{
bomb.visible = true;
bomb.lastY = bomb.y = plane.y + 1;
bomb.lastX = bomb.x = plane.x;
}
}
}
void update()
{
enemy.lastX = enemy.x;
if( enemy.x < SCREEN_COLS )
{
enemy.x += 1;
}
else
{
enemy.x = 1;
}
if( bomb.y < ENEMY_ROW )
{
bomb.lastY = bomb.y;
bomb.y += 1;
}
if( bomb.y == ENEMY_ROW && ( bomb.x >= enemy.x + 2 || bomb.x <= enemy.x - 2 ) )
{
bomb.visible = false;
++bombMiss;
}
if( (bomb.visible == true) && (bomb.x >= enemy.x - 1) && (bomb.x == enemy.x) && (bomb.x <= enemy.x + 1) && (bomb.y >= enemy.y - 1) && (bomb.y == enemy.y) && (bomb.y <= enemy.y + 1) )
{
enemy.visible = false;
bomb.visible = false;
++bombHit;
playAgain();
}
}
void draw()
{
drawChar( enemy );
drawChar( plane );
drawChar( bomb );
}
void drawGameScreen()
{
cout << setbk( RED ) << clrscr;
drawRectangle( BLUE, 1, 1, SCREEN_COLS, SCREEN_ROWS - 2 );
drawRectangle( GREEN, 1, SCREEN_ROWS - 1, SCREEN_COLS, 1 );
}
void drawChar( ConioChar cch )
{
if( cch.lastX != cch.x || cch.lastY != cch.y )
{
cout << setxy( cch.lastX, cch.lastY ) << setbk( cch.bgColour ) << " ";
cch.lastX = cch.x;
cch.lastY = cch.y;
}
if( cch.visible )
{
cout << setbk( cch.bgColour ) << setclr( cch.textColour );
cout << setxy( cch.x, cch.y ) << cch.ch;
}
}
bool check4Win()
{
return false;
}
bool playAgain()
{
cout << setbk( WHITE ) << setclr( RED ) << setxy( 22, 19 ) << "You asploded the enemy!";
cout << setbk( WHITE ) << setclr( RED ) << setxy( 22, 20 ) << "Hits: " << bombHit << " Misses: " << bombMiss;
cout << setbk( WHITE ) << setclr( RED ) << setxy( 22, 21 ) << "Press Y to play again: ";
char ch = getch();
if( toupper( ch ) == 'Y' )
{
drawGameScreen();
enemy.visible = true;
return true;
}
else
{
cout << setbk( WHITE ) << setclr( RED ) << setxy( 25, 15 ) << "Thanks for playing!";
kbhit();
return false;
}
}
bool introduction()
{
cout << setbk( BLUE ) << clrscr;
cout << setclr( WHITE );
cout << setxy( 32, 12 ) << "The Introduction";
char ch;
do
{
cout << setclr( YELLOW ) << setxy( 26, 22 ) << "Do you want to Play? Y or N";
ch = getch();
ch = toupper( ch );
}
while( ch != 'N' && ch != 'Y' );
if( ch == 'Y' )
{
return true;
}
else
{
return false;
}
}
|