Programming a 2-D tank game

I'm working on my 2-D tank game for player vs computer.

Page 1 of 1

5 Replies - 8279 Views - Last Post: 13 December 2008 - 11:33 AM Rate Topic: -----

#1 codecsmash49   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 09-December 08

Programming a 2-D tank game

Post icon  Posted 09 December 2008 - 09:15 AM

I've been banging my head against a wall but i've made progress!
I'm stuck at these points:
1) making the robot move at random when the player is in range engage them.
2) checking to see if the weapons hit(grenade/laser) I'm guessing checking player positon with the X,Y for grenade and X or Y for laser.
3)Displaying the ascii grid
4) Player = ascii 193- North 194-South 195- East 180- West. How do i display so they rotate as the player changes direct? I was guessing a IF loop.
5) same for the Computer as number 4
6)Randoming the stats for the robot so they = 10 and do not go over, but can go under.
7) Laser i was going to use '.' for the laser or ascii #220. How can i display so i shoots across the map
8)Grenade- was going to use ascii 177 to show the area the grenade hit in.

*not needed but i would also like 2 try and drop random walls*

Any help or other problems you see i will love you for LIFE!
#include <cstdlib>
#include <iostream>
#include "color.h"
#include <conio.h>

using namespace std;
using namespace Petter;

void title();
void credits();
void intro();
void instructions();
int menu(char arrayGrid[][21]);
void displayGrid(char arrayGrid[][21]);
void begin();
void yourTank();
void yourTankRotate();
void compTank();
void compTankRotate();
void stats();
void compStats();
void shootL();
void shootG();
void shieldLeft();
void move();
void hit();
void direction();
void tankFace();



int main(int argc, char *argv[])
{
	//yourTank;
	//compTank;
	
	//yourTankY =2;
	//yourTankX = 2;
	//compTankY = 19;
	//compTankX = 19; 
	
	char arrayGrid[21][21]={
						{201,205,209,205,209,205,209,205,209,205,209,205,209,205,209,205,209,205,209,205,187},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{200,205,207,205,207,205,207,205,207,205,207,205,207,205,207,205,207,205,207,205,188},
						};
	
	
	title();
	credits();
	cout << endl;
	cout << endl;
	menu(arrayGrid);
	
	
	system("PAUSE");
	return EXIT_SUCCESS;
}
void title()
{
	cout << endl;
	cout << endl;
	cout << endl;
	cout << endl;
	cout <<  "\t\t\t" <<RED << " WELCOME TO M.I.M.E WARS!!!!!!!" << NORMAL << endl;   
}
void credits()
{
	cout << endl;
	cout << endl;
	cout << endl;
	cout << endl;
	cout << "\t\t\tCreated by: Michael Meyer" << endl;
	cout << "\t\t\tWith Help from: Mountain Dew" << endl;
}
int menu(char arrayGrid[][21])
{
	char choice;
	
	do
	{
	
	
	cout << "								 :[B]ATTLE!:   " <<endl;
	cout << "											   " <<endl;
	cout << "								:[S]STORY LINE:" <<endl;
	cout << "											   " <<endl;
	cout << "							   :[I]NSTRUCTIONS:" <<endl;
	cout << "											   " <<endl;
	cout << "							  :[E]XIT(chicken!?):	" <<endl;
	
	choice =getch(); // calls for input
	switch(choice)
	{
				   case 'b':
						{
							begin();
						}
				   break;
				   case 's':
						{
							intro();
						}
				   break;
				   case 'i':
						{
							instructions();
						}
				   break;
				   case 'e':
				   break;
	} 
	}
	while(choice != 'e');  

	return 0;
}
	 
void instructions();
{
	cout << "\t\t\t\t Instructions" << endl << endl << endl;

			cout << "Player controls:" << endl;
			cout << "W key: Progress the M.I.M.E forward, towards the way they are facing." << endl;
			cout << "A key: Rotates the M.I.M.E left." << endl;
			cout << "D key: Rotatest the M.I.M.E Counter right." << endl;
			cout << "E key: Chooses to fire the M.I.M.E's Lazer." << endl;
			cout << "Q key: Chooses to lob a M.I.M.E's Grenade." << endl;	 
	}
void into();
{
	cout << "\t\t\t\t Introduction" << endl << endl << endl;
			cout << "\tDuring the third World War a new breed of warefare was brought to life. M.I.M.E's were born to be a slave of men.";
		   Sleep(4000);
			cout << " M.I.M.E, Mechines In Main Engagement, were a far more advance design of A.I.(Artificial Intelegence).";
			Sleep(4000);
			cout << "There mechines   were designed to lower human casualties, but instead turned and increase the   death toll.";
			cout << endl;
			Sleep(4000);
			cout << "\tWhen the M.I.M.Es turned, it was nothing more then a silent killing of  those around them.";
			Sleep(4000);
			cout << "Man was once again forced back into traditional warfare of	opertaing there own mechines and demise.";
			cout << endl;
			cout << endl;
			cout << endl;
			Sleep(4000);
			cout << "Who will win!?!?.........";
			cout << endl;
			cout << endl;	
}

void displayGrid(char arrayGrid[][21] // display the grid + robots
{
	
}
void yourTank(); // your tank Green
{
	 
}

void yourTankRotate(); // Shows you tank rotating N, S, W, E
{
	
}

void compTank(); // computer tank Red
{
	
}

void compTankRotate(); // Shows computer tank rotating N, S, W, E
{
	
}

void stats();
{
	int shield; // HP
	int move; // actions per a round
	int weapon; // +damage
	int range; // how far your weapon shoots
	int total = range + shield + weapon + move; // total stats cannot acceed 10
	int remaining = 10 - range + shield + weapon + move; 
	
	 cout << "Choose the stats you would like for your M.I.M.E., with a max total of 10." << endl;
	 
	 while(total != 10)
	 {
		 cout << "Choose 1-6 for your range of your weapon(" << remaining << " points left: ";
		 cin >> range;
		 while((cin == 0) || (range < 0))
					{
						cin.clear();
						cin.ignore(10000, '\n');
						cout << "Please enter the amount of range: ";
						cin >> range;
					}
		 
		 cout << "Choose 1-6 for your shields(" << remaining << " points left: ";
		 cin >> shield;
		 while((cin == 0) || (shield < 0))
					{
						cin.clear();
						cin.ignore(10000, '\n');
						cout << "Please enter amount for the shield: ";
						cin >> shiel;
					}
		 
		 cout << "Choose 1-6 for your weapon damage(" << remaining << " points left: ";
		 cin >> weapon;
		 while((cin == 0) || (weapon < 0))
					{
						cin.clear();
						cin.ignore(10000, '\n');
						cout << "Please enter the amount for the weapon: ";
						cin >> weapon;
					}
		 
		 cout << "Choose 1-6 for your movement speed(" << remaining << " points left: ";
		 cin >> move;
		 while((cin == 0) || (move < 0))
					{
						cin.clear();
						cin.ignore(10000, '\n');
						cout << "Please enter the amount for movement: ";
						cin >> move;
					}
	}
}

void compStats(); // stats MUST be random 1-6 cannot accede 10 total
{
	srand(time(0)); // int random number
	int shield; // HP
	int move; // actions per a turn
	int weapon; // +damage
	int range; // how far weapon can shoot
	int total = range + shield + weapon + move;
	
	cout << "The computers stats are as shown: " << endl
	
	while(total != 10 )
	{
		range = (rand(0) % 6);
		shield = (rand(0) % 6);
		weapon = (rand(0) % 6);
		move = (rand(0) % 6);
	}
	
	cout << "Range is = " << range << endl;
	cout << "Shield is = " << shield << endl;
	cout << "Weapon is = " << weapon << endl;
	cout << "Movement is = " << move << endl;
}

void shootL(); // direct shot 2 damage
{
	 
}

void shootG(); // Area of affect dmg 3x3 1 dmg
{
	
}

void shieldLeft(); // remaining life points
{
	 int yourShield;
	 int compShield;
	 
	 cout << "Your shield left is: " << yourShield << endl;
	 
	 cout << "The computers shield left is: " << compShield << endl;
}

void direction(); // which direction you are facing to determine which way you can move
{				 // face must remain 1-4
	int face;
	char direction;
	
	if( face = 1)
		{
			direction = "North";
		}
	else if(face = 2)
		{
			direction = "West";
		}
	else if(face = 3)
		{
			direction = "South";	
		}
	else if(face = 4)
		{
			direction = "East"	
		}
	else( face > 4 || face < 1)
	{
		face = 1;
		direction = "North"; 
	}
	cout << "You are facing: " << direction << endl;
}

void yourTankFace();
{
	int face;
	
	if( face = 1)//North
		{
			  cout << GREEN << (char)193;
		}
	else if(face = 2)//South
		{
			cout << GREEN << (char)194;
		}
	else if(face = 3)//West
		{
			cout << GREEN << (char)180;	
		}
	else(face = 4)//East
		{
			cout << GREEN << (char)195;
		}
}

void compTankFace();
{
	int face;
	
	if( face = 1)//North
		{
			cout << RED << (char)193;
		}
	else if(face = 2)//South
		{
			cout << RED << (char)194;
		}
	else if(face = 3)//East
		{
			cout << RED << (char)180;
		}
	else(face = 4)//West
		{
			cout << RED << (char)195;	
		}	
}

void move(); // Move you forward
{
	 int choice;
	 int moveX;
	 int moveY;
	 
	 cout << "Choose a movement command." << endl;
	 cin >> choice
	 
	 switch(choice)
	 {
		case w: // move forward adding 1 to X or Y axis based on direction facing 
			while(x != 0 && x != 21 && y != 0 && y != 21)
			{
				if(int face = 2 || face = 4)
					{
						yourTankX + 1 = moveX;	
					}   
				else(int face = 1 || face = 3)
					{
						yourTankY + 1 = moveY;
					} 
			}
			
		case a: // rotates you to the left subtracting 1 from face
							
			
		case d: // rotates you to the right adding 1 to face
			
			
		case e: // fires THE LAZER! 
			hit();
			
		case q:  // Lobs the grenade
			hit();
	 }
}

void hit() // determines if the weapons hit
{
	 
}



Is This A Good Question/Topic? 0
  • +

Replies To: Programming a 2-D tank game

#2 Tom9729   User is offline

  • Segmentation fault
  • member icon

Reputation: 181
  • View blog
  • Posts: 2,642
  • Joined: 30-December 07

Re: Programming a 2-D tank game

Posted 09 December 2008 - 11:08 AM

Well first of all, since you're using C++ you might as well make a tank class. It will remove a lot of redundancy later.

Try to concentrate on getting everything working BEFORE you try to program the AI.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5375
  • View blog
  • Posts: 14,285
  • Joined: 18-April 07

Re: Programming a 2-D tank game

Posted 09 December 2008 - 11:35 AM

Ok, here are some tips and help figuring out a few of your problems....

1) Lets conquer some of the issues you already have with the syntax that is going to cause problems for you. First of all you have lines like if( face = 1). You are using an assignment operator, so you are not comparing. This is going to evaluate to true every time because an assignment always happens and is true. You will want if( face == 1). Double equal sign is for comparisons.

