First let me say I stole the base of the code from this video
http://www.youtube.com/watch?v=FmNW8mCiZ9w
However, I thought once I watched the video I would continue adding and make something useful. I am extremely new to coding, but I thought something like this would keep me interested and going. So please take a look at my code, make some suggestions, provide easier ways of doing things, and enjoy!
#include <iostream>
#include "windows.h"
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
using namespace std;
//begin game
bool game_running = true;
//define map
string PlayAgain = "y";
int main()
{ do{
//draw map
char map[11][21] = {
"####################",
"#D #",
"# #",
"# #",
"# #",
"# #",
"# 0 #",
"# #",
"# #",
"####################"
};
//declare starting position and score
int x = 1;
int y = 1;
int score = 0;
system("cls");
//Print Instructions
cout << "Use arrows to move. \n";
cout << "Play for as long as you can before you are trapped! \n";
cout << "Once you are trapped press esc to end game." << endl<< flush;
system("PAUSE");
//define actions of game
while(game_running == true){
system("cls");
for (int display=0; display<10; display++){
cout << map[display] << endl;
}
system("pause>nul");
srand(time(0));
if(GetAsyncKeyState(VK_DOWN)){
int y2 = y + 1;
int y3 = 1+ (rand()%8);
int x3 = 1+ (rand()%18);
if(map[y2][x]== ' '){
map [y][x] = ' ';
y++;
map [y][x] = 'D';
}
if(map[y2][x]=='0'){
map [y3][x3] = '0';
map [y][x] = '@';
y++;
map [y][x] = 'D';
score++;
}
}
if(GetAsyncKeyState(VK_UP)){
int y2 = y - 1;
int y3 = 1+ (rand()%8);
int x3 = 1+ (rand()%18);
if(map[y2][x]== ' '){
map [y][x] = ' ';
y--;
map [y][x] = 'D';
}
if(map[y2][x]=='0'){
map [y3][x3] = '0';
map [y][x] = '@';
y--;
map [y][x] = 'D';
score++;
}
}
if(GetAsyncKeyState(VK_RIGHT)){
int x2 = x + 1;
int y3 = 1+(rand()%8);
int x3 = 1 +(rand()%18);
if(map[y][x2]==' '){
map [y][x] = ' ';
x++;
map [y][x] = 'D';
}
if(map[y][x2]=='0'){
map [y3][x3] = '0';
map [y][x] = '@';
x++;
map [y][x] = 'D';
score++;
}
}
if(GetAsyncKeyState(VK_LEFT)){
int x2 = x - 1;
int y3 = 1+ (rand()%8);
int x3 = 1+ (rand()%18);
if(map[y][x2]==' '){
map [y][x] = ' ';
x--;
map [y][x] = 'D';
}
if(map[y][x2]=='0'){
map [y3][x3] = '0';
map [y][x]= '@';
x--;
map [y][x] = 'D';
score++;
}
}
if(GetAsyncKeyState(VK_ESCAPE)){
game_running = false;
}
}
system("cls");
//Give option to play againg.
cout << "Game Over...Your score is " << score << endl;
cout << "Would you like to play again? y/n" << endl;
cin >> PlayAgain;
if(PlayAgain == "y"){
game_running = true;
}
}while(PlayAgain == "y");
if(PlayAgain != "y")
{
cout << "Thank you for playing!"<<endl;
}
system("PAUSE");
return 0;
}