Moveable character?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 14
Reputation: Code Shark is an unknown quantity at this point 
Solved Threads: 0
Code Shark Code Shark is offline Offline
Newbie Poster

Moveable character?

 
0
  #1
Jul 19th, 2008
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.

  1. // Code Shark
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. char character = 'X', movement;
  9.  
  10. do {
  11. system("Cls");
  12.  
  13. cout << "Do you want your character to move [L]eft, [R]ight, [U]p or";
  14. cout << " [D]own or [E]xit: ";
  15. cin >> movement;
  16.  
  17. switch (movement)
  18. {
  19. case 'L':
  20. cout << "\b" << character;
  21. break;
  22.  
  23. case 'R':
  24. cout << " " << character;
  25. break;
  26.  
  27. case 'U':
  28. //cout << ???;
  29. break;
  30.  
  31. case 'D':
  32. cout << endl << character;
  33. /* the thing is if i just go endl here the character will
  34.   go straight to the start of the new line and most likelys
  35.   the character will be spaces away from it. */
  36. break;
  37.  
  38. case 'E':
  39. break;
  40. }
  41. }
  42. while(movement != 'E');
  43.  
  44. cin.get();
  45.  
  46. return 0;
  47. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Moveable character?

 
0
  #2
Jul 19th, 2008
Originally Posted by Code Shark View Post
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.

  1. // Code Shark
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. char character = 'X', movement;
  9.  
  10. do {
  11. system("Cls");
  12.  
  13. cout << "Do you want your character to move [L]eft, [R]ight, [U]p or";
  14. cout << " [D]own or [E]xit: ";
  15. cin >> movement;
  16.  
  17. switch (movement)
  18. {
  19. case 'L':
  20. cout << "\b" << character;
  21. break;
  22.  
  23. case 'R':
  24. cout << " " << character;
  25. break;
  26.  
  27. case 'U':
  28. //cout << ???;
  29. break;
  30.  
  31. case 'D':
  32. cout << endl << character;
  33. /* the thing is if i just go endl here the character will
  34.   go straight to the start of the new line and most likelys
  35.   the character will be spaces away from it. */
  36. break;
  37.  
  38. case 'E':
  39. break;
  40. }
  41. }
  42. while(movement != 'E');
  43.  
  44. cin.get();
  45.  
  46. return 0;
  47. }

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.

  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int main ()
  6. {
  7. char grid[5][5];
  8. for (int i = 0; i < 5; i++)
  9. {
  10. for (int j = 0; j < 5; j++)
  11. {
  12. grid[i][j] = 'X';
  13. }
  14. }
  15. char movement;
  16. int xPos = 2;
  17. int yPos = 2;
  18.  
  19. do
  20. {
  21. grid[xPos][yPos] = 'O';
  22. system("Cls");
  23.  
  24. for (int i = 0; i < 5; i++)
  25. {
  26. for (int j = 0; j < 5; j++)
  27. {
  28. cout << grid[j][i];
  29. }
  30.  
  31. cout << endl;
  32. }
  33.  
  34. cout << "Do you want your character to move [L]eft, [R]ight, [U]p or";
  35. cout << " [D]own or [E]xit: ";
  36. cin >> movement;
  37.  
  38. grid[xPos][yPos] = 'X';
  39.  
  40. switch (movement)
  41. {
  42. case 'L':
  43. if (xPos > 0)
  44. {
  45. xPos--;
  46. }
  47. break;
  48.  
  49. case 'R':
  50. if (xPos < 4)
  51. {
  52. xPos++;
  53. }
  54.  
  55. case 'U':
  56. if (yPos > 0)
  57. {
  58. yPos--;
  59. }
  60. break;
  61.  
  62. case 'D':
  63. if (yPos < 4)
  64. {
  65. yPos++;
  66. }
  67. break;
  68.  
  69. case 'E':
  70. break;
  71. }
  72. }
  73. while(movement != 'E');
  74.  
  75. cin.get();
  76.  
  77. return 0;
  78. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC