Thread: X and Y

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    19

    X and Y

    Aloha all. I'm quite new to C++ programming.
    I've been asked to make a demo app - a dungeon RPG with characters for graphics.

    I decided to draw the map in notepad and use fstream in C++ to read from the file and draw the map. I will then draw the player into the world (as the letter 'x' to start with) and set the controls to the arrow keys to move the character around. The collision detection will work on detecting what type of character occupies the space you are trying to move to, | and _ for doors, # for walls and a space for open floor etc.

    I've run into the problem of placing the player in the map:
    dungeonseeker.cpp
    Code:
    #include <iostream>
    #include <string>  //This allows creation and manipulation of strings.
    #include <fstream> //This allows opening and reading of files.
    #include <process> //This allows the use of system("cls");
    
    using namespace std;
    
    
    
    //This is the function that will open the map file
    //and draw it into the console
    void drawMap()
    {	
    	//Open the map file for reading
    	ifstream mapfile("map.ds");
    
    	//Store each line of the file in a string to be displayed
    	string line;
    	
    	if (mapfile.is_open())
    	{
    		//Read from the file until the end is reached
    		while (! mapfile.eof())
    		{
    			//Read each line and output it to the screen.
    			//This will loop through each line until there
    			//are no lines left to read.
    			getline(mapfile, line);
    			cout << line << endl;
    		}
    		
    		mapfile.close();
    	}
    }
    
    
    //Set the starting location of the player
    void initPlayer()
    {
    	//0,0 is at the top left of the map file.
    	//Positive X values move horizontally right,
    	//Positive Y values move  vertically down.
    	int x = 15;
    	int y = 5;
    
    	
    }
    
    
    int main()
    {
    	//Location variables (link with initPlayer and controlPlayer)?
    	//int x, y;
    
    	//Clear the screen ready for the game
    	system("cls");
    
    	//Draw the map
    	drawMap();
    	
    	//Position the player
    	initPlayer();
    
    	return 0;
    }
    I can't seem to attach the map file


    As you can see, I've initialised two integers (x and y) to hold the co-ords of the start position but I cannot think of how to place a character at a position on the screen relating to these values. Any suggestions?
    Last edited by rickyoswaldiow; 10-18-2006 at 07:36 AM. Reason: attachments

  2. #2
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    place everything from the map file into a 2 D arrays and whenever you want to put the player relating to the screen, say (15, 5)

    do something like
    Code:
    mapStuff[15][5] = 'X' // suppose we use 'X' to represent the player
    a bit inefficient i know (suppose if the map is humoungous)

    but that's all i can think about now

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    19
    I thought of using an array myself but that's not really my problem.

    I can declare some values of where I want the 'x' to be drawn, but that's only relative to our perception of the map. How do I tell the compiler to understand the co-ordinates.
    Once the map has been drawn the program waits for input, the next line of output would be put directly after the last character of the map, how can I tell it to jump back to the begining of the map (the top left corner) and define that as 0,0?

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102
    This question is answered in the faq.

    FAQ > How do I... (Level 2) > gotoxy() in a Windows Console

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    19
    Many thanks

Popular pages Recent additions subscribe to a feed