Keeping track of the player in a 2d numbered grid.

This is a discussion on Keeping track of the player in a 2d numbered grid. within the C++ Programming forums, part of the General Programming Boards category; Every time my player moves I want to use my code to relay his xy position via the point struct ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Keeping track of the player in a 2d numbered grid.

    Every time my player moves I want to use my code to relay his xy position via the point struct to a cout for visual display of his coordinates. And I want to do it in one line of flexible code, am I on the right track?

    Code:
    #include <conio.h>
    #include <iostream>
    #include "Point.h"
    template <typename T>
    T Bound(T val, T min, T max)
    {
        if(val < min) return min;
        if(val > max) return max;
        return val;
    }
    
    class Player
    {
    private:
    
    	int strength;
    	int dexterity;
    	int intelligence;
    	char direction;
    	int x_location;
    	int y_location;
    
    public:
    	Player()
    	{
    		x_location = 0;
    		y_location = 0;
    	}
    	Point move()
    	{
    		direction = _getch();
    		if (direction == 'w') y_location++;
    		else if(direction == 'a') x_location--;
    		else if(direction == 's') y_location--;
    		else if(direction == 'd') x_location++;
    		x_location = Bound(x_location, -10, 10);
    		y_location = Bound(y_location, -10, 10);
    		Point location;
    		location.x = x_location;
    		location.y = y_location;
    		return location;
    	}
    };
    Code:
    #include "stdafx.h"
    #include "World.h"
    #include "Player.h"
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	World Adventursaur;
    	Player player;
    	int enemy = 15;
    	do
    	{
    		enemy--;
    		
    	}
    	while(enemy > 0);
    
    	return 0;
    }
    Code:
    #include "Player.h"
    
    
    class World
    {
    public:
    
    	void get_coordinates(Player player)
    	{
    		
    		player.move();
    	}
    
    };
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Eureka!

    I fixed it, I definitely think it is better in some way now.

    It actually works for one.

    Code:
    #include "stdafx.h"
    #include "World.h"
    #include "Player.h"
    #include <iostream>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	Game_Area area(10, -10);
    	Player player;
    
    	int enemy = 15;
    	do
    	{
    		player.move(area.x_boundary, area.y_boundary);
    		std::cout << player.local.x_location << ", " << player.local.y_location << std::endl;
    		enemy--;
    		
    	}
    	while(enemy > 0);
    
    	return 0;
    }
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d grid
    By lord in forum C++ Programming
    Replies: 4
    Last Post: 02-03-2009, 08:06 PM
  2. Replies: 8
    Last Post: 11-03-2008, 09:48 PM
  3. Find a word in a 2d grid
    By The_Kingpin in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2005, 05:38 PM
  4. Keeping track of static external structure
    By pwilfred in forum C Programming
    Replies: 6
    Last Post: 03-13-2003, 06:23 PM
  5. keeping track of number of pieces left
    By axon in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2003, 05:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21