Secondly you may want to generate your numbers like range = (rand() % 6) + 1 so that way you won't have a range of 0 which makes no real sense unless the weapon is going to be disabled. The lowest value will be 1. Notice I also took out the 0, it is not needed here.

Lastly, I see you setup a char that is called "direction" and then you stuff a string into it. I think will be wanting char * direction to place the string into. Because as a normal char, it only holds one character.

2) Now for some of the questions...

Making the robot move at random when the player is in range. You are going to come up with a routine which is going to be run each time a player or computer tank moves which will start at each computer tank and scan the nearby boxes to see if a player is detected. This is essentially collision detection here. So lets say you have a game screen similar to this...

0 0 0 0 0 S S S
0 P S S 0 S C S
0 S C S 0 S S S
0 S S S 0 0 0 0



In the game board above P = The player tank and C = the computer tanks, S= box scanned at range 1. As you notice in the box the 8 boxes around each C is being scanned (could be done using a loop) and if a player is found, then you can take the appropriate action. As you see the Computer on the far right of the screen does not see the player in range. The Computer on the left however detects that the player is coming in from the northwest direction. Since the player is found there, the code can determine that south would be an ideal place to escape to and will face south, then move 1 space. It will then rescan to see if the player is still in sights.

2) Weapons hitting... first I think you should make two functions instead of one. One for the grenade and the other for the laser. Why? Because these two weapons will act differently and may grow more sophisticated in the future. Plus you may want more weapons later etc all with different attributes.

A laser may be as simple as a bishop in chess checking a diagonal on the board to see if any pieces are in the way. It would loop through the X, the Y, or the X and Y for the given number of squares to see if a computer tank is in the way. If a computer tank is found, it is hit. Exactly as you stated, the X and the Y are the same coordinates as the tank.

