header1.h

#include <iostream>
#include <string>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <Windows.h>
#include <conio.h>

using namespace std;

int gotoxy_object(int x, int y) // your object
{  
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD point;
	point.X = x-1;
	point.Y = y-1;     
	SetConsoleCursorPosition(hConsole, point);
	return SetConsoleCursorPosition(hConsole, point);
}

int gotoxy1(int x, int y) // position code for object 1
{  
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD point;
	point.X = x-1;
	point.Y = y-1;     
	SetConsoleCursorPosition(hConsole, point);
	return SetConsoleCursorPosition(hConsole, point);
}

int gotoxy2(int x, int y) // position code for object 2
{  
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD point;
	point.X = x-1;
	point.Y = y-1;     
	SetConsoleCursorPosition(hConsole, point);
	return SetConsoleCursorPosition(hConsole, point);
}

int gotoxy3(int x, int y) // position code for object 3
{  
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD point;
	point.X = x-1;
	point.Y = y-1;     
	SetConsoleCursorPosition(hConsole, point);
	return SetConsoleCursorPosition(hConsole, point);
}

int gotoxy4(int x, int y) // position code for object 4
{  
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD point;
	point.X = x-1;
	point.Y = y-1;     
	SetConsoleCursorPosition(hConsole, point);
	return SetConsoleCursorPosition(hConsole, point);
}

int gotoxy_words(int x, int y) // position code for words and input
{  
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD point;
	point.X = x-1;
	point.Y = y-1;     
	SetConsoleCursorPosition(hConsole, point);
	return SetConsoleCursorPosition(hConsole, point);
}

void Player_1_object()
{
	cout << "$" << endl;
}

void object1()
{
	cout << "A" << endl;
}

void object2()
{
	cout << "B" << endl;
}

void object3()
{
	cout << "C" << endl;
}

void object4()
{
	cout << "D" << endl;
}

header2.h

#include "header1.h"

class Player
{
public:
	char Up, Down, Left, Right;
	char up, down, left, right;

public:
	char get_Up_movement(Player &player1);
	char get_Down_movement(Player &player1);
	char get_Left_movement(Player &player1);
	char get_Right_movement(Player &player1);

	char get_up_movement(Player &player1);
	char get_down_movement(Player &player1);
	char get_left_movement(Player &player1);
	char get_right_movement(Player &player1);
};

char get_Up_movement(Player &player1)
{
	cout << "What is your up movement? ";
	cin >> player1.Up;
	cout << endl;

	return player1.Up;
}

char get_Down_movement(Player &player1)
{
	cout << "What is your down movement? ";
	cin >> player1.Down;
	cout << endl;

	return player1.Down;
}

char get_Left_movement(Player &player1)
{
	cout << "What is your left movement? ";
	cin >> player1.Left;
	cout << endl;

	return player1.Left;
}

char get_Right_movement(Player &player1)
{
	cout << "What is your right movement? ";
	cin >> player1.Right;
	cout << endl;

	return player1.Right;
}


char get_up_movement(Player &player1)
{
	cout << "What is your up movement? ";
	cin >> player1.up;
	cout << endl;

	return player1.up;
}

char get_down_movement(Player &player1)
{
	cout << "What is your down movement? ";
	cin >> player1.down;
	cout << endl;

	return player1.down;
}

char get_left_movement(Player &player1)
{
	cout << "What is your left movement? ";
	cin >> player1.left;
	cout << endl;

	return player1.left;
}

char get_right_movement(Player &player1)
{
	cout << "What is your right movement? ";
	cin >> player1.right;
	cout << endl;

	return player1.right;
}

struct player_movement
{
	char Up_, Down_, Left_, Right_;
	char up_, down_, left_, right_;
};

main_program.cpp

#include "header2.h"

