1,105,197 Community Members

Console game help!

Member Avatar
Triarius
Newbie Poster
8 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi!
I'm making a console game with cars. I am stuck trying to make the enemy car approach.
If you have any thoughts or ideas, let me know!
I'm thinking of using classes, but i dont know how to make them :)
Is this legal? It probably isn't but here goes nothing...

//What I'm trying to do here is make new object without constructing them one by one.
     //Is that even possible?
     for(int i = 0;;i++)
     {
          constructor name(i);
     }

If not, how would I make something like this?
Don't be angry if you find some* noob mistakes. I'm just a beginner. Even though I have been "learning" C++ for some years, I have just started seriously programming.
Thanks in a advance.

//Car Game
//The goal of this game is not to hit another approaching car.
//You get 100 points for every car you pass.
//You move by using LEFT and RIGHT
#include <ctime>
#include <conio.h>
#include <windows.h>
#include <iostream>
using namespace std;

void printTrack();
bool move();
void enemyCome();

char track[20][9]={//0   1   2   3   4   5   6   7   8
		   {'#',' ',' ',' ',' ',' ',' ',' ','#'}, //0	 full
		   {'#',' ',' ',' ',' ',' ',' ',' ','#'}, //1	 full
		   {'#',' ',' ',' ',' ',' ',' ',' ','#'}, //2	 full
		   {'#',' ',' ',' ',' ',' ',' ',' ','#'}, //3	 full 
		   {'#',' ',' ',' ',' ',' ',' ',' ','#'}, //4	 free
		   {'#',' ',' ',' ',' ',' ',' ',' ','#'}, //5	 free
		   {'#',' ',' ',' ',' ',' ',' ',' ','#'}, //6	 free
		   {'#',' ',' ',' ',' ',' ',' ',' ','#'}, //7	 free
		   {'#',' ',' ',' ',' ',' ',' ',' ','#'}, //8	 full
		   {'#',' ',' ',' ',' ',' ',' ',' ','#'}, //9	 full
		   {'#',' ',' ',' ',' ',' ',' ',' ','#'}, //10	 full
		   {'#',' ',' ',' ',' ',' ',' ',' ','#'}, //11	 full
		   {'#',' ',' ',' ',' ',' ',' ',' ','#'}, //12	 free
		   {'#',' ',' ',' ',' ',' ',' ',' ','#'}, //13	 free
		   {'#',' ',' ',' ',' ',' ',' ',' ','#'}, //14	 free
		   {'#',' ',' ',' ',' ',' ',' ',' ','#'}, //15	 full
		   {'#',' ',' ',' ',' ',' ',' ',' ','#'}, //16	 full
		   {'#',' ',' ',' ',' ',' ',' ',' ','#'}, //17	 full
		   {'#',' ',' ',' ',' ',' ',' ',' ','#'}, //18	 full
		   {'#',' ',' ',' ',' ',' ',' ',' ','#'}};//19	 free
		//   0   1   2   3   4   5   6   7   8
bool trackPos = true;

char car[4][3], car1[4][3], car2[4][3] =  {    {' ',219,' '},
		       {219,219,219},
		       {' ',219,' '},
		       {219,' ',219}};
		
char player[4][3]={    {' ',219,' '},
       		       {219,' ',219},
		       {' ',219,' '},
		       {219,' ',219}};	       
bool playerIsLeft = true;

int main()
{
	enemyCome();
	for(;;)
	{	
		system("cls");
		printTrack();
		trackPos = !trackPos;
		move();
		Sleep(300);
	}
}

int enemyCarNr = 0;

//Prints the track
void printTrack(){
	for (int i = 0;i < 20; i++)
	{
		for(int j = 0; j < 9; j++)
		{	
			if(enemyComing[enemyCarNr] = 1)	
			//Changes the edges of the road, making an illusion of moving
			else if(j == 0 || j == 8)
			{
				if(trackPos == true)
				{
					if( i % 2 == 0)
					{
						cout << "!";
					}
					else
					{
						cout << "#";
					}
				}
				else if(trackPos == false)
				{
					if(i % 2 == 1)
					{
						cout << "!";
					}
					else
					{
						cout << "#";
					}
				}
			}
			//Prints player on the left side
			else if (playerIsLeft == true && i >= 16 && j > 0 && j < 4)
			{
				for(int l = 0; l < 3; l++)
				{
					cout << player[i-16][l];
				}
				j+=2;
			}
			//Prints player on the right side
			else if (playerIsLeft == false && i >= 16 && j > 4 && j <8)
			{
				for(int l = 0; l < 3; l++)
				{
					cout << player[i-16][l];
				}
				j+=2;
			}
			//Prints track normally
			else
			{
			cout << track[i][j];
			}
		}
		cout << endl;
	}
	enemyCarNr++;	
}

//Gets player's move and changes his position
//This part I copied, so I have very little idea what it does ;)
bool move()
{
	if(GetAsyncKeyState(VK_LEFT))
	{
		playerIsLeft = true;
		return playerIsLeft;
	}
	else if(GetAsyncKeyState(VK_RIGHT))
	{
		playerIsLeft = false;
		return playerIsLeft;
	}

        //This is how I wanted to make it, but it turns out getch() waits for input, so I
        //Couldn't do it like this :/
	/*char playerMove;
	playerMove = _getch();
	if(playerMove == 'a' || playerMove == 'A')
	{
		playerIsLeft = true;
		return playerIsLeft;
	}	
	else
	{
		playerIsLeft = false;
		return playerIsLeft;
	}
	*/
}

http://www.youtube.com/watch?v=LYaJZfBQipo
This is what I am trying to make. Sorry I couldn't find any better footage and I am ashamed of this, but it might make things clearer ie. what I'm trying to make.
*read "many"

Member Avatar
Labdabeta
Master Poster
761 posts since Feb 2011
Reputation Points: 161 [?]
Q&As Helped to Solve: 42 [?]
Skill Endorsements: 6 [?]
 
0
 

I would absolutely suggest using classes. Something like this will do nicely:

class enemyCar
{
    private:
    char car[4][3]=//put your cars' code here
    int x;
    int y;
    public:
    enemyCar(/*any constructor parameters you may want*/);//this is the constructor
    void step();//executes one turn for this car
    void draw();//draw the car in the correct spot
};

Once you have this class (with the functions filled in of course) you can create and use enemy cars in something similar to this:

enemyCar *enemies=new enemyCar[/*number of enemies you want*/];
while (/*player is alive*/)
{
    for (int i=0; i</*number of enemies you want*/; i++)
    {
        enemies[i].step();
        enemies[i].draw();
    }
}

Hope this helps!

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: