Space invaders is a simple fun game. I made this game as a semester project as a beginner. I am sharing its source code here. By that time, I had no experience in bitmap graphics so I used custom header files - will be attaching it as well - to make characters.
Controls :
Use LEFT and RIGHT arrow keys to move the spaceship.
Use SPACE BAR to fire.
You have 3 lives in total.
Source Code :
#include <iostream>
#include <conio.h>
#include "help.h"
#include <windows.h>
#include <time.h>
using namespace std;
// Enemy Row 1
void Enemy_Row1(int ey1, int ey2, int ex1, int ex2, int arr[8][3])
{
for (int i = 0; i < 8; i++) // for 8 enemies
{
for (int j = 0; j < 3; j++)
{
if (j == 0) // set x1 coordinate in array
arr[i][j] = ex1;
if (j == 1) // set x2 coordinate in array
arr[i][j] = ex2;
if (j == 2) // set alive
arr[i][j] = 1;
}
// if enemy is alive
if (arr[i][2] == 1)
{
myRectE(arr[i][0], ey1, arr[i][1], ey2, 255); // create enemy
myEllipse(arr[i][0] + 5, ey1 + 2, (arr[i][1] - 15), ey1 + 12, 255, 0, 0);
myEllipse(arr[i][0] + 25, ey1 + 2, (arr[i][1] - 15), ey1 + 12, 255, 0, 0);
myLine(arr[i][0] + 10, ey2 - 10, arr[i][0] + 20, ey2 - 10, 200);
}
ex1 = ex1 + 75; // distance between enemies
ex2 = ex2 + 75;
}
}
void Enemy_Row2(int ey1, int ey2, int ex1, int ex2, int arr[8][3])
{
for (int i = 0; i < 7; i++) // for 7 enemies
{
for (int j = 0; j < 3; j++)
{
if (j == 0) // set x1 coordiante in array
arr[i][j] = ex1;
if (j == 1) // set x2 coordinate in array
arr[i][j] = ex2;
if (j == 2) // alive
arr[i][j] = 1;
}
// if enemy is alive
if (arr[i][2] == 1)
{
myRectE2(arr[i][0], ey1, arr[i][1], ey2, 255); // create enemy
myEllipse(arr[i][0] + 5, ey1 + 2, (arr[i][1] - 17), ey1 + 12, 255, 0, 0);
myEllipse(arr[i][0] + 25, ey1 + 2, (arr[i][1] - 17), ey1 + 12, 255, 0, 0);
myLine(arr[i][0] + 10, ey2 - 10, arr[i][0] + 20, ey2 - 10, 200);
}
ex1 = ex1 + 75; // distance between enemies
ex2 = ex2 + 75;
}
}
bool CheckGameOver(int Row1Alive[], int Row2Alive[], int lives) // checks if game is over depending on enemies alive and lives left
{
int DeadRow1 = 0; // none is dead in row 1
int DeadRow2 = 0; // none is dead in row 2
if (lives < 0) // lives of spaceship
return true;
for (int i = 0; i < 8; i++) // number of dead in row 1
{
if (Row1Alive[i] == 0)
DeadRow1++;
}
for (int i = 0; i < 7; i++) // number of dead in row 2
{
if (Row2Alive[i] == 0)
DeadRow2++;
}
if (DeadRow1 == 8 && DeadRow2 == 7) // if all dead :)
{
return true;
}
else return false;
}
void DestroySpaceship(int sx1, int sy1, int sx2, int sy2, int & lives, int score)
{
lives--; // decrease a life
myRectR(sx1, sy1, sx2, sy2, 0); // overwrite with black
myRectR(sx1 + 15, sy1 - 10, sx2 - 15, sy1, 0);
myLine((sx1 + sx2) / 2, sy1 - 10, (sx1 + sx2) / 2, sy1 - 15, 0);
if (lives >= 0) // if alive
{
gotoxy(30, 1); // inform on screen
cout << "You lost a life !";
Sleep(750); // pause
system("cls");
gotoxy(5, 1);
cout << "Lives : " << lives; // display lives
gotoxy(65, 1);
cout << "Score : " << score; // displays score
myRectS(sx1, sy1, sx2, sy2, 255); // create again
myRectS(sx1 + 15, sy1 - 10, sx2 - 15, sy1, 150);
myLine((sx1 + sx2) / 2, sy1 - 10, (sx1 + sx2) / 2, sy1 - 15, 255);
}
}
//Enemy Bullets
void EnemyFire(float x, int &y, int ebulletcolor)
{
myLine(x, y, x, y + 10, ebulletcolor); // create bullet
Sleep(10); // slight pause
myLine(x, y, x, y + 10, 0); // erase
}
bool Destroy_Barrier(float bulletx, float bullety, int bx1, int bx2)
{
if (bulletx >= bx1 && bulletx <= bx2) // if bullet lies in barrier
{
return true;
}
return false;
}
// Enemmies fire of row 1
void Enemyfire_R1(int arr[8][3], int enemynum, int &y, int sx1, int sy1, int sx2, int sy2, int &lives, int bx1, int bx2, int & color, int &enemy_has_hit_r1, int& ebulletcolor, int score)
{
float x;
if (y == 90 || y == 130) // resets hit after bullet moves top to bottom.
enemy_has_hit_r1 = 0, ebulletcolor = 255; // resets bullet color to white at start and end
if (y + 10 == 420)
enemy_has_hit_r1 = 0, ebulletcolor = 255;
if (arr[enemynum][2] == 1) // if enemy alive
{
x = (arr[enemynum][0] + arr[enemynum][1]) / 2; // x= (x1 + x2)/2
if (ebulletcolor != 0) // while bullet hasn't hit anything
EnemyFire(x, y, ebulletcolor);
y = y + 10;
}
if (y == 300 && enemy_has_hit_r1 == 0 && color >= 0) // when bullet approaches barrier and nothing has been hit and barrier exists
{
if (Destroy_Barrier(x, y, bx1, bx2))
{
color = color - 50; // reduce barrier intensity
enemy_has_hit_r1 = 1; // something has been hit
ebulletcolor = 0; // hit nothing else.
}
}
else
{
Sleep(20); // to avoid fast processing
}
if (y == 370 && ebulletcolor != 0 && enemy_has_hit_r1 == 0) // When bullet approaches spaceship
{
if (x >= sx1 && x <= sx2)
{
DestroySpaceship(sx1, sy1, sx2, sy2, lives, score);
enemy_has_hit_r1 = 1; // something has been hit
ebulletcolor = 0; // hit nothing else
return;
}
}
else if (arr[enemynum][2] == 0)
{
y = 500;
}
}
void Enemyfire_R2(int arr[8][3], int enemynum, int &y, int sx1, int sy1, int sx2, int sy2, int &lives, int bx1, int bx2, int & color, int &enemy_has_hit_r2, int& ebulletcolor, int score)
{
float x;
if (y == 90 || y == 130) // resets hit after bullet moves top to bottom.
enemy_has_hit_r2 = 0, ebulletcolor = 255; // resets bulletcolor to white at start and end.
if (y + 10 == 420)
enemy_has_hit_r2 = 0, ebulletcolor = 255;
if (arr[enemynum][2] == 1) // if enemy alive
{
x = (arr[enemynum][0] + arr[enemynum][1]) / 2; // x= (x1 + x2)/2
if (ebulletcolor != 0) // while bullet hasn't hit anything
EnemyFire(x, y, ebulletcolor);
y = y + 10;
}
if (y == 300 && enemy_has_hit_r2 == 0 && color >= 0) // when bullet approaches barrier and nothing has been hit and barrier exists
{
if (Destroy_Barrier(x, y, bx1, bx2))
{
color = color - 50; // reduce barrier intensity
enemy_has_hit_r2 = 1; // something has been hit
ebulletcolor = 0; // hit nothing else.
}
}
else
{
Sleep(20); // to avoid fast processing
}
if (y == 370 && ebulletcolor != 0 && enemy_has_hit_r2 == 0) // When bullet approaches spaceship
{
if (x >= sx1 && x <= sx2)
{
DestroySpaceship(sx1, sy1, sx2, sy2, lives, score);
enemy_has_hit_r2 = 1; // something has been hit
ebulletcolor = 0; // hit nothing else
return;
}
}
else if (arr[enemynum][2] == 0)
{
y = 500;
}
}
//Destroys enemies of row 1
bool DestroyEnemiesR1(int bulletx, int x1, int y1, int x2, int y2, int Row1Alive[], int arr[8][3])
{
for (int add = 0, i = 0; i < 8; i++) // checks one by one which enemy should be destroyed
{
if (bulletx >= x1 + add && bulletx <= x2 + add)
{
if (Row1Alive[i] == 1) // if enemy exists
{
myRectR(x1 + add, y1, x2 + add, y2, 0);
Row1Alive[i] = 0; // make it die
arr[i][2] = 0;
return true;
}
}
add = add + 75;
}
return false;
}
//Destroys enemies of row 2
bool DestroyEnemiesR2(int bulletx, int eX1, int eY1, int eX2, int eY2, int Row2Alive[], int arr[7][3])
{
for (int add = 0, i = 0; i < 7; i++) // checks one by one which enemy is hit
{
if (bulletx >= eX1 + add && bulletx <= eX2 + add)
{
if (Row2Alive[i] == 1) // if enemy exists
{
myRectR(eX1 + add, eY1, eX2 + add, eY2, 0);
Row2Alive[i] = 0; // kill it
arr[i][2] = 0;
return true;
}
}
add = add + 75;
}
return false;
}
// Spaceship fire
void bullet(float & bulletx, float & bullety, int & lx, int &sx1, int &sy1, int &sx2, int &sy2, int ex1, int ey1, int ex2, int ey2, int eX1, int eY1, int eX2, int eY2, int Row1Alive[], int Row2Alive[], int & score, int arrE1[8][3], int arrE2[7][3], int & barriercolor, int &bx1, int &bx2)
{
int sbulletcolor = 255, spaceship_has_hit = 0; // reset bullet
float bullety1 = bullety; // copy current bullety value to save changes if spaceship is moved while bullet is in progress
float bulletx1 = bulletx; // copy current bulletx value to save changes if spaceship is moved while bullet is in progress
for (;bullety1 > 0; )
{
myLine(bulletx1, bullety1 - 12, bulletx1, bullety1 - 22, sbulletcolor); // create bullet
if (bullety1 == 131 && spaceship_has_hit == 0) // Bullet approaches row 2 and nothing has been hit
{
if (DestroyEnemiesR2(bulletx1, eX1, eY1, eX2, eY2, Row2Alive, arrE2)) // call function
{
sbulletcolor = 0, spaceship_has_hit = 1, score = score + 50, gotoxy(65, 1); // change color to 0, something is hit, hit nothing else, update score
cout << "Score : " << score;
}
else
sbulletcolor = 255; // else no change
}
if (bullety1 == 91 && spaceship_has_hit == 0) // bullet approaches row 1
{
if (DestroyEnemiesR1(bulletx1, ex1, ey1, ex2, ey2, Row1Alive, arrE1)) // if condition true
{
sbulletcolor = 0, spaceship_has_hit = 1, score = score + 100, gotoxy(65, 1); // change color to 0, something is hit, hit nothing else, update score
cout << "Score : " << score;
}
else
sbulletcolor = 255;
}
Sleep(20);
myLine(bulletx1, bullety1 - 12, bulletx1, bullety1 - 22, 0); // erase bullet
bullety1 = bullety1 - 10; // decrement
if (_kbhit()) // for movement of spaceship during movement of bullet
{
char key = GetAsyncKeyState(37); //Moves spaceship left
if (key == 1)
{
myRectR(sx1, sy1, sx2, sy2, 0);
myRectR(sx1 + 15, sy1 - 10, sx2 - 15, sy1, 0);
lx = (sx1 + sx2) / 2;
myLine(lx, sy1 - 10, lx, sy1 - 15, 0);
sx1 = sx1 - 10;
sx2 = sx2 - 10;
myRectS(sx1, sy1, sx2, sy2, 255);
myRectS(sx1 + 15, sy1 - 10, sx2 - 15, sy1, 150);
lx = (sx1 + sx2) / 2;
myLine(lx, sy1 - 10, lx, sy1 - 15, 255);
bulletx = (sx1 + sx2) / 2;
bullety = sy1 + 1;
}
key = GetAsyncKeyState(39); // Moves spaceship right
if (key == 1)
{
myRectR(sx1, sy1, sx2, sy2, 0);
myRectR(sx1 + 15, sy1 - 10, sx2 - 15, sy1, 0);
lx = (sx1 + sx2) / 2;
myLine(lx, sy1 - 10, lx, sy1 - 15, 0);
sx1 = sx1 + 10;
sx2 = sx2 + 10;
myRectS(sx1, sy1, sx2, sy2, 255);
myRectS(sx1 + 15, sy1 - 10, sx2 - 15, sy1, 150);
lx = (sx1 + sx2) / 2;
myLine(lx, sy1 - 10, lx, sy1 - 15, 255);
bulletx = (sx1 + sx2) / 2;
bullety = sy1 + 1;
}
}
}
}
// For Right movement of the Barrier
void Barrier_Right(int &x1, int y1, int &x2, int y2, int &flag, int color)
{
myRectReplacingBarrier(x1, y1, x2, y2, 0); // delete barrier
x1 = x1 + 10; // increments
x2 = x2 + 10;
myRectBarrier(x1, y1, x2, y2, color); // overwrite
if (x1 == 500) // check if barrier has reached fixed point
flag = 1;
}
// for left movement of the Barrier
void Barrier_Left(int &x1, int y1, int &x2, int y2, int &flag, int color)
{
myRectReplacingBarrier(x1, y1, x2, y2, 0); // delete barrier
x1 = x1 - 10; // decrements
x2 = x2 - 10;
myRectBarrier(x1, y1, x2, y2, color); // overwrite
if (x1 < 30) // check if it has reached fixed point
flag = 0;
}
void main()
{
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " \\ // " << endl;
cout << " @@@@@@@@@@@@@@@@@@@@ " << endl;
cout << " @@@@@@@@@@@@@@@@@@@@@@@@@ " << endl;
cout << " @@@@@@@@( )@@@@@@@@@@( )@@@@@@ " << endl;
cout << " @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ " << endl;
cout << " @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ " << endl;
cout << " @@@@@@@@@@WWWWWWWWWWW@@@@@@@@@@ " << endl;
cout << " @@@@@@@@MMMMMMMMMMM@@@@@@@@ " << endl;
cout << " @@@@@@@@@@@@@@@@@@@@@@ " << endl;
cout << " # # " << endl;
cout << " # # " << endl<< endl << endl;
cout << " ******** SPACE INVADERS ******** " << endl;
Sleep(3000);
system("cls");
cout << " WELCOME TO SPACE INVADERS "; // Main menu
cout << endl << endl;
cout << " The game instructions are as follow : " << endl << endl;
cout << " 1) Use the left and right arrow keys to move the spaceship left or right." << endl << endl;
cout << " 2) Use the space button to fire a bullet. The bullet will kill an enemy but you cannot damage barrier." << endl << endl;
cout << " 3) You have 3 lives. The aliens will fire at random and if you get hit, you will lose a life." << endl << " When you have 0 lives and you get hit once more, you will lose the game." << endl << endl;
cout << " 4) Your score will increase by 50 or 100 depending on which row the alien is." << endl << " The upper row alien will give you 100 points whereas the lower row enemies will give 50 points." << endl << endl;
cout << " 5) The barrier can be hit upto 5 times by the aliens. It will block bullets from aliens." << endl << " However it will be destroyed after 5 hits. Then you will be vulnerable to enemy fire." << endl << endl;
cout << "PRESS ANY KEY TO START THE GAME OR PRESS Q TO EXIT" << endl;
int choice = _getch(); // if user wants to exit
if (choice == 'q' || choice == 'Q')
return;
system("cls"); // clear screen
cout << "LOADING ... Please wait" << endl; // fancy :P
for (int i = 0; i <5; i++)
{
cout << "* ";
if (i == 4)
cout << " ... Ready!";
Sleep(500);
}
system("cls");
// Coordinates for enemy row 1
int ex1 = 40;
int ey1 = 60;
int ex2 = 70;
int ey2 = 90;
int arrE1[8][3];
Enemy_Row1(ey1, ey2, ex1, ex2, arrE1);
// Coordinates for enemy row 2
int eX1 = 74;
int eY1 = 100;
int eX2 = 106;
int eY2 = 130;
int arrE2[7][3];
Enemy_Row2(eY1, eY2, eX1, eX2, arrE2);
// To count for enemyfire
int count = 1;
int enemy_has_hit_r1 = 0;
int enemy_has_hit_r2 = 0;
int ebulletcolor1 = 255;
int ebulletcolor2 = 255;
int srand(time(NULL)); // turn initialise
// Row 1 & 2 enemies status
int Row1Alive[8] = { 1,1,1,1,1,1,1,1 };
int Row2Alive[7] = { 1,1,1,1,1,1,1 };
// Game
int score = 0;
int lives = 3;
// Coordinates for barrier
int bx1 = 20;
int by1 = 300;
int bx2 = 120;
int by2 = 310;
int flag = 0;
int barriercolor = 250;
//Cooordinates for spaceship
int sx1 = 270;
int sy1 = 380;
int sx2 = 320;
int sy2 = 400;
int lx; // for the spaceship needle
//Coordinates for spaceship fire
float bulletx = (sx1 + sx2) / 2, bullety = sy1;;
//Spaceship
myRectS(sx1, sy1, sx2, sy2, 255);
myRectS(sx1 + 15, sy1 - 10, sx2 - 15, sy1, 150);
lx = (sx1 + sx2) / 2;
myLine(lx, sy1 - 10, lx, sy1 - 15, 255);
//Enemy turn - Tells whose turn is it to fire from Row 1
int enemyturnR1 = rand() % 8;
int y1 = ey2;
// Enemy Turn - Tells whose turn is it to fire from Row 2
int enemyturnR2 = rand() % 7;
int y2 = eY2;
gotoxy(65, 1); // initial score
cout << "Score : " << score;
gotoxy(5, 1); // initial lives
cout << "Lives : " << lives;
bool gamenotover = true; // condition for game loop
for (;gamenotover; )
{
char key = GetAsyncKeyState(37); //Moves spaceship left
if (key == 1)
{
myRectR(sx1, sy1, sx2, sy2, 0);
myRectR(sx1 + 15, sy1 - 10, sx2 - 15, sy1, 0);
lx = (sx1 + sx2) / 2;
myLine(lx, sy1 - 10, lx, sy1 - 15, 0);
sx1 = sx1 - 10;
sx2 = sx2 - 10;
myRectS(sx1, sy1, sx2, sy2, 255);
myRectS(sx1 + 15, sy1 - 10, sx2 - 15, sy1, 150);
lx = (sx1 + sx2) / 2;
myLine(lx, sy1 - 10, lx, sy1 - 15, 255);
bulletx = (sx1 + sx2) / 2;
bullety = sy1 + 1;
}
key = GetAsyncKeyState(39); // Moves spaceship right
if (key == 1)
{
myRectR(sx1, sy1, sx2, sy2, 0);
myRectR(sx1 + 15, sy1 - 10, sx2 - 15, sy1, 0);
lx = (sx1 + sx2) / 2;
myLine(lx, sy1 - 10, lx, sy1 - 15, 0);
sx1 = sx1 + 10;
sx2 = sx2 + 10;
myRectS(sx1, sy1, sx2, sy2, 255);
myRectS(sx1 + 15, sy1 - 10, sx2 - 15, sy1, 150);
lx = (sx1 + sx2) / 2;
myLine(lx, sy1 - 10, lx, sy1 - 15, 255);
bulletx = (sx1 + sx2) / 2;
bullety = sy1 + 1;
}
key = GetAsyncKeyState(32); // Space bar pressed, for spaceship fire
if (key == 1)
{
bullet(bulletx, bullety, lx, sx1, sy1, sx2, sy2, ex1, ey1, ex2, ey2, eX1, eY1, eX2, eY2, Row1Alive, Row2Alive, score, arrE1, arrE2, barriercolor, bx1, bx2);
myLine(lx, sy1 - 10, lx, sy1 - 15, 255);
}
if (CheckGameOver(Row1Alive, Row2Alive, lives) == true)
gamenotover = false;
// For enemy fire
if (y1 > 400) // if enemy row 1 bullet has reached bottom, reset position and enemy turn
{
enemyturnR1 = rand() % 8; // random enemy turn
y1 = ey2; // reset coordinate to top
}
Enemyfire_R1(arrE1, enemyturnR1, y1, sx1, sy1, sx2, sy2, lives, bx1, bx2, barriercolor, enemy_has_hit_r1, ebulletcolor1, score); // call function to fire
if (y2 > 400) // if enemy row 2 bullet has reached bottom, reset position and enemy turn
{
enemyturnR2 = rand() % 7; // random enemy turn
y2 = eY2; // reset coordinate to top
}
Enemyfire_R2(arrE2, enemyturnR2, y2, sx1, sy1, sx2, sy2, lives, bx1, bx2, barriercolor, enemy_has_hit_r2, ebulletcolor2, score); // call function to fire
//My moving barrier
if (bx1 < 500 && flag == 0 && barriercolor >= 0) // moves barrier right til it reaches a fixed point
{
Barrier_Right(bx1, by1, bx2, by2, flag, barriercolor);
}
if (flag == 1 && barriercolor >= 0) // moves barrier left till it reaches a fixed point
{
Barrier_Left(bx1, by1, bx2, by2, flag, barriercolor);
}
}
system("cls"); // clear screen
if (lives < 0) // if lose
cout << "Game over. You lost." << endl << "Your score : " << score << endl << "Better luck next time.";
else // if win
cout << "Game over. You won." << endl << "Your score : " << score;
cout << endl;
system("pause");
}
I have used the custom graphics. You need to have this header file given below as a must for the code above to work.
Header file :
I used it under the name of "help.h" as you can see in the code above. In case you use any other name, please change it there as well. ^^
#pragma once
#include <windows.h>
using namespace std;
void gotoxy(int x,int y)
{
HANDLE h=GetStdHandle(STD_OUTPUT_HANDLE);
COORD c;
c.X=x;
c.Y=y;
SetConsoleCursorPosition(h,c);
}
void gotoxyscore(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void myLine(float x1, float y1, float x2, float y2,int color) //use three 3 integers if you want colored lines.
{
HWND console_handle = GetConsoleWindow();
HDC device_context = GetDC(console_handle);
//change the color by changing the values in RGB (from 0-255) to get shades of gray. For other colors use 3 integers for colors.
HPEN pen =CreatePen(PS_SOLID,2,RGB(color,color,color)); //2 is the width of the pen
SelectObject(device_context,pen);
MoveToEx(device_context,x1,y1,NULL);
LineTo(device_context,x2, y2);
ReleaseDC(console_handle, device_context);
}
void myRectE(int x1, int y1, int x2, int y2,int color)
{
HWND console_handle = GetConsoleWindow();
HDC device_context = GetDC(console_handle);
//change the color by changing the values in RGB (from 0-255)
HPEN pen =CreatePen(PS_SOLID,2,RGB(255,0,0));
SelectObject(device_context,pen);
HBRUSH brush = ::CreateSolidBrush(RGB(245,245,220)); //Fill color is black
SelectObject(device_context,brush);
Rectangle(device_context,x1,y1,x2,y2);
ReleaseDC(console_handle, device_context);
DeleteObject(pen);
DeleteObject(brush);
DeleteObject(device_context);
}
void myRectBarrier(int x1, int y1, int x2, int y2,int color)
{
HWND console_handle = GetConsoleWindow();
HDC device_context = GetDC(console_handle);
//change the color by changing the values in RGB (from 0-255)
HPEN pen =CreatePen(PS_SOLID,2,RGB(color,color,color));
SelectObject(device_context,pen);
HBRUSH brush = ::CreateSolidBrush(RGB(0,color,0)); //Fill color is black
SelectObject(device_context,brush);
Rectangle(device_context,x1,y1,x2,y2);
ReleaseDC(console_handle, device_context);
DeleteObject(pen);
DeleteObject(brush);
DeleteObject(device_context);
}
void myRectReplacingBarrier(int x1, int y1, int x2, int y2,int color)
{
HWND console_handle = GetConsoleWindow();
HDC device_context = GetDC(console_handle);
//change the color by changing the values in RGB (from 0-255)
HPEN pen =CreatePen(PS_SOLID,2,RGB(0,0,0));
SelectObject(device_context,pen);
HBRUSH brush = ::CreateSolidBrush(RGB(0,0,0)); //Fill color is black
SelectObject(device_context,brush);
Rectangle(device_context,x1,y1,x2,y2);
ReleaseDC(console_handle, device_context);
DeleteObject(pen);
DeleteObject(brush);
DeleteObject(device_context);
}
void myRectS(int x1, int y1, int x2, int y2,int color)
{
HWND console_handle = GetConsoleWindow();
HDC device_context = GetDC(console_handle);
//change the color by changing the values in RGB (from 0-255)
HPEN pen =CreatePen(PS_SOLID,2,RGB(0,0,255));
SelectObject(device_context,pen);
HBRUSH brush = ::CreateSolidBrush(RGB(0,200,0)); //Fill color is green
SelectObject(device_context,brush);
Rectangle(device_context,x1,y1,x2,y2);
ReleaseDC(console_handle, device_context);
DeleteObject(pen);
DeleteObject(brush);
DeleteObject(device_context);
}
void myRectE2(int x1, int y1, int x2, int y2,int color)
{
HWND console_handle = GetConsoleWindow();
HDC device_context = GetDC(console_handle);
//change the color by changing the values in RGB (from 0-255)
HPEN pen =CreatePen(PS_SOLID,2,RGB(255,0,0));
SelectObject(device_context,pen);
HBRUSH brush = ::CreateSolidBrush(RGB(160,32,240)); //Fill color is black 160, 32 ,240
SelectObject(device_context,brush);
Rectangle(device_context,x1,y1,x2,y2);
ReleaseDC(console_handle, device_context);
DeleteObject(pen);
DeleteObject(brush);
DeleteObject(device_context);
}
void myRectR(int x1, int y1, int x2, int y2,int color)
{
HWND console_handle = GetConsoleWindow();
HDC device_context = GetDC(console_handle);
//change the color by changing the values in RGB (from 0-255)
HPEN pen =CreatePen(PS_SOLID,2,RGB(0,0,0));
SelectObject(device_context,pen);
HBRUSH brush = ::CreateSolidBrush(RGB(0,0,0)); //Fill color is black
SelectObject(device_context,brush);
Rectangle(device_context,x1,y1,x2,y2);
ReleaseDC(console_handle, device_context);
DeleteObject(pen);
DeleteObject(brush);
DeleteObject(device_context);
}
void myEllipse(int x1, int y1, int x2, int y2,int color1, int color2, int color3)
{
HWND console_handle = GetConsoleWindow();
HDC device_context = GetDC(console_handle);
//change the color by changing the values in RGB (from 0-255)
HPEN pen =CreatePen(PS_SOLID,2,RGB(255,0,255));
SelectObject(device_context,pen);
HBRUSH brush = ::CreateSolidBrush(RGB(0,255,0)); //Fill color is black
SelectObject(device_context,brush);
Ellipse(device_context,x1,y1,x2,y2);
ReleaseDC(console_handle, device_context);
}
Let me know if you have any confusions. Best of Luck!
PS : I hope commenting will help you understand the code. :)
0 comments:
Post a Comment