int main()
{
	system("mode 80, 40");

	Player player1;
	char movement;
	string decision;

	cout << "Do you want to use letters or numbers as your movement? ";
	cin >> decision;
	cout << endl;

	if (decision == "letters" || decision == "letter")
	{
		get_Up_movement(player1);
		get_Down_movement(player1);
		get_Left_movement(player1);
		get_Right_movement(player1);
	}

	else if (decision == "numbers" || decision == "number")
	{
		get_up_movement(player1);
		get_down_movement(player1);
		get_left_movement(player1);
		get_right_movement(player1);
	}

	system("pause");
	system("cls");

	player_movement player_1;

	player_1.Up_ = player1.Up;
	player_1.Down_ = player1.Down;
	player_1.Left_ = player1.Left;
	player_1.Right_ = player1.Right;

	player_1.up_ = player1.up;
	player_1.down_ = player1.down;
	player_1.left_ = player1.left;
	player_1.right_ = player1.right;

	int my_obj_x = 25;
	int my_obj_y = 25;

	int obj1_x = 25;
	int obj1_y = 1;

	int obj2_x = 62;
	int obj2_y = 25;

	int obj3_x = 13;
	int obj3_y = 39;

	int obj4_x = 2;
	int obj4_y = 25;

	gotoxy1(obj1_x,obj1_y);
	object1();

	gotoxy2(obj2_x,obj2_y);
	object2();

	gotoxy3(obj3_x,obj3_y);
	object3();

	gotoxy4(obj4_x,obj4_y);
	object4();

	gotoxy_object(my_obj_x,my_obj_y);
	Player_1_object();
	
	for (int i = 0; i < 7; i++)
	{
		int x = 1;
		int y = 1;
		int move_right_x = (my_obj_x + x);
		int move_left_x = (my_obj_x - x);
		int move_up_y = (my_obj_y - y);
		int move_down_y = (my_obj_y + y);

		gotoxy_words(35,39);
		cout << "Enter movement key: ";
		cin >> movement;
		gotoxy_words(35,39);
		cout << "                                           " << endl;
		gotoxy_words(35,37);
		cout << "                                           " << endl;
	
		if (movement == player_1.Up_ || movement == player_1.up_)
		{
			my_obj_y = move_up_y;

			gotoxy_object(my_obj_x,my_obj_y);
			Player_1_object();

			if (my_obj_x == obj1_x && my_obj_y == obj1_y)
			{
				gotoxy_words(35,37);
				cout << " You crashed into object 1." << endl;
				goto fail;
			}

			else if (my_obj_x == obj2_x && my_obj_y == obj2_y)
			{
				gotoxy_words(35,37);
				cout << " You crashed into object 2." << endl;
				goto fail;
			}

			else if (my_obj_x == obj3_x && my_obj_y == obj3_y)
			{
				gotoxy_words(35,37);
				cout << " You crashed into object 3." << endl;
				goto fail;
			}

			else if (my_obj_x == obj4_x && my_obj_y == obj4_y)
			{
				gotoxy_words(35,37);
				cout << " You crashed into object 4." << endl;
				goto fail;
			}

			else
			{
				gotoxy_words(35,37);
				cout << " You Moved North." << endl;
			}
		}

		else if (movement == player_1.Down_ || movement == player_1.down_)
		{
			my_obj_y = move_down_y;

			gotoxy_object(my_obj_x,my_obj_y);
			Player_1_object();

			if (my_obj_x == obj1_x && my_obj_y == obj1_y)
			{
				gotoxy_words(35,37);
				cout << " You crashed into object 1." << endl;
				goto fail;
			}

			else if (my_obj_x == obj2_x && my_obj_y == obj2_y)
			{
				gotoxy_words(35,37);
				cout << " You crashed into object 2." << endl;
				goto fail;
			}

			else if (my_obj_x == obj3_x && my_obj_y == obj3_y)
			{
				gotoxy_words(35,37);
				cout << " You crashed into object 3." << endl;
				goto fail;
			}

			else if (my_obj_x == obj4_x && my_obj_y == obj4_y)
			{
				gotoxy_words(35,37);
				cout << " You crashed into object 4." << endl;
				goto fail;
			}

			else
			{
				gotoxy_words(35,37);
				cout << " You Moved South." << endl;
			}
		}


		else if (movement == player_1.Left_ || movement == player_1.left_)
		{
			my_obj_x = move_left_x;

			gotoxy_object(my_obj_x,my_obj_y);
			Player_1_object();

			if (my_obj_x == obj1_x && my_obj_y == obj1_y)
			{
				gotoxy_words(35,37);
				cout << " You crashed into object 1." << endl;
				goto fail;
			}

			else if (my_obj_x == obj2_x && my_obj_y == obj2_y)
			{
				gotoxy_words(35,37);
				cout << " You crashed into object 2." << endl;
				goto fail;
			}

			else if (my_obj_x == obj3_x && my_obj_y == obj3_y)
			{
				gotoxy_words(35,37);
				cout << " You crashed into object 3." << endl;
				goto fail;
			}

			else if (my_obj_x == obj4_x && my_obj_y == obj4_y)
			{
				gotoxy_words(35,37);
				cout << " You crashed into object 4." << endl;
				goto fail;
			}

			else
			{
				gotoxy_words(35,37);
				cout << " You Moved West." << endl;
			}
		}

		else if (movement == player_1.Right_ || movement == player_1.right_)
		{
			my_obj_x = move_right_x;

			gotoxy_object(my_obj_x,my_obj_y);
			Player_1_object();

			if (my_obj_x == obj1_x && my_obj_y == obj1_y)
			{
				gotoxy_words(35,37);
				cout << " You crashed into object 1." << endl;
				goto fail;
			}

			else if (my_obj_x == obj2_x && my_obj_y == obj2_y)
			{
				gotoxy_words(35,37);
				cout << " You crashed into object 2." << endl;
				goto fail;
			}

			else if (my_obj_x == obj3_x && my_obj_y == obj3_y)
			{
				gotoxy_words(35,37);
				cout << " You crashed into object 3." << endl;
				goto fail;
			}

			else if (my_obj_x == obj4_x && my_obj_y == obj4_y)
			{
				gotoxy_words(35,37);
				cout << " You crashed into object 4." << endl;
				goto fail;
			}

			else
			{
				gotoxy_words(35,37);
				cout << " You Moved East." << endl;
			}
		}
	}
		
		fail: gotoxy_words(35,39);
		system("pause");
		return 0;
}

