Thread: movement question

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    movement question

    I'm making the class or whatever that will let the computer tell where the player is at according to an array. How could I have the program test for the characters within the array and do things according to what character is in the position the player is in. Here's the code:
    Code:
    #include <iostream>
    using namespace std;
    
    #define NUM_ROWS 3
    
    #define Church_ID 'c'
    #define Item_ID   'i'
    #define Weapon_ID 'w'
    #define Armor_ID  'a'
    #define Road_ID   'r'
    #define Wall_ID   '*'
    
    /*********************/
    //c = church
    //i = item shop
    //* = not accessible
    //w = weapon shop
    //a = armor shop
    //r = road
    /*********************/
    
    char *FunkyTown[NUM_ROWS] = {"cri",
    							 "wra",
    						 	 "*r*"};
    
    class move
    {
    private:
    	int xpos;
    	int ypos;
    	int ymax;
    	int ymin;
    	int xmax;
    	int xmin;
    	int previousYpos;
    	int previousXpos;
    public:
    	move()
    	{
    		xpos = 1;
    		ypos = 3;
    		ymin = 1;
    		ymax = NUM_ROWS;
    		xmin = 0;
    		xmax = 2;
    	}
    	void position(int value)
    	{
    		previousYpos = ypos;
    		previousXpos = xpos;
    		if(value == 1)
    		{
    			ypos--;
    			if(ypos <= 0)
    			{ypos = ymin;}
    			else if(ypos > 3)
    			{ypos = ymax;}
    		}
    		else if(value == 2)
    		{
    			ypos++;
    			if(ypos <= 0)
    			{ypos = ymin;}
    			else if(ypos > 3)
    			{ypos = ymax;}
    		}
    		else if(value == 3)
    		{
    			xpos--;
    			if(xpos <= 0)
    			{xpos = xmin;}
    			else if(xpos > 2)
    			{xpos = xmax;}
    		}
    		else
    		{
    			xpos++;
    			if(xpos <= 0)
    			{xpos = xmin;}
    			else if(xpos > 2)
    			{xpos = xmax;}
    		}
    		if(xpos == 0 && ypos == 3 || xpos == 2 && ypos == 3) //this is the easy way I test
    		{													 //to see if the player hits a wall
    			xpos = previousXpos;							 //without using the array at all.
    			ypos = previousYpos;
    			cout << "You can't go that way, its a wall." << endl;
    		}
    	}
    	void displayPos()
    	{
    		cout << "Current Position: " << (xpos+1) << "," << ypos << endl;
    	}
    };
    
    void main()
    {
    	move character;
    	int choice = 0;
    	cout << "Movement Test v1.0" << endl;
    	while(choice!=5)
    	{
    		cout << "1 Move North" << endl;
    		cout << "2 Move South" << endl;
    		cout << "3 Move West" << endl;
    		cout << "4 Move East" << endl;
    		cout << "5 Quit" << endl;
    		cout << "Choose: ";
    		cin >> choice;
    		system("cls");
    		switch(choice)
    		{
    		case 1: character.position(choice);
    			break;
    		case 2: character.position(choice);
    			break;
    		case 3: character.position(choice);
    			break;
    		case 4: character.position(choice);
    			break;
    		case 5: break;
    		}
    		character.displayPos();
    	}
    }
    I'm probably doing this the wrong way totally, any help would be much appreciated. Thanks in advance.
    -Psycho

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    This is the basic idea: (and it is better to check if he can go in that direction before you actually move the player)
    Code:
    #define TILE_WALKABLE 0
    #define TILE_OBSTACLE 1
    
    #define LEFT 0
    #define RIGHT 1
    #define UP 2
    #define DOWN 3
    
    //Function that checks for obstacles
    BOOL CheckForObstacle(int X, int Y)
    {
       if(WorldArray[X][Y] == TILE_OBSTACLE) return TRUE;
       else return FALSE;
    }
    
    //Your move-function. The argument is which direction he
    //is trying to walk at
    void Position(int Value)
    {
       switch(Value)
       {
          case LEFT:
              if(!CheckForObstacle(PlayerXpos - 1, PlayerYpos)) PlayerXpos--;
          break;
          case RIGHT:
              if(!CheckForObstacle(PlayerXpos + 1, PlayerYpos)) PlayerXpos++;
          break;
          case UP:
              if(!CheckForObstacle(PlayerXpos, PlayerYpos - 1)) PlayerYpos--;
          break;
          case DOWN:
              if(!CheckForObstacle(PlayerXpos, PlayerYpos + 1)) PlayerYpos++;
          break;
       }
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    what's wrong with your code, what errors do you get
    1 more thing when u have a constructor dont u have to set it in your main somehow i dont remember it right now but i'll go read about it now please post your errors.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    I got it to work finally. Thanks Magos.

    I wasn't getting any errors, the program just wasn't working the way I wanted it to. The constructor is called whenever you create a variable using the class, ex. "move character". The point of the contructor usually is to initialize all the variables in the class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM