Advertisement

Map quest game C++/ Help me please

Started by June 12, 2020 02:17 PM
4 comments, last by Switchboy 4 years, 5 months ago

Please I need your help.
1.How to connect character object with "@" from the map and enemy object with "%"?

2.How to make different enemyes with diferent parameters like file or list or…and to connect with simbol “%”?

class Map.h

#pragma once
#include <windows.h>
#include <stdlib.h>
#include "Character.h"

//#include "Player.h"

class theMap
{

private:
   
    bool game_running = true;
    Character* ch;
    int x=1;
    int y=1;

    char map[32][84] = {


    "################################################################################",
    "#@                                                   ____                      #",
    "#        { }* { }  { }                    ___       <____>                     #",
    "#     { }//{ }//{ }// { }    ++++++++++  <___>                                 #",
    "#    *//{ }//{ }//*{ }//{ } % /   /   |+++++++++++    | |                   +++#",
    "#  { }- F O R E S T            / /    /       /  |++++++++          ++++++++|  #",
    "#  // *{ } *{ } * { }* { }//    /     D O C K            |++++++++++|          #",
    "#   { }//{ }// { }//{ }//            %          /      /                       #",
    "#   // * // * *// * //     %___/___________~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#",
    "#     { }   { }   { }     % |M           T|                                    #",
    "#     //    //    //     %  |  A      E   |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#",
    "#                       %        R  K     |           %                        #",
    "#               %      %    |_____________|         %            %             #",
    "#                %                            /// ////// ////       %          #",
    "#                 %                           |-| |----| |--|          %       #",
    "#                  %                          | | |    | |  |       %          #",
    "#  ^^  ^^  ^^  ^^   %                         | | |    | |  |               %  #",
    "# /=/ /=/ /=/ /=/    %                    % ^^^^^^^^^^^^^^^^^^^^               #",
    "#   V I L A G E       %                  %  |------------------|               #",
    "#     ^^  ^^  ^^  ^^   %                %   |                  |               #",
    "#    /=/ /=/ /=/ /=/    %              %         P A L A C E   |               #",
    "#                                         |======================     %        #",
    "#                                             %                                #",
    "#                   ______________________|__________     %                    #",
    "#              %   [ ______________________________ ]__   __                   #",
    "#                      | |                  | |             <=============>    #",
    "#         %           %                                ______             |    #",
    "#               [ (      %    A R E N A              ) ]    | A R M O R Y |    #",
    "#                [ (   | |                  | |     ) ]     |             |    #",
    "#                 [ (______________________________) ]      <=============>    #",
    "#                  [________________________________]                          #",
    "################################################################################"

  };


public:
    theMap() {}
   
    theMap(int x, int y, Character * ch) {
        this->x = x;
        this->y = y;
        this->ch = ch;
    }
    ~theMap() {}
    
    void moveChar();
    void fullscreen();
          
};

class Map.cpp

#include "theMap.h"
#include "Character.h"
#include <iostream>
#include <windows.h>
#include <stdlib.h>
//#include <exeption>

using std::endl;		using std::cout;
using std::string;





void theMap::moveChar() {

    while (game_running = true) {

        system("cls");

        for (int i = 0; i < 32; i++) {
            for (int j = 0;j < 84;j++) {
                
                    cout << map[i] << endl;
                    if (j == ch->getX() && i == ch->getY()){
                       map[i][j] = '@';
                            cout << map[j] << endl;
                        
                }
            }
        }
        cout << "\n\n\nPress Escape to exit the game.\n\n\n";
        system("pause");


        if (GetAsyncKeyState(VK_DOWN)) {
            int y2 = y + 1;
          
            if (map[y2][x] == ' ') {
                map[y][x] = ' ';
                y++;
                map[y][x] = '@';
                if (map[5][18]) {
                    cout << "Wewkom to the FOREST";
                }
            }
        }
        if (GetAsyncKeyState(VK_UP)) {
            int y1 = y - 1;
            if (map[y1][x] == ' ') {
                map[y][x] = ' ';
                y--;
                map[y][x] = '@';
            }
        }
        if (GetAsyncKeyState(VK_LEFT)) {
            int x2 = x - 1;
            if (map[y][x2] == ' ') {
                map[y][x] = ' ';
                x--;
                map[y][x] = '@';
                if (map[y][x] = '@' == map[5][25]) {
                    system("pause");
                    cout << "Wellcome to the FOREST";
                }
            }
        }
        if (GetAsyncKeyState(VK_RIGHT)) {
            int x1 = x + 1;
            if (map[y][x1] == ' ') {
                map[y][x] = ' ';
                x++;
                map[y][x] = '@';
            }
        }

        if (GetAsyncKeyState(VK_ESCAPE)) {
            cout << "\n\n\nThank you for the game!\n\n\n";
            exit(3);
        }
    }
}
void theMap::fullscreen()
{
    keybd_event(VK_MENU, 0x38, 0, 0);
    keybd_event(VK_RETURN, 0x1c, 0, 0);
    keybd_event(VK_RETURN, 0x1c, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_MENU, 0x38, KEYEVENTF_KEYUP, 0);


}




class Character.h