A grenade might be lobbed over other players or squares on the board. Lets say you wanted to eventually make this co-op play and there is another player on the board. A laser might hit your partner because it is straight on, but a grenade could be thrown over squares and land on or nearby the computer. Determine the X and Y of the grenade landing point and just like the scan for a computer tank mentioned in part 1 above, see if ANY tanks are in the X and Y square or surrounding it a certain number of squares. In our game board if the player lobbed a grenade to X = 5 and Y = 1 (remember arrays start at zero) you would have a grenade land just left of the C tank. It would then scan over and see the computer tank at coordinates X = 6, Y = 1 and damage it.

3) Displaying ascii grid... this is a simple nested for loop where you are going to loop through the column for each row of your grid 2D array. It will be looking for a player, a computer tank, or an empty space. You can then print the corresponding character.

4) Rotating players... this is a bit of a graphics thing than a simple ascii setup like you have here. If these were images instead of just chars, you would be activating an animation which would do the rotating. You are setup as a very rigid box grid here where you are dealing in for directions, not 360 like you would in a cartesian plane with points setup. This is something much more complicated than what you are doing here. The best you can hope for is that if you are facing west and you want to go east that you change your player char color to face south, then face east. That would be a simple loop and would only be for when you are changing from north to south (or vice versa) or from west to east (or vice versa).

5) Again, same as number 4.

6) I pointed out the correction to your random number generating. But to get them to equal up to ten is just the matter of a little subtraction and using the result in the next random number. So lets say we have a total of 10 and our first number is 3 for range. We subtract 3 from 10 to get 7. Your next random stat will be based on 7. If 7 > 6, we still use rand() % 6 + 1, otherwise we are going to use rand() % result + 1. Where result is the points we have left. Another more sloppy way is to randomize all the stats each time, add up their numbers and if it is less than (or equal to) 10, keep them. Otherwise re-randomize all stats again in a loop until it is less than or equal to 10.

7) '.' for the laser - Loop through the array placing a period in each spot between you and target. Again since you are doing an ascii game for this the animation is not exactly going to be there. The best you can do is put the period in there for X number of spots (based on range) or until it hits target tank. Then refresh the print to show the trail and hit, reset the squares back to empty and refresh the screen again after a period of time.

8) Ascii 177 is into extended ascii so it may or may not show the same chars... depends on the character set. Other than that.... not sure what else you want. "Nice?"

As for walls, they are just another character on the board with their own properties. Maybe it can take 2 hits before being removed from game. In this case for each wall hit you will need to keep a counter for number of times it has been hit.

Hope these help and next time, might want to try a few less questions at once. Not many people here are going to want to answer all of these at one time.

Enjoy!

"At DIC we be game developing code ninjas... we developed the game hide the weenie. It was a huge hit with the adults." :snap:
Was This Post Helpful? 1
  • +
  • -

#4 codecsmash49   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 09-December 08

Re: Programming a 2-D tank game

Posted 10 December 2008 - 12:35 PM

Ya sorry about that. Thank you for the help tho =D

im still having a problem understanding how to assemble my functions and displaying my grid.

here is the new code:
#include <cstdlib>
#include <iostream>
#include "color.h"
#include <conio.h>

using namespace std;
using namespace Petter;

void title();
void credits();
void intro();
void instructions();
int menu(char arrayGrid[][21]);
void displayGrid(char arrayGrid[][21]);
void begin();
void yourTank();
void yourTankRotate();
void compTank();
void compTankRotate();
void stats();
void compStats();
void yourShootL();
void compShootL();
void shootG();
void shieldLeft();
void move();
void hitLaser();
void hitGrenade();
void direction();
void tankFace();



