| | |
Moveable character?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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,844
Reputation:
Solved Threads: 503
•
•
•
•
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
Views: 389 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






