Mar 19, 2015 at 10:18am UTC
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <Windows.h>
using namespace std;
int input, position = 1, timescore, point_count, point_count2;
string line;
bool game = false, options = false, menu = true;
char map[25][48] = {
" +---------------------------+",
" | # |",
" |..###..####..#..####..###..|",
" |..###..####..#..####..###..|",
" |...........................|",
" |..###..#..#######..#..###..|",
" |.......#..#######..#.......|",
" |.####..#.....#.....#..####.|",
" |.####..####..#..####..####.|",
" |.####..#...........#..####.|",
" |.####..#..###.###..#..####.|",
" |.......#.....#.............|",
" |.####..#..#######..#..####.|",
" |.####..#...........#..####.|",
" |.####..#..#######..#..####.|",
" |.####..#..#######..#..####.|",
" |.............#.............|",
" |.###.........#........###..|",
" |...#..................#....|",
" |##.#...#..#######..#..#..##|",
" |.......#.....#.....#.......|",
" |...########..#..########...|",
" |...########..#..########...|",
" | |",
" +---------------------------+",
};
void drawMenu()
{
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;
if (position == 1)
{
cout << " +---------------------+" << endl;
cout << " | [PLAY] |" << endl;
cout << " |---------------------|" << endl;
}
else
{
cout << " +---------------------+" << endl;
cout << " | PLAY |" << endl;
cout << " |---------------------|" << endl;
}if (position == 2)
{
cout << " | [INFO] |" << endl;
cout << " |---------------------|" << endl;
}
else
{
cout << " | INFO |" << endl;
cout << " |---------------------|" << endl;
}if (position == 3)
{
cout << " | [EXIT] |" << endl;
cout << " +---------------------+" << endl;
}
else
{
cout << " | EXIT |" << endl;
cout << " +---------------------+" << endl;
}
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << " .-. .-. .--. " << endl;
cout << " | OO| | OO| / _.-' .-. .-. .-. .-. " << endl;
cout << " | | | | | '-. '-' '-' '-' '-' " << endl;
cout << " '^^^' '^^^' '--' " << endl;
cout << endl;
}
void reading()
{
system("cls");
drawMenu();
while (true){
if (GetAsyncKeyState(VK_DOWN) && position < 3)
{
Sleep(100);
position++;
system("cls");
drawMenu();
}
else if (GetAsyncKeyState(VK_UP) && position > 1)
{
Sleep(100);
position--;
system("cls");
drawMenu();
}
else if (GetAsyncKeyState(VK_RETURN))
{
if (position == 1)
{
game = true;
menu = false;
break;
}
else if (position == 2)
{
options = true;
menu = false;
break;
}
else if (position == 3)
exit(0);
}
else
Sleep(150);
}
}
void drawMap()
{
system("cls");
cout << endl;
cout << endl; cout << endl;
cout << endl;
for (int i = 0; i < 25; i++){
for (int j = 0; j < 47; j++)
cout << map[i][j];
cout << endl;
}
}
void finish()
{
system("cls");
cout << endl;
cout << endl;
cout << " Your time is: " << timescore / 5 << " seconds!";
cout << endl;
cout << " Your score of points is: " << point_count;
cout << endl;
cout << " Your score of special points is: " << point_count2;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << " [The game will exit in 10 seconds...]";
Sleep(10000);
exit(0);
}
void gameRunning()
{
system("cls");
int x = 19, y = 1, c = 0, px, py, score = 0;
srand(time(NULL));
map[y][x] = 1;
drawMap();
while (c != 9)
{
px = rand() % 49;
py = rand() % 26;
if (map[py][px] == '.')
{
map[py][px] = '@';
c++;
}
}
drawMap();
while (true)
{
if (point_count == 334)
{
finish();
point_count = 0;
point_count2 = 0;
map[y][x] = ' ';
c = 0;
y = 1;
x = 19;
map[y][x] = 1;
break;
}
if (point_count2 == 3)
{
system("color E1");
}
if (point_count2 == 6)
{
system("color 1E");
}
if (point_count2 == 9)
{
finish();
point_count = 0;
point_count2 = 0;
map[y][x] = ' ';
c = 0;
y = 1;
x = 19;
map[y][x] = 1;
break;
}
if (GetAsyncKeyState(VK_UP) && map[y - 1][x] == ' ')
{
map[y][x] = ' ';
y--;
map[y][x] = 1;
}
else if (GetAsyncKeyState(VK_DOWN) && map[y + 1][x] == ' ')
{
map[y][x] = ' ';
y++;
map[y][x] = 1;
}
else if (GetAsyncKeyState(VK_LEFT) && map[y][x - 1] == ' ')
{
map[y][x] = ' ';
x--;
map[y][x] = 1;
}
else if (GetAsyncKeyState(VK_RIGHT) && map[y][x + 1] == ' ')
{
map[y][x] = ' ';
x++;
map[y][x] = 1;
}
if (GetAsyncKeyState(VK_UP) && map[y - 1][x] == '.')
{
map[y][x] = ' ';
y--;
map[y][x] = 1;
point_count++;
}
else if (GetAsyncKeyState(VK_DOWN) && map[y + 1][x] == '.')
{
map[y][x] = ' ';
y++;
map[y][x] = 1;
point_count++;
}
else if (GetAsyncKeyState(VK_LEFT) && map[y][x - 1] == '.')
{
map[y][x] = ' ';
x--;
map[y][x] = 1;
point_count++;
}
else if (GetAsyncKeyState(VK_RIGHT) && map[y][x + 1] == '.')
{
map[y][x] = ' ';
x++;
map[y][x] = 1;
point_count++;
}
if (GetAsyncKeyState(VK_UP) && map[y - 1][x] == '@')
{
map[y][x] = ' ';
y--;
map[y][x] = 1;
point_count2++;
}
else if (GetAsyncKeyState(VK_DOWN) && map[y + 1][x] == '@')
{
map[y][x] = ' ';
y++;
map[y][x] = 1;
point_count2++;
}
else if (GetAsyncKeyState(VK_LEFT) && map[y][x - 1] == '@')
{
map[y][x] = ' ';
x--;
map[y][x] = 1;
point_count2++;
}
else if (GetAsyncKeyState(VK_RIGHT) && map[y][x + 1] == '@')
{
map[y][x] = ' ';
x++;
map[y][x] = 1;
point_count2++;
}
Sleep(50);
timescore++;
drawMap();
cout << endl;
cout << endl;
cout << " [POINTS]--->" << point_count << "/334";
cout << endl;
cout << " [SPECIAL POINTS]--->" << point_count2 << "/9";
cout << endl;
cout << " [TIME]--->" << timescore / 5;
}
}
void drawOptions()
{
system("cls");
while (true)
{
cout << endl << " Press [Up],[DOWN],[LEFT],[RIGHT] to move.\n\n Collect all the points or the special points to win. \n \n \n";
cout << endl;
cout << endl;
cout << " >>>[Back]<<<";
if (GetAsyncKeyState(VK_RETURN))
{
menu = true;
options = false;
break;
}
Sleep(150);
system("cls");
}
}
int main()
{
system("color 1E");
system("mode con lines=40 cols=69");
while (true){
if (game)
gameRunning();
if (menu)
reading();
if (options)
drawOptions();
}
}
Mar 19, 2015 at 6:33pm UTC
good :)
but there is few notes, which i think they can improve your coding and programming skills.
1) i suggest using OOP to make programs like this, its very useful and if you practice enough and become a pro. in OOP, it will help you in future.(specially because you are using c++)
2) try to write your functions in a header file, and then use them in your main file, it will help you to keep your codes more clear and understandable.
3)try to keep away from using "system" function, its better to use non-OS-based functions.
BUT, this program is very good for a beginner.
keep going.
--------
Be Best in Your Life.
S.Amir Mohammad Hassanli.