int main(int argc, char *argv[])
{
	//yourTank;
	//compTank;
	
	//yourTankY =2;
	//yourTankX = 2;
	//compTankY = 19;
	//compTankX = 19; 
	//int compShield; // HP
	//int compMove; // actions per a turn
	//int compWeapon; // +damage
	//int compRange; // how far weapon can shoot
	//int compTotal = compRange + compShield + compWeapon + compMove; 
	//int yourShield;// HP
	//int yourMove; // actions per a round
	//int yourWeapon; // +damage
	//int yourRange; // how far your weapon shoots
	//int yourTotal = yourRange + yourShield + yourWeapon + yourMove; // total stats cannot acceed 10
	//int yourRemaining = 10 - yourRange + yourShield + yourWeapon + yourMove;
	//int yourFace;
	//char action;
	char arrayGrid[][21]={
						{201,205,209,205,209,205,209,205,209,205,209,205,209,205,209,205,209,205,209,205,187},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{200,205,207,205,207,205,207,205,207,205,207,205,207,205,207,205,207,205,207,205,188},
						};
	
	
	title();
	credits();
	cout << endl;
	cout << endl;
	menu(arrayGrid);
	
	
	system("PAUSE");
	return EXIT_SUCCESS;
}
void title()
{
	cout << endl;
	cout << endl;
	cout << endl;
	cout << endl;
	cout <<  "\t\t\t" <<RED << " WELCOME TO M.I.M.E WARS!!!!!!!" << NORMAL << endl;   
}
void credits()
{
	cout << endl;
	cout << endl;
	cout << endl;
	cout << endl;
	cout << "\t\t\tCreated by: Michael Meyer" << endl;
	cout << "\t\t\tWith Help from: Mountain Dew" << endl;
}
int menu(char arrayGrid[][21])
{
	char choice;
	
	do
	{
	
	
	cout << "								 :[B]ATTLE!:   " <<endl;
	cout << "											   " <<endl;
	cout << "								:[S]STORY LINE:" <<endl;
	cout << "											   " <<endl;
	cout << "							   :[I]NSTRUCTIONS:" <<endl;
	cout << "											   " <<endl;
	cout << "							  :[E]XIT(chicken!?):	" <<endl;
	
	choice =getch(); // calls for input
	switch(choice)
	{
				   case 'b':
						{
							begin();
						}
				   break;
				   case 's':
						{
							intro();
						}
				   break;
				   case 'i':
						{
							instructions();
						}
				   break;
				   case 'e':
				   break;
	} 
	}
	while(choice != 'e');  

	return 0;
}
	 
void instructions()
{
	cout << "\t\t\t\t Instructions" << endl << endl << endl;

			cout << "Player controls:" << endl;
			cout << "W key: Progress the M.I.M.E forward, towards the way they are facing." << endl;
			cout << "A key: Rotates the M.I.M.E left." << endl;
			cout << "D key: Rotatest the M.I.M.E Counter right." << endl;
			cout << "E key: Chooses to fire the M.I.M.E's Lazer." << endl;
			cout << "Q key: Chooses to lob a M.I.M.E's Grenade." << endl;	 
	}
void into()
{
	cout << "\t\t\t\t Introduction" << endl << endl << endl;
			cout << "\tDuring the third World War a new breed of warefare was brought to life. M.I.M.E's were born to be a slave of men.";
		   Sleep(4000);
			cout << " M.I.M.E, Mechines In Main Engagement, were a far more advance design of A.I.(Artificial Intelegence).";
			Sleep(4000);
			cout << "There mechines   were designed to lower human casualties, but instead turned and increase the   death toll.";
			cout << endl;
			Sleep(4000);
			cout << "\tWhen the M.I.M.Es turned, it was nothing more then a silent killing of  those around them.";
			Sleep(4000);
			cout << "Man was once again forced back into traditional warfare of	opertaing there own mechines and demise.";
			cout << endl;
			cout << endl;
			cout << endl;
			Sleep(4000);
			cout << "Who will win!?!?.........";
			cout << endl;
			cout << endl;	
}

void begin(char arrayGrid[][21])
{
	stats();
	//displayGrid(arrayGrid[][21]);
	cout << endl;
	shieldLeft();
	cout << endl;
	direction();
	cout << endl;
	cout << endl;
	move();
}
void displayGrid(char arrayGrid[][21]) // display the grid + robots
{
   /* cout << (char){201,205,209,205,209,205,209,205,209,205,209,205,209,205,209,205,209,205,209,205,187},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{199,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,197,196,182},
						{186,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',179,' ',186},
						{200,205,207,205,207,205,207,205,207,205,207,205,207,205,207,205,207,205,207,205,188},
						}; */
}
void yourTank() // your tank Green
{
	 
}

/*void yourTankRotate() // Shows you tank rotating N, S, W, E
{
	if(action == a)
		{
			yourFace - 1;
			
		}
	else if(action == d)
		{
			yourFace - 1;
		}
	//yourTankFace(); 
}
*/
void compTank() // computer tank Red
{
	
}

void compTankRotate() // Shows computer tank rotating N, S, W, E
{
	
}

