| | |
Moveable character?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2008
Posts: 14
Reputation:
Solved Threads: 0
Hey.
I am wanting to make this program so that the user can move the position of his characters. I have made a attempt but i am confused about some bits.
Some of the problems include: At the start of the Do loop the characters position will be deleted in my code because of the system clear. Another worry is that when the user wants to move up or down the character will be brought to the start of the line and that may not be directly under/above the last position. If you could help i would be very graceful.
I am wanting to make this program so that the user can move the position of his characters. I have made a attempt but i am confused about some bits.
C++ Syntax (Toggle Plain Text)
// Code Shark #include <iostream> using namespace std; int main() { char character = 'X', movement; do { system("Cls"); cout << "Do you want your character to move [L]eft, [R]ight, [U]p or"; cout << " [D]own or [E]xit: "; cin >> movement; switch (movement) { case 'L': cout << "\b" << character; break; case 'R': cout << " " << character; break; case 'U': //cout << ???; break; case 'D': cout << endl << character; /* the thing is if i just go endl here the character will go straight to the start of the new line and most likelys the character will be spaces away from it. */ break; case 'E': break; } } while(movement != 'E'); cin.get(); return 0; }
Some of the problems include: At the start of the Do loop the characters position will be deleted in my code because of the system clear. Another worry is that when the user wants to move up or down the character will be brought to the start of the line and that may not be directly under/above the last position. If you could help i would be very graceful.
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
•
•
•
•
Hey.
I am wanting to make this program so that the user can move the position of his characters. I have made a attempt but i am confused about some bits.
C++ Syntax (Toggle Plain Text)
// Code Shark #include <iostream> using namespace std; int main() { char character = 'X', movement; do { system("Cls"); cout << "Do you want your character to move [L]eft, [R]ight, [U]p or"; cout << " [D]own or [E]xit: "; cin >> movement; switch (movement) { case 'L': cout << "\b" << character; break; case 'R': cout << " " << character; break; case 'U': //cout << ???; break; case 'D': cout << endl << character; /* the thing is if i just go endl here the character will go straight to the start of the new line and most likelys the character will be spaces away from it. */ break; case 'E': break; } } while(movement != 'E'); cin.get(); return 0; }
Some of the problems include: At the start of the Do loop the characters position will be deleted in my code because of the system clear. Another worry is that when the user wants to move up or down the character will be brought to the start of the line and that may not be directly under/above the last position. If you could help i would be very graceful.
I think the way to go is to keep track of the (x,y) coordinates of the character. When you move the character, decrement or increment the x or y coordinate of the character, then display it. The program below is a modification of your program where the character is represented by 'O' and it can move around in a 5 x 5 character grid. Empty spaces are denoted by 'X'. (xPos,yPos) represent the character's current location and the 5 x 5 grid is redrawn each trip through the do-while loop.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main () { char grid[5][5]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { grid[i][j] = 'X'; } } char movement; int xPos = 2; int yPos = 2; do { grid[xPos][yPos] = 'O'; system("Cls"); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { cout << grid[j][i]; } cout << endl; } cout << "Do you want your character to move [L]eft, [R]ight, [U]p or"; cout << " [D]own or [E]xit: "; cin >> movement; grid[xPos][yPos] = 'X'; switch (movement) { case 'L': if (xPos > 0) { xPos--; } break; case 'R': if (xPos < 4) { xPos++; } case 'U': if (yPos > 0) { yPos--; } break; case 'D': if (yPos < 4) { yPos++; } break; case 'E': break; } } while(movement != 'E'); cin.get(); return 0; }
![]() |
Similar Threads
- Windows GUI - problem with dialog box (C++)
- Pacman for Python (Python)
Other Threads in the C++ Forum
- Previous Thread: Although it's not really important...
- Next Thread: Reading user input file into an array
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