My code works successfully and all, but I have a question. How do I just make the object move w/o showing the user where it previously was? If you guys or girls don't understand what I mean, please make a blank project and copy paste this code in your visual studio c++. I've always wanted to know but no one has shown me. Anyway, Thank You Again Forum and Thank You Again Forum Users And Members. Good Night Everyone. =)

Recommended Answers

All 5 Replies

I have not read all of your code, but from a quick scan I would suggest printing a space where the character used to be.

so i could use an if statement couldn't I? like if user input = d then i make a blank space behind the previous movement?

I would suggest a switch statement. If the user enters the 'down' key, then move the character down, draw it and draw a space in the old position. I feel very strongly that fixing movement to WASD is a bad idea. It should be WASD by default, but should be able to be changed (this is coming from a Dvorak keyboard user. WASD is spread out all over my layout!). Here is the general idea behind what I am saying:

putAt(player.position.x,player.position.y,' ');
switch (user_input)
{
    case up_key:
    --(player.position.y);
    break;
    case right_key:
    ++(player.position.x);
    break;
    case down_key:
    ++(player.position.y);
    break;
    case left_key:
    --(player.position.x);
    break;
    default:
    //what do you want to do here?
}
putAt(player.position.x,player.position.y,player.characterRepresentation);

Of course that code is very much general, you would have to modify it to fit your needs.

where did you define putAt??? or is that inside of the library?

putAt was just a place holder for what would have to be some kind of platform dependent code. In your case a viable implementation would be:

void putAt(int x, int y, char chr)
{
    COORD loc;
    loc.X=x;
    loc.Y=y;
    SetConsoleCursorPosition(hConsole,loc);
    cout<<chr;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.