void stats()
{
   int yourShield; // HP
   int yourMove; // actions per a round
   int yourWeapon; // +damage
   int yourRange; // how far your weapon shoots
   int yourTotal = yourRange + yourShield + yourWeapon + yourMove; // total stats cannot acceed 10
   int yourRemaining = 10 - yourRange + yourShield + yourWeapon + yourMove;
	
	 cout << "Choose the stats you would like for your M.I.M.E., with a max total of 10." << endl;
	 
	 while(yourTotal != 10)
	 {
		 cout << "Choose 1-6 for your range of your weapon(" << yourRemaining << " points left: ";
		 cin >> yourRange;
		 while((cin == 0) || (yourRange < 0))
					{
						cin.clear();
						cin.ignore(10000, '\n');
						cout << "Please enter the amount of range: ";
						cin >> yourRange;
					}
		 
		 cout << "Choose 1-6 for your shields(" << yourRemaining << " points left: ";
		 cin >> yourShield;
		 while((cin == 0) || (yourShield < 0))
					{
						cin.clear();
						cin.ignore(10000, '\n');
						cout << "Please enter amount for the shield: ";
						cin >> yourShield;
					}
		 
		 cout << "Choose 1-6 for your weapon damage(" << yourRemaining << " points left: ";
		 cin >> yourWeapon;
		 while((cin == 0) || (yourWeapon < 0))
					{
						cin.clear();
						cin.ignore(10000, '\n');
						cout << "Please enter the amount for the weapon: ";
						cin >> yourWeapon;
					}
		 
		 cout << "Choose 1-6 for your movement speed(" << yourRemaining << " points left: ";
		 cin >> yourMove;
		 while((cin == 0) || (yourMove < 0))
					{
						cin.clear();
						cin.ignore(10000, '\n');
						cout << "Please enter the amount for movement: ";
						cin >> yourMove;
					}
	}
}

void compStats() // stats MUST be random 1-6 cannot accede 10 total
{
	srand(time(0)); // int random number
	int total = 0;
	int compRange;
	int compShield;
	int compWeapon;
	int compMove;
	
	cout << "The computers stats are as shown: " << endl;
	
	while(total != 10 )
	{
		compRange = (rand() % 6) + 1;
		total = compRange - 10;
		compShield = (rand() % 6) + 1;
		total = (compShield + compRange) - 10;
		compWeapon = (rand() % 6) + 1;
		total =(compShield + compRange + compWeapon) - 10;
		compMove = (rand() % 6) + 1;
		total =(compShield + compRange + compRange + compMove) - 10;
	}
	
	cout << "Range is = " << compRange << endl;
	cout << "Shield is = " << compShield << endl;
	cout << "Weapon is = " << compWeapon << endl;
	cout << "Movement is = " << compMove << endl;
}


void yourShootL() // direct shot 2 damage
{
	int face;
	int yourTankX;
	int yourRange;
	int weaponX;
	int yourTankY;
	int weaponY;
	
	if( 2 == face || 4 == face)
		{
			weaponX = yourTankX + yourRange;
		}
	else if( 1 == face || 3 == face)
		{
			 weaponY = yourTankY + yourRange;
		}



	 hitLaser();
}

void compShootL() // direct shot 2 damage
{
	int face;
	int compTankY;
	int compTankX;
	int weaponX;
	int weaponY;
	int compRange;

	if( 2 == face || 4 == face)
		{
			 weaponX = compTankX + compRange;
		}
	else if ( 1 == face || 3 == face)
		{
			 weaponY = compTankY + compRange;
		}



	 hitLaser();
}

void shootG() // Area of affect dmg 3x3 1 dmg
{
	int face;
	int yourTankX;
	int yourRange;
	int weaponX;
	int yourTankY;
	int weaponY;
	
	if( 2 == face || 4 == face)
		{
			 weaponX = yourTankX + yourRange;
		}
	else if( 1 == face || 3 == face)
		{
			 weaponY = yourTankY + yourRange;
		}
		
	hitGrenade();
}

void shieldLeft() // remaining life points
{
	 int yourShield;
	 int compShield;
	 
	 cout << "Your shield left is: " << yourShield << endl;
	 
	 cout << "The computers shield left is: " << compShield << endl;
}

void direction() // which direction you are facing to determine which way you can move
{				 // face must remain 1-4
	int yourFace;
	
	if( yourFace == 1)
		{
			 cout << "You are facing North";
		}
	else if(yourFace == 2)
		{
			 cout << "You are facing West";
		}
	else if(yourFace == 3)
		{
			cout << "You are facing South";	
		}
	else if(yourFace == 4)
		{
			cout << "You are facing East";	
		}
	else if( yourFace > 4 || yourFace < 1)
	{
		yourFace = 1;
		cout << "You are facing North"; 
	}
}

