0% found this document useful (0 votes)
7 views9 pages

New Text Document

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

New Text Document

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

#include<iostream>

#include<conio.h>
#include<time.h>
#include<ctime>
#include<iomanip>
#include<ctime>
#include<cstdlib>
#include<fstream>
#include<windows.h>

const int row = 20;


const int cols = 40;
int life = 3;
int score = 0;
int distance = 0;
int colDist = 0;

int h_i[3]{ 2,5,18 };


int h_j[3]{ 10,18,36 };

int c_i[3]{ 4,10,17 };


int c_j[3]{ 5,25,35 };

using namespace std;


void stage();
void character();
void print();
void Colors();

void Coins();
void movement();
void playGame();
void nameOfPlayer();
void mainMenu();
void gameplay();
void gameHistoryOnFile();
void gameHistoryInFile();
void loadGame();
void mainScreen();

string name;
char ch;
int n, turn = -1, inpt = 1;
bool win = false, flag = false;

char map[row][cols];

int x = 17, y = 20;//location of object


int cx = 2, cy = 20;//location of computer player

int main()
{
mainScreen();
loadGame();
mainMenu();
system("pause");
return 0;

}
void mainScreen() {
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\tENDLESS";
Sleep(1000);
system("cls");
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\tRUNNER";
Sleep(1000);
system("cls");
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\tGAME!";
Sleep(1000);
system("cls");
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\tENDLESS RUNNER GAME!
";
system("pause");
system("cls");
}
void playGame() //playGame in the menu
{
nameOfPlayer();
while (life != 0)
{
if (win == true) {
break;
}
stage();
character();
print();
movement();
Coins();
::distance += 50;

colDist += 50;
Colors();
system("cls");
}

cout << "Well PLayed!!!" << endl << "your score is


: " << ::score << endl;
system("pause");
return;
}

void gameplay()
{
cout << "1.The game will start with the player character running
automatically" << endl;
cout << "2.The player can control the character's movement by using
arrow keys jump,
sliding,left,right, forward, backward." << endl;
cout << endl;
cout << "left movement=left arrow key\n";
cout << "right movement=right arrow key\n";
cout << "forward movement=up arrow key\n";
cout << "backward movement= down arrow key\n";
cout << endl;
cout << "3.The player character will have three lives at the start of
the game, and they will lose a
life if they hit an obstacle." << endl;
cout << "4.The game will end when the player runs out of lives."
<< endl;
cout << "5.The player character will collect coins while running, and
the score will increase with
each coin collected." << endl;
cout << "6.The distance covered by the player character will be
measured, and the game's speed
will increase as the distance increases." << endl;
cout << "7.When the distance covered by the player character exceeds
2000 meters, a computer
player will be introduced in the game that will follow the player character and try
to collide with them."
<< endl;

cout << "8.The player cannot pass through the walls on the side."
<< endl;
cout << "\n || enter x to return to main menu ||
\n ";

do
{
cin >> ch;
} while (ch != 'x');
system("cls");

}
void loadGame() {
cout << "\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\tLoading.";
Sleep(1000);
system("cls");
cout << "\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\tLoading..";
Sleep(1);
system("cls");
cout << "\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\tLoading...";
Sleep(1000);
system("cls");
}
void mainMenu()
{
cout << " ------------------------------------- " << endl;
cout << "| 1- PLAY GAME |" << endl;
cout << "| 2- GAME PLAY |" << endl;
cout << "| 3- GAME HISTORY |" << endl;
cout << "| 4- EXIT |" << endl;

cout << " -------------------------------------" << endl;


cout << "what would you like ? " << endl;
do
{
cin >> ch;
switch (ch)
{
case '1':
system("cls");
playGame();
gameHistoryOnFile();
break;
case'2':
system("cls");
gameplay();
if (ch == 'x')
{
mainMenu();
system("cls");
}
break;

case'3':
system("cls");
gameHistoryInFile();
if (ch == 'x')
mainMenu();
break;
case'4':

system("cls");
system("pause");
cout << "thank you for playing the game ! " << endl;
return;
break;

default:
cout << "invalid character input , enter again " << endl;
}

} while (ch > 52 && ch < 49);


return;

}
void nameOfPlayer() //takes name of player
{
cout << "enter your name player " << endl;
cin >> name;
}
void stage()
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < cols; j++)
{
map[i][j] = ' ';
if (i == 0)
{
map[i][j] = '-';

}
if (i == 19)
{
map[i][j] = '-';
}
if (j == 0)
{
map[i][j] = '|';
}
if (j == 39)
{
map[i][j] = '|';
}

}
}
}

void Colors() {
if (colDist < 1000) {
system("Color 0E");
}
else if (colDist < 2000) {
system("Color 0A");
}
else if (colDist < 3000) {
system("Color 0B");
}
else if (colDist < 4000) {

system("Color 0C");
}
else if (colDist < 5000) {
system("Color 0D");
}
else if (colDist > 5000) {
colDist -= 5000;
}

}
void character()
{
for (int i = 0; i < 3; i++)
{
if (h_i[i] == x && h_j[i] == y)
{
life--;
h_i[i] = rand() % 20;
h_j[i] = rand() % 40;
}
}

for (int i = 0; i < 3; i++)


{
h_i[i]++;
h_j[i];

if (h_j[i] == 40)
h_j[i] = 1;

if (h_i[i] == 20)
h_i[i] = rand() % 20;

if (::distance > 2000)


{
if (cx < x)
{
cx++;
}
else if (cx > x)
{
cx--;
}

if (cy < y)
{
cy++;
}
else if (cy > y)
{
cy--;
}

if (cx == x && cy == y)
{
life--;
cx = rand() % 20;

cy = rand() % 40;
}
}

map[x][y] = 'X';
map[x][y - 1] = '|';
map[x + 1][y] = '-';
map[x - 1][y] = '^';
map[x][y + 1] = '|';

if (::distance > 2000)


{
map[cx][cy] = 'O';
}
}
void Coins()
{
for (int i = 0; i < 3; i++)
{
if (c_i[i] == x && c_j[i] == y)
{
score = score + 10;
c_i[i] = rand() % 20;
c_j[i] = rand() % 40;
}
}

for (int i = 0; i < 3; i++)


{

c_i[i]++;
c_j[i];

if (c_j[i] == 40)
c_j[i] = 1;

if (c_i[i] == 20)
c_i[i] = rand() % 20;

for (int i = 0; i < 3; i++) {


map[c_i[i]][c_j[i]] = '$';
}
}
void print()
{
character();
Coins();
for (int i = 0; i < row; i++)
{
for (int j = 0; j < cols; j++)
{
if (h_i[0] == i && h_j[0] == j)
{
cout << "*";
}

else if (h_i[1] == i && h_j[1] == j)


{

cout << "*";


}

else if (h_i[2] == i && h_j[2] == j)


{
cout << "*";
}

else
{
cout << map[i][j];
}
}
cout << endl;
}

cout << " LIFES : " << life << " SCORE : "
<< score << " DISTANCE: " << ::distance << endl;

}
void movement()
{
char ch;
ch = _getch();

if (int(ch) == 'H')//for up movement


{
if (x > 10)
{
x = x - 1;

}
else if (int(ch) == 'P')//for down movement
{
if (x < 18)
{
x = x + 1;
}
}
else if (int(ch) == 'M')//for right movement
{
if (y < 37)
{
y = y + 1;
}

}
else if (int(ch) == 'K')//for left movement
{
if (y > 2)
{
y = y - 1;
}

else if (int(ch) == ' ')//for jump


{
if (x > 10)
{
x = x - 3;
}

}
void gameHistoryOnFile() //puts data in the file
{
ofstream fout;
fout.open("Endless runner Game History", ios::app);
if (!fout.is_open())
{
std::cout << "file not found \n";
return;
}

fout << endl;


fout << "name of player : " << name << endl;
fout << "score " << score << endl;
fout.close();

return;
}

void gameHistoryInFile() //reads data in file


{
char ch1;
ifstream fin;
fin.open("Endless Runner Game History", ios::app);
if (!fin.is_open())
{
std::cout << "file not found \n";
return;
}
while (!fin.eof())
{
fin.get(ch1);
std::cout << ch1;
}
fin.close();
std::cout << " || enter x to return to main menu ||
\n ";
do
{
cin >> ch;
} while (ch != 'x');
std::system("cls");
return;
}

You might also like