### Help, Simple 2D array Game

Hey all,

I need some help on first 2D array game. To make this game I just took a simple approach making a drawing function that draws the array, and a function that sets my array to default values. Then check for arrow keys pressed then set player new position, clear screen and redraw the array. My program works pretty much fine expect for 2 problems. Heres a bit of my code.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````int keys; do { if(keys=_getch()==72) // Up key { firstmap(map); // set all values of the array back to default currenty = currenty - 1; map[currenty][currentx] = "*"; // sets player new position system("CLS"); // clear screen drawmap(map); // redraws new array with new player position } if(keys=_getch()==75) // Left Key { firstmap(map); currentx = currentx - 1; map[currenty][currentx] = "*"; system("CLS"); drawmap(map); } if(keys=_getch()==77) // Right key { firstmap(map); currentx = currentx + 1; map[currenty][currentx] = "*"; system("CLS"); drawmap(map); } if(keys=_getch()==80) // down key { firstmap(map); currenty = currenty + 1; map[currenty][currentx] = "*"; system("CLS"); drawmap(map); } }while(map[3][4]!="*");``````

When my program enters the loop only the last if statement will work. For example in my code above my program will run flawlessly for the down key but not for the rest of the keys. If I switch the last if statement to the up key it will work fine for the up key nothing else(same for the rest of the keys). As well my second question is how come I have to press the button twice in order for it to work.
Last edited on
@BruteCOder
You may want to try this.
 ``1234`` ``````#define UP 72 #define LEFT 75 #define RIGHT 77 #define DOWN 80 ``````

Then check with
 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````int keys = _getch(); switch(keys) { case UP: { firstmap(map); // set all values of the array back to default currenty = currenty - 1; map[currenty][currentx] = "*"; // sets player new position system("CLS"); // clear screen drawmap(map); // redraws new array with new player position break; } case LEFT: { firstmap(map); currentx = currentx - 1; map[currenty][currentx] = "*"; system("CLS"); drawmap(map); break; } case RIGHT: { firstmap(map); currentx = currentx + 1; map[currenty][currentx] = "*"; system("CLS"); drawmap(map); break; } case DOWN: { firstmap(map); currenty = currenty + 1; map[currenty][currentx] = "*"; system("CLS"); drawmap(map); break; }``````
^^Thanks alot worked like a charm
Topic archived. No new replies allowed.