void yourTankFace()
{
   int yourFace;
	
	if( yourFace == 1)//North
		{
			  cout << GREEN << (char)193;
		}
	else if(yourFace == 2)//South
		{
			cout << GREEN << (char)194;
		}
	else if(yourFace == 3)//West
		{
			cout << GREEN << (char)180;	
		}
	else if(yourFace == 4)//East
		{
			cout << GREEN << (char)195;
		}
}

void compTankFace()
{
	int face;
	
	if( face = 1)//North
		{
			cout << RED << (char)193;
		}
	else if(face == 2)//South
		{
			cout << RED << (char)194;
		}
	else if(face == 3)//East
		{
			cout << RED << (char)180;
		}
	else if(face == 4)//West
		{
			cout << RED << (char)195;	
		}	
}

void move() // Move you forward
{
	 int x;
	 int y;
	 int action;
	 int moveX;
	 int moveY;
	 int yourTankX;
	 int yourTankY;
	 int face;
	 
	 
	 cout << "Choose an action: 1 - Move Forward, 2 - Rotate Left, 3 - Rotate right, 4 - Fire Laser, 5 - Lob Grenade." << endl;
	 cin >> action;
	 
	 switch(action)
	 {
		case 1: // move forward adding 1 to X or Y axis based on direction facing 
			while( x != 0 && x != 21 && y != 0 && y != 21)
			{
				if( 2== face || 4 == face)
					{
						 moveX =yourTankX + 1;	
					}   
				else if ( 1 == face || 3 == face)
					{
						 moveY = yourTankY + 1;
					} 
			}
			break;
			
		case 2: // rotates you to the left subtracting 1 from face
			 // yourTankRotate();
			  break;
					  
		case 3: // rotates you to the right adding 1 to face
			  //yourTankRotate();
			  break;
						  
		case 4: // fires THE LAZER! 
			  //shootL();
			  break;
			
		case 5:  // Lobs the grenade
			 //shootG();
			 break;
		
	 }
}

void hitLaser() // determines if the laser hit
{
	int weaponY;
	int yourTankY;
	int compTankY;
	int weaponX;
	int yourTankX;
	int compTankX;
	
	if (weaponY == yourTankY || weaponY == compTankY)
		{

			shieldLeft();
		}
	else if (weaponX == yourTankX || weaponX == compTankX)
			{
	
				shieldLeft();
			}
		 
}

void hitGrenade() // determines if the Grenade hits
{
	int weaponY;
	int yourTankY;
	int compTankY;
	int weaponX;
	int yourTankX;
	int compTankX;
	
	if (weaponY + 3 == compTankY)
		{

			shieldLeft();
		}
	else if (weaponX + 3 == compTankX)
			{
	
				shieldLeft();
			}
}


This post has been edited by codecsmash49: 11 December 2008 - 07:01 AM

Was This Post Helpful? 0
  • +
  • -

#5 codecsmash49   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 09-December 08

Re: Programming a 2-D tank game

Posted 12 December 2008 - 12:28 PM

Can anyone help me?

I dont really know how to put this all together.

Also i still have not figured out how to display the grid and tanks, so it refreshes them at the end of each sequence
Was This Post Helpful? 0
  • +
  • -

#6 morrock   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-December 08

Re: Programming a 2-D tank game

Posted 13 December 2008 - 11:33 AM

I figured out why the random generation of stats wasn't working (This is Neil). The code was adding to the result too early. Try this, it increments after subtracting from the remainder value instead of incrementing it after randomly generating the number.
 srand(time(0)); // int random number
	int compShield;
	int compWeapon;
	int compMove;
	int compRange;

	int remain = 6;
	compShield = rand() % remain;
	remain -= compShield++;

	compWeapon = rand() % remain;
	remain -= compWeapon++;

	compMove = rand() % remain;
	remain -= compMove++;

	compRange = ++remain;

	cout << "compShield: ";
	cout << compShield;
	cout << endl;
	cout << "compWeapon: ";
	cout << compWeapon;
	cout << endl;
	cout << "compMove: ";
	cout << compMove;
	cout << endl;
	cout << "compRange: ";
	cout << compRange;
	cout << endl;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1