game docsupload
game docsupload
Submitted by:
Anas Faiz 2023-CS-26
Supervised by:
Dr. Muhammad Awais Hassan
Course:
CSC-102 Programming Fundamentals
Table of Content
s
Tank Wars...................................................................................................................................................3
Tank wars Characters..................................................................................................................................3
Game Objects..............................................................................................................................................3
Rules & Interactions...................................................................................................................................3
Goal of The Game.......................................................................................................................................4
Wireframes of the Game.............................................................................................................................4
Function Prototypes....................................................................................................................................7
Complete Code...........................................................................................................................................9
Tank Wars
The game is based on the a war tank which is going to fight to with his enemies and fight to
death
I have used the concept of world war 1 in which my player plays the role of one side of the
waring
nation but in my game there is only one player and three enemy in the first level and one player
and one enemy in 2nd level so the competition is tough for the player but he has do it for his
country.
Game Objects
The object in the game is:
o A health of 200 is given to the player in level one and 100 in final level. Its objective is to
shoot and kill all the enemies before they kill him. Every time it takes a bullet it loses the
health of 50 so it has play very carefully in order to survive.
o Survival is one of the main objective.
Function Prototypes
void gotoxy(int x, int y);
char getCharAtxy(short int x, short int y);
void printenemy();
void eraseenemy();
void printenemy2();
void eraseenemy2();
void printenemy3();
void eraseenemy3();
void eraseplayer();
void printplayer();
void moveplayerleft();
Anas Faiz 2023-CS-26
Grow
void moveplayerright();
void moveenemy(int digit);
void moveenemy2(int digit);
void moveenemy3(int digit);
void moveBullet();
void removefire();
void header();
bool checkCollision();
void printmaze();
void enemyhealth();
void enemyShoot();
void printEnemyBullets();
void moveEnemyBullet();
void printplayerhealth();
void gamelost();
void gamewon();
int random(int min, int max);
void printfinallevel();
void printfinalmaze();
void printfinalenemy();
void erasefinalenemy();
void printplayerfinal();
void eraseplayerfinal();
void printfinalplayerhealth();
void printfinalenemyhealth();
void finalenemyshoot();
void movefinalenemybullet();
void movefinalplayerbullet();
Anas Faiz 2023-CS-26
Grow
int eX = 2, eY = 2;
Complete Code
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
void printenemy2();
void eraseenemy2();
void printenemy3();
void eraseenemy3();
void eraseplayer();
void printplayer();
void moveplayerleft();
void moveplayerright();
void moveenemy(int digit);
void moveenemy2(int digit);
void moveenemy3(int digit);
void moveBullet();
void removefire();
void header();
bool checkCollision();
void printmaze();
void enemyhealth();
void enemyShoot();
void printEnemyBullets();
void moveEnemyBullet();
void printplayerhealth();
void gamelost();
void gamewon();
int random(int min, int max);
void printfinallevel();
void printfinalmaze();
void printfinalenemy();
void erasefinalenemy();
void printplayerfinal();
void eraseplayerfinal();
void printfinalplayerhealth();
void printfinalenemyhealth();
void finalenemyshoot();
void movefinalenemybullet();
void movefinalplayerbullet();
int movefinalenemy(int digit);
void movefinalplayerup();
void movefinalplayerdown();
void erasefinalplayerbullet();
void printfinalenemybullet();
// firstlevelvariables
int eX = 2, eY = 2;
int eX2 = 30, eY2 = 15;
Anas Faiz 2023-CS-26
Grow
int main()
{
system("cls");
system("Color 04");
header();
system("Color 0A");
system("cls");
printmaze();
printenemy();
printenemy2();
printenemy3();
printplayer();
// bool isFiring = false;
while (true)
{
if (playerhealth < 0)
{
playerhealth = 0;
break;
}
if (playerhealth >= 0 && !checkenemy1 && !checkenemy2 && !checkenemy3)
{
finallevel = true;
}
enemyhealth();
enemyShoot();
moveEnemyBullet();
printEnemyBullets();
printplayerhealth();
if (enemyhealth1 <= 0)
{
enemyhealth1 = 0;
checkenemy1 = false;
enemy1bulletcheck = true;
gotoxy(enemey1bulletX, enemey1bulletY);
cout << " ";
}
if (enemyhealth2 <= 0)
{
enemyhealth2 = 0;
checkenemy2 = false;
enemy2bulletcheck = true;
gotoxy(enemey2bulletX, enemey2bulletY);
Anas Faiz 2023-CS-26
Grow
if (GetAsyncKeyState(VK_ESCAPE))
{
break;
}
if (GetAsyncKeyState(VK_SPACE))
{
Anas Faiz 2023-CS-26
Grow
if (!playerbulletcheck)
{
bX = pX + 6;
bY = pY - 1;
playerbulletcheck = true;
}
}
if (playerbulletcheck)
{
moveBullet();
}
if (playerhealth <= 0)
{
system("cls");
gamelost();
getch();
lost = true;
break;
}
if (finallevel)
{
system("cls");
printfinallevel();
Sleep(3000);
system("cls");
printfinalmaze();
printplayerfinal();
printfinalenemy();
while (true)
{
finalenemyshoot();
movefinalenemybullet();
printfinalenemybullet();
printfinalenemyhealth();
printfinalplayerhealth();
Sleep(100);
if (finalenemyhealth <= 0)
{
checkfinalenemy = false;
checkfinalenemybullet = true;
gotoxy(finalenemybulletX, finalenemybulletY);
cout << " ";
lost = true;
}
if (GetAsyncKeyState(VK_UP))
{
movefinalplayerup();
}
if (GetAsyncKeyState(VK_DOWN))
{
movefinalplayerdown();
Anas Faiz 2023-CS-26
Grow
}
if (GetAsyncKeyState(VK_SPACE))
{
if (!checkfinalplayerbullet)
{
playerbulletfX = playerfX + 21;
playerbulletfY = playerfY;
checkfinalplayerbullet = true;
}
}
if (checkfinalplayerbullet)
{
movefinalplayerbullet();
}
Sleep(100);
if (lost)
{
break;
}
if (finish)
Anas Faiz 2023-CS-26
Grow
{
break;
}
}
if (lost)
{
system("cls");
gamelost();
getch;
exit(0);
}
if (finish)
{
system("cls");
gamewon();
getch;
exit(0);
}
return 0;
}
void printmaze()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
cout <<
"##########################################################################
##########################" << endl;
cout << "# # #" << endl;
cout << "# # #" << endl;
cout << "# # #" << endl;
cout << "# # #" << endl;
void printenemy()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
gotoxy(eX, eY);
cout << " _____ ";
gotoxy(eX, eY + 1);
cout << " __|_>o<_|__ ";
gotoxy(eX, eY + 2);
cout << "/___________\\ ";
gotoxy(eX, eY + 3);
cout << "\\=\\>-----</=/";
gotoxy(eX, eY + 4);
cout << "_______________";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
}
void printenemy2()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
gotoxy(eX2, eY2);
cout << " _____/_o_\\_____ ";
gotoxy(eX2, eY2 + 1);
cout << "(==(/_______\\)==) ";
gotoxy(eX2, eY2 + 2);
cout << " \\==\\/ \\/==/";
gotoxy(eX2, eY2 + 3);
cout << "===================";
}
void printenemy3()
Anas Faiz 2023-CS-26
Grow
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
gotoxy(eX3, eY3);
cout << " _____ ";
gotoxy(eX3, eY3 + 1);
cout << " __|_>o<_|__ ";
gotoxy(eX3, eY3 + 2);
cout << "/___________\\ ";
gotoxy(eX3, eY3 + 3);
cout << "\\=\\>-----</=/";
gotoxy(eX3, eY3 + 4);
cout << "---------------";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
}
void eraseenemy()
{
gotoxy(eX, eY);
cout << " ";
gotoxy(eX, eY + 1);
cout << " ";
gotoxy(eX, eY + 2);
cout << " ";
gotoxy(eX, eY + 3);
cout << " ";
gotoxy(eX, eY + 4);
cout << " ";
}
void eraseenemy2()
{
Anas Faiz 2023-CS-26
Grow
gotoxy(eX2, eY2);
cout << " ";
gotoxy(eX2, eY2 + 1);
cout << " ";
gotoxy(eX2, eY2 + 2);
cout << " ";
gotoxy(eX2, eY2 + 3);
cout << " ";
}
void eraseenemy3()
{
gotoxy(eX3, eY3);
cout << " ";
gotoxy(eX3, eY3 + 1);
cout << " ";
gotoxy(eX3, eY3 + 2);
cout << " ";
gotoxy(eX3, eY3 + 3);
cout << " ";
gotoxy(eX3, eY3 + 4);
cout << " ";
}
eX += 5;
}
else
{
eX -= 5;
}
if (eX >= 60)
{
eX = 2;
}
if (eX <= 2)
{
eX = 60;
}
printenemy();
}
}
eX3 = 60;
}
printenemy3();
}
}
void eraseplayer()
{
gotoxy(pX, pY);
cout << " ";
gotoxy(pX, pY + 1);
cout << " ";
gotoxy(pX, pY + 2);
cout << " ";
gotoxy(pX, pY + 3);
cout << " ";
gotoxy(pX, pY + 4);
cout << " ";
}
void printplayer()
{
gotoxy(pX, pY);
cout << "!!!!!!o!!!!!!!";
gotoxy(pX, pY + 1);
cout << " || ";
gotoxy(pX, pY + 2);
cout << " ___( )___ ";
gotoxy(pX, pY + 3);
cout << "/~~~~~~~~~~~\\";
gotoxy(pX, pY + 4);
Anas Faiz 2023-CS-26
Grow
void moveplayerleft()
{
if (getCharAtxy(pX - 2, pY) == ' ' && getCharAtxy(pX - 2, pY + 1) == ' ' &&
getCharAtxy(pX - 2, pY + 2) == ' ')
{
eraseplayer();
pX = pX - 3;
printplayer();
}
}
void moveplayerright()
{
if (getCharAtxy(pX + 15, pY) != '#')
{
eraseplayer();
pX += 3;
printplayer();
}
}
void removefire()
{
gotoxy(bX, bY);
cout << " ";
}
void moveBullet()
{
removefire();
if (getCharAtxy(bX, bY) != '#')
{
bY -= 1; // Move up
}
else
{
playerbulletcheck = false;
}
if (getCharAtxy(bX, bY) == '_')
{
if (enemyhealth1 != 0)
{
enemyhealth1 -= 50;
}
playerbulletcheck = false; // Bullet should be inactive after hitting an enemy
removefire();
}
removefire();
}
-------------------Instructions--------------------------------
1: Press right arrow key to move player right.
2: Press left arrow key to move player left.
3: Press up space key to shoot bullet from the tank of player.
4: Enemies will spawn randomly on screen and you need
to kill them by shooting at them with your bullet.
5: Press escape key to exit the game.
6: Avoid player's contact with enemy bullet and kill them before they do.
7: You can see health bar of enemies in bottom part of the screen.
)";
getch();
}
void enemyhealth()
{
gotoxy(2, 28);
cout << "Enemy1: " << enemyhealth1;
gotoxy(16, 28);
cout << "Enemy2: " << enemyhealth2;
gotoxy(32, 28);
cout << "Enemy3: " << enemyhealth3;
}
void moveEnemyBullet()
{
Anas Faiz 2023-CS-26
Grow
gotoxy(enemey3bulletX, enemey3bullety);
cout << "o";
}
}
void enemyShoot()
{
if (!enemy1bulletcheck)
{
enemey1bulletX = eX + 7;
enemey1bulletY = eY + 2;
enemy1bulletcheck = true;
}
if (!enemy2bulletcheck)
{
enemey2bulletX = eX2 + 7;
enemey2bulletY = eY2 + 2;
enemy2bulletcheck = true;
}
if (!enemy3bulletcheck)
{
enemey3bulletX = eX3 + 7;
enemey3bullety = eY3 + 2;
enemy3bulletcheck = true;
}
}
void printplayerhealth()
{
gotoxy(84, 2);
cout << "Player Health: " << endl;
gotoxy(88, 3);
cout << " ";
Anas Faiz 2023-CS-26
Grow
gotoxy(88, 3);
cout << playerhealth;
}
char getCharAtxy(short int x, short int y)
{
CHAR_INFO ci;
COORD xy = {0, 0};
SMALL_RECT rect = {x, y, x, y};
COORD coordBufSize;
coordBufSize.X = 1;
coordBufSize.Y = 1;
return ReadConsoleOutput(GetStdHandle(STD_OUTPUT_HANDLE), &ci, coordBufSize,
xy, &rect) ? ci.Char.AsciiChar : ' ';
}
void gotoxy(int x, int y)
{
COORD coordinates;
coordinates.X = x;
coordinates.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordinates);
}
void gamelost()
{
cout << R"(
____ __ _ _ _
/ ___| __ _ _ __ ___ ___ / _(_)_ __ (_)___| |__
| | _ / _` | '_ ` _ \ / _ \ | |_| | '_ \| / __| '_ \
| |_| | (_| | | | | | | __/ | _| | | | | \__ \ | | |
\____|\__,_|_| |_| |_|\___| |_| |_|_| |_|_|___/_| |_|
\ \ / /__ _ _ | | ___ ___| |_
\ V / _ \| | | | | |/ _ \/ __| __|
)";
}
void gamewon()
{
cout << R"(
____ __ _ _ _
/ ___| __ _ _ __ ___ ___ / _(_)_ __ (_)___| |__
| | _ / _` | '_ ` _ \ / _ \ | |_| | '_ \| / __| '_ \
| |_| | (_| | | | | | | __/ | _| | | | | \__ \ | | |
\____|\__,_|_| |_| |_|\___| |_| |_|_| |_|_|___/_| |_|
\ \ / /__ _ _ \ \ / /__ _ __
\ V / _ \| | | | \ \ /\ / / _ \| '_ \
| | (_) | |_| | \ V V / (_) | | | |
|_|\___/ \__,_| \_/\_/ \___/|_| |_|
)";
}
void printfinallevel()
{
cout << R"(
_____ _ _ _ _
| ___(_)_ __ __ _| | | | _____ _____| |
| |_ | | '_ \ / _` | | | |/ _ \ \ / / _ \ |
| _| | | | | | (_| | | | | __/\ V / __/ |
|_| |_|_| |_|\__,_|_| |_|\___| \_/ \___|_|
)";
Anas Faiz 2023-CS-26
Grow
}
void printfinalmaze()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
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 << "# * #" << endl;
cout << "# * #" << endl;
cout << "# * #" << endl;
cout << "# #" << endl;
cout << "# #" << endl;
cout << "# #" << endl;
Anas Faiz 2023-CS-26
Grow
gotoxy(88, 3);
cout << finalplayerhealth;
}
void printfinalenemyhealth()
{
gotoxy(20, 28);
cout << "Enemy: ";
gotoxy(27, 28);
cout << " ";
gotoxy(27, 28);
cout << finalenemyhealth;
}
void finalenemyshoot()
{
if (!checkfinalenemybullet)
{
finalenemybulletX = enemyfX;
finalenemybulletY = enemyfY + 2;
checkfinalenemybullet = true;
}
}
void printfinalenemybullet()
{
if (checkfinalenemybullet)
{
gotoxy(finalenemybulletX, finalenemybulletY);
cout << "O";
}
}
void movefinalenemybullet()
{
Anas Faiz 2023-CS-26
Grow
}
else
{
checkfinalplayerbullet = false;
}
if (getCharAtxy(playerbulletfX + 1, playerbulletfY) == ':' || getCharAtxy(playerbulletfX +
1, playerbulletfY) == '!')
{
if (finalenemyhealth != 0)
{
finalenemyhealth -= 50;
}
checkfinalplayerbullet = false; // Bullet should be inactive after hitting an enemy
erasefinalplayerbullet();
}
if (getCharAtxy(playerbulletfX + 2, playerbulletfY) == '#' || getCharAtxy(playerbulletfX +
2, playerbulletfY) == '*')
{
checkfinalplayerbullet = false;
erasefinalplayerbullet();
}
else
{
gotoxy(playerbulletfX, playerbulletfY);
cout << "O";
}
}
int movefinalenemy(int digit)
{
{
erasefinalenemy();
if (checkfinalenemy)
{
if (digit == 0)
{
enemyfY += 3;
}
else
{
enemyfY -= 3;
}
if (enemyfY >= 23)
{
enemyfY = 2;
}
if (enemyfY <= 2)
{
enemyfY = 23;
}
printfinalenemy();
}
}
}
void movefinalplayerup()
{
if (getCharAtxy(playerfX, playerfY - 1) != '#' && playerfY > 2)
{
eraseplayerfinal();
playerfY -= 1;
printplayerfinal();
}
}
Anas Faiz 2023-CS-26
Grow
void movefinalplayerdown()
{
if (getCharAtxy(playerfX, playerfY + 1) != '#' && playerfY < 23) //== ' ' &&
getCharAtxy(pX - 2, pY + 1) == ' ' && getCharAtxy(pX - 2, pY + 2) == ' ')
{
eraseplayerfinal();
playerfY = playerfY + 1;
printplayerfinal();
}
}