First Simple Game

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;
}


jkb52088 wrote:
#include "windows.h"
"Windows.h" has a capital W. Ironically, this only matters if you're not on Windows.
jkb52088 wrote:
1
2
3
#include <cstdlib>
//...
#include <stdlib.h> 
These are the same header except that <cstdlib> is the C++ version and <stdlib.h> is the C version. Stick with the C++ version.
jkb52088 wrote:
using namespace std;
I assume by now you know why this is bad. If not, Google search it ;)
jkb52088 wrote:
1
2
3
4
5
//begin game
bool game_running = true;
//define map

string PlayAgain = "y";
It is bad to declare variables outside of functions.
jkb52088 wrote:
1
2
int main()
{ do{
This indentation style will make things harder for you.
jkb52088 wrote:
system("cls");
system() is bad - don't use it.
jkb52088 wrote:
cout << "Once you are trapped press esc to end game." << endl<< flush;
std::endl already flushes the stream, there is no need to use std::flush ;)
jkb52088 wrote:
1
2
3
    while(game_running == true){
    //...
    srand(time(0));
Doing this defeats the purpose of randomization entirely. Seed once, at the start of main, and never again - don't seed in a loop.
jkb52088 wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
    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;
    }
If I enter anything but "y", it exits the loop, then enters the if statement,which is redundant; you already know it isn't "y".
Wow, Thanks for the quick reply. I will review why I did things the way I did them. However, I'm heading for the sack. The reality is I am teaching myself c++, and I have never programmed before. I'm beginning my career, and considering going back to school. I just wanted to see if programming even interests me. I can assure you I am far from know much at all. I am watching Bucky's videos, and currently only on 14 of 73. I just had to get away from printing stuff out. I need to challenge myself to do something more.
I'm glad you are wanting to challenge yourself :) but a common mistake everyone makes is staying in the console for too long and trying to use it for games. If you think you're ready for doing something like that and you can handle the learning curve, you should move to a graphics library like SFML or SDL (I prefer SFML). The console isn't meant for games but it is excellent for test programs.
Ok, here is an update. I think i fix the areas you address. I originally typed "window.h" because that's what the video had so that is my ignorance. Also with the second issue. I searched how to do something and it said it came from that library. I can't remember exactly, but deleted from code just not the library. I searched why it's bad to not use using namespace. Does that mean it just bad practice to use the using namespace statement all the time? Is there a time it's practically or just stay away from it? On declaring out side of function I didn't realize I wasn't in main. I though I was thanks for catching that. With the do function I didn't take time to make code look good. The flush I think comes back to the stdlib.h bit I believe. Please expain on randomization. It was new to me. I knew I could chose a random number and I just pulled from Bucky's video on a random number generator. Lastly, that now makes perfect since thanks for pointing that out. By the way thanks for all the help. If you could show or suggest how I can avoid system(cls). I read some articles, but it just didn't sink in. Thanks


#include <iostream>
#include "Windows.h"
#include <cstdlib>




int main()
{
//begin game
bool game_running = true;

//give option to play again
std::string PlayAgain = "y";
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
std::cout << "Use arrows to move. \n";
std::cout << "Play for as long as you can before you are trapped! \n";
std::cout << "Once you are trapped press esc to end game." << std::endl;
system("PAUSE");


//define actions of game
while(game_running == true){
system("cls");

for (int display=0; display<10; display++){
std::cout << map[display] << std::endl;
}
system("pause>nul");


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.


std::cout << "Game Over...Your score is " << score << std::endl;
std::cout << "Would you like to play again? y/n" << std::endl;
std::cin >> PlayAgain;
if(PlayAgain == "y"){
game_running = true;

}
}while(PlayAgain == "y");


std::cout << "\nThank you for playing!"<<std::endl;

return 0;
}
Topic archived. No new replies allowed.