### Text Game

Made a simple text game but I cannot or dont even know where to begin on
Do I have to point out each location where the wall is going to be??
That would be pain. Is there an easier way?

Right now I only have 1 wall which is 0
The player is number 8
and the background is 1

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879`` ``````#include #include #include #include using namespace std; int main () { // Room Size int x = 10; int y = 10; // Some variables int grid[y][x]; int pX = 0; int pY = 0; string pCommand; // Loop GameLoop: system("CLS"); // Collison walls if (pCommand == "u"){ if (pY > 0 && grid[pY-1][pX] != 0){ pY -= 1; } } if (pCommand == "d"){ if (pY < y-1 && grid[pY+1][pX] != 0){ pY += 1; } } if (pCommand == "l"){ if (pX > 0 && grid[pY][pX-1] != 0){ pX -= 1; } } if (pCommand == "r"){ if (pX < x-1 && grid[pY][pX+1] != 0){ pX += 1; } } // Refresh/Placing everything for(int i=0; i> pCommand; if (pCommand == "e"){ goto Exit; } goto GameLoop; Exit: getch(); }``````
Last edited on
 ``12345678910111213141516171819202122`` ``````// The size of an array in C++ has to be constant but some compilers // accepts it anyway. The names x and y sounds like coordinates, // naming them width and hight might make more sense. In that case // you can use x and y as names instead of i and n because they are // actually coordinates. const int x = 10; const int y = 10; // You can initialize the grid this way int grid[y][x] = { 8, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };``````

Last edited on
Wow thanks a lot it worked!
But now when I type "u" I go right

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960`` ``````int main () { // Room const int width = 10; const int height = 10; int grid[width][height]; // Some variables int pX = 0; int pY = 0; string pCommand; // Loop GameLoop: system("CLS"); // Collison walls if (pCommand == "u"){ if (pY > 0 && grid[pY-1][pX] != 0){ pY -= 1; } } if (pCommand == "d"){ if (pY < height-1 && grid[pY+1][pX] != 0){ pY += 1; } } if (pCommand == "l"){ if (pX > 0 && grid[pY][pX-1] != 0){ pX -= 1; } } if (pCommand == "r"){ if (pX < width-1 && grid[pY][pX+1] != 0){ pX += 1; } } // Refresh/Placing everything for(int x=0; x
I think you have your x and y mixed up (i.e. grid[pX][pY]), but really it's just however you understand it.
Topic archived. No new replies allowed.