#pragma once
#include <string>
#include <iostream>

using std::string;


class Character
{
private:
	string m_name;
	int m_level, m_hp, m_strenght, m_intelligence,m_money;
	int x =1;
	int	y =1;

public:

	Character();
	Character(string name, int level, int hp, int strenght, int intelligence, int money, int x1, int y1);
	void SetCharacter(int x, int y);
	const int getX(void)const;
	const int getY(void)const;

	/*void attack();
	void defend();
	void levelUp();*/


};

class Character.cpp

#include "Character.h"
#include<iostream>
#include <windows.h>
#include <stdlib.h>

Character::Character() {

	
		m_name = "Witcher";
		m_level = 1;
		m_hp = 100;
		m_strenght = 10;
		m_intelligence = 10;
		m_money = 140;
		x = 1;
		y = 1;
	}
Character::Character(string name, int level, int hp, int strenght, int intelligence, int money, int x, int y)
{
	m_name = name;
	m_level = level;
	m_hp = hp;
	m_strenght = strenght;
	m_intelligence = intelligence;
	m_money = money;
	this-> x=x;
	this-> y=x;
}

void Character:: SetCharacter(int x, int y) {
	this->x = x;
	this->y = x;
}
const int Character::getX(void)const {
	return x;
}

const int Character::getY(void)const {
	return y;
}

class Enemy.h

#pragma once
#include <iostream>


class Enemy
{
private:
	 



};

class Enemy.cpp

#include "Enemy.h"

Main


#include "theMap.h"
#include <iostream>

int main()
{
	theMap m;
	m.fullscreen();
	m.moveChar();
	
return 0;

}

estervina said:
1.How to connect character object with "@" from the map and enemy object with "%"?

Basically you tell the object where its symbol is in the map. One way to do that is by searching for it in the map.

estervina said:
2.How to make different enemyes with diferent parameters like file or list or…and to connect with simbol “%”?

You make a different object for each enemy, and store them in a std::vector, although I'd suggest you start withe 1 enemy first.

Advertisement

@Alberth

Hello!
How to search?
There is a n error:"Unhandled exeption thrown: read access violation. this was nullptr."

estervina said:
How to search?

Look at each position of the map until you found it, would be my guess. How else would you find something where you don't know where it is?

estervina said:
There is a n error:"Unhandled exeption thrown: read access violation. this was nullptr."

It's not an error, it's a program crash. C++ assumes you know what you're doing, it has no problems with you making mistakes, It simply does something random (it's called ‘undefined behavior’), in this case the program crashes.

To find out where the problem is, try using a debugger. Likely you should start reading a C++ debugger manual or tutorial first.

BTW: This is why this site generally recommends not starting with C++, it's a harsh unforgiving environment. Other languages tend to give you more useful feedback when you make an error. I don't know why you picked C++, but maybe you should consider using something else???

Albert

I like to put my maps in 2D arrays where the first index is your X coordinate and the second index is your Y coordinate. If you are new i would suggest using one array for your terrain, one for items and one for enimies. The easiest way to do this is to have just arrays of intergers and have a list of objects for items and actors.

Some simple pseudo code in C++:

#define MAPWIDTH 10
#define MAPHEIGHT 10
#define MAXACTORS 99

int mapTerrainArray[MAPWIDTH][MAPHEIGHT] =
    {
        {1,1,1,1,1,1,1,1,1,1},
        {1,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,1},
        {1,1,1,1,1,1,1,1,1,1},
    };

int mapActorsArray[MAPWIDTH][MAPHEIGHT] =
    {
        {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
        {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
        {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
        {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
        {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
        {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
        {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
        {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
        {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
        {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
    };


class actors{

public:
    actors(){
        //standard constructor only used when initializing array (not needed if using std::vector)
        this->actorIsActuallySet = false;
    }
    actors(int positionX, int positionY){

    }
    void drawActor(){
        //Stuff to draw actor goes here
    }
    void updateActor(){
        //stuff to update actor goes here
    }
private:
    int positionX;
    int positionY;
    bool actorIsActuallySet;
};

actors listOfActors[MAXACTORS]; //You should probably use an std::vector here but for the sake of simplicity...

void drawMap(){
    for(int i = 0; i < MAPWIDTH; i++){
        for(int j = 0; j < MAPHEIGHT; j++){
            switch(mapTerrainArray[i][j]){
                case 0:
                //drawFloorTile at postion x= i y = j
                break;
                case 1:
                //drawWallTile at postion x= i y = j
                break;
            }
        }
    }
}

void drawActors(){
    for(int i = 0; i < MAPWIDTH; i++){
        for(int j = 0; j < MAPHEIGHT; j++){
            if(mapActorsArray[i][j] != -1){
                //Then there is an actor here! (here is  at postion x= i y = j)
                listOfActors[mapActorsArray[i][j]].drawActor();
            }
        }
    }
}

void drawGame(){
    drawMap();
    drawActors();
}

int main()
{
    int x = 0;
    while( x != 1){
    //user input

    //update game

    //draw game
    drawGame();
    }
}

This creates a map with walls around it with no actors until you spawn one.

None

This topic is closed to new replies.

Advertisement