Okay so for my beginners c++ class we have to make game for our final project. The game is a simple shooter type game with ascii chars and stuff for the player and the enemy. We covered up to 2d arrays in class and then the teacher gave us the assignment and tasked us with figuring it out ourselves.

So far i have the game board, the player and the enemy. Well the enemy doesn't move around yet still trying to figure out the movement for the player. I'm completely lost on how to code it so you can move around the board. I have it set so that you type w to move up, d for right, a for left and s for down. I can get the first initial movement to work about after that it leaves the original player character in place and moves in a diagonal line.

Any tips would be greatly for the movement would be greatly appreciated. :)

This is my code so far:

#include <cstdlib>
#include <iostream>




using namespace std;

const int SIZE = 23;
const char UP = 30;
const char DOWN = 31;
const char LEFT = 17;
const char RIGHT = 16;
const char ENEMY = 'X';
const char BULLET = 7;



void printmap(char [][SIZE], int);
void loadmap(char [][SIZE], int);



int main(int argc, char *argv[])
{


char move;
char numbers[SIZE][SIZE];
loadmap(numbers, SIZE);
printmap(numbers, SIZE);

system("CLS");
numbers[1][1] = RIGHT;
numbers[21][21] = ENEMY;
printmap(numbers, SIZE);


for(int i = 1; i < 5; i++)
{
cin >> move;
switch(move)
{
case 'w': system("CLS");
numbers[i-1][i] = UP;
printmap(numbers, SIZE);
break;

case 'a': system("CLS");
numbers[i][i-1] = LEFT;
printmap(numbers, SIZE);
break;

case 's': system("CLS");
numbers[i+1][i] = DOWN;
printmap(numbers, SIZE);
break;

case 'd': system("CLS");
numbers[i][i+1] = RIGHT;
numbers[i][i] = 0;
printmap(numbers, SIZE);
break;

case 'r':
break;
}
}







system("PAUSE");
return EXIT_SUCCESS;
}

void printmap(char inArray[][SIZE], int S)
{
for(int r = 0; r < S; r++)
{
for(int c = 0; c < S; c++)
{
//total grid
cout << inArray[r][c];
}
cout << endl;
}
}

void loadmap(char inArray[][SIZE], int S)
{
for(int r = 0; r < S; r++)
{
for(int c = 0; c < S; c++)
{
//inside space
inArray[r][c] = 0;
}
}
for(int r = 1; r < S-1; r++)
{
for(int c = 1; c < S-1; c++)
{
//top and bottom
inArray[0][c] = 205;
inArray[S-1][c] = 205;

}
}
for(int r = 1; r < S-1; r++)
{
for(int c = 1; c < S-1; c++)
{
//left and right side
inArray[r][0] = 186;
inArray[r][S-1] = 186;

}
}
//corners
inArray[0][0] = 201;
inArray[S-1][S-1] = 188;
inArray[0][S-1] = 187;
inArray[S-1][0] = 200;

}

The const UP, DOWN, LEFT, AND RIGHT are the facing directions of the player.

what you are trying to do is suprisingly not that difficult. this type of ascii game will involve some simple techniques. in any animated ascii game, it's just a matter of clearing and re-drawing the screen. you will need simple collision detection to tell when you've hit the enemy. you've mentioned that you need random movement for your enemy, here is a simple code that moves a stick figure around the screen:

#include <iostream>
#include <windows.h>
using namespace std;

enum{up, right, down, left};
int direction = 0;
int x = 20,
    y = 10;

srand(time(NULL));

char stickman[3][4];

//clear the arrays
for(int i=0; i<3; i++)
   for(int j=0; j<4; j++)
       stickman[i][j] = ' ';     
   
//The stickman
stickman[0][1] = 'O';
stickman[1][0] = '-';
stickman[1][1] = '+';
stickman[1][2] = '-';
stickman[2][1] = '^';
stickman[0][3] = '\n';
stickman[1][3] = '\n';
stickman[2][3] = '\0';

while(true)
{
     direction = rand()%4;

     switch(direction)
     {
          //Test the x,y coordinates to keep it on the screen
          case up:     if(y>0)  {y--;}  break;
          case right:  if(x<123){x++;}  break;
          case down:   if(y<152){y++;}  break;
          case left:   if(x>0)  {x--;}  break;
     }

     gotoxy(x,y); cout << stickman; 
     Sleep(800);
     system("cls");
}

void gotoxy(int x, int y)
{
     COORD coord;
     coord.X = x;
     coord.Y = y;
     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_H ANDLE), coord);
}

void set_console_size()
{
     HANDLE hOut;
     SMALL_RECT DisplayArea = {0, 0, 0, 0};
     //set x and y to whatever ye' want
     int x = 125;
     int y = 55;

     hOut = GetStdHandle(STD_OUTPUT_HANDLE);
     DisplayArea.Right  = x;
     DisplayArea.Bottom = y;

     SetConsoleWindowInfo(hOut, TRUE, &DisplayArea);
}

I gave you a few tools from the <windows.h> library that you'll need in your ascii game, the Sleep() function as a delay, gotoxy() so you can move around on the screen and set_console_size() so you can force the console window size to whatever you want; you'll know your boundries and not draw outside of the dos window.

Also, Here is a really good site to get a lot of functionality for your dos game.

New and improved version:

#include <iostream>
#include <windows.h>
using namespace std;

enum{up, right, down, left};
int direction = 0;
int x = 20,
    y = 10;

srand(time(NULL));

char stickman[3][4];

//clear the arrays
for(int i=0; i<3; i++)
   for(int j=0; j<4; j++)
       stickman[i][j] = ' ';     
   
//The stickman
stickman[0][1] = 'O';
stickman[1][0] = '-';
stickman[1][1] = '+';
stickman[1][2] = '-';
stickman[2][1] = '^';
stickman[0][3] = '\0';
stickman[1][3] = '\0';
stickman[2][3] = '\0';

while(true)
{
     direction = rand()%4;

     switch(direction)
     {
          //Test the x,y coordinates to keep it on the screen
          case up:     if(y>0)  {y--;}  break;
          case right:  if(x<123){x++;}  break;
          case down:   if(y<152){y++;}  break;
          case left:   if(x>0)  {x--;}  break;
     }

     gotoxy(x,y);   cout << stickman[0]; 
     gotoxy(x++,y); cout << stickman[1]; 
     gotoxy(x++,y); cout << stickman[2]; 

     Sleep(800);
     system("cls");
}

void gotoxy(int x, int y)
{
     COORD coord;
     coord.X = x;
     coord.Y = y;
     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_H ANDLE), coord);
}

void set_console_size()
{
     HANDLE hOut;
     SMALL_RECT DisplayArea = {0, 0, 0, 0};
     //set x and y to whatever ye' want
     int x = 125;
     int y = 55;

     hOut = GetStdHandle(STD_OUTPUT_HANDLE);
     DisplayArea.Right  = x;
     DisplayArea.Bottom = y;

     SetConsoleWindowInfo(hOut, TRUE, &DisplayArea);
}

Full working program:

#include <iostream>
#include <ctime>
#include <windows.h>

using namespace std;

void gotoxy(int, int);
void set_console_size();

int main()
{
     enum{up, right, down, left};
     char stickman[3][4];
     int direction = 0;
     int x = 40,
         y = 20;

     srand(time(NULL));
     set_console_size();
 
     //clear the arrays
    for(int i=0; i<3; i++)
        for(int j=0; j<4; j++)
            stickman[i][j] = ' ';

     //The stickman
     stickman[0][1] = 'O';
     stickman[1][0] = '-';
     stickman[1][1] = '+';
     stickman[1][2] = '-';
     stickman[2][1] = '^';
     stickman[0][3] = '\0';
     stickman[1][3] = '\0';
     stickman[2][3] = '\0';

     while(true)
     {
          direction = rand()%4;
          cout << direction << endl;

          switch(direction)
          {
               //Test the x,y coordinates to keep it on the screen
               case up:     if(y>=0)  {y--; } else{y+=3;} cout << "up";    break;
               case right:  if(x<=120){x+=3;} else{x-=3;} cout << "right"; break;
               case down:   if(y<=153){y++; } else{y-=3;} cout << "down";  break;
               case left:   if(x>=3)  {x-=3;} else{x+=3;} cout << "left";  break;          
          }

          //Draw stickman
          cout << endl << "x=" << x << ", y=" << y;
          gotoxy(x,y);   cout << stickman[0];
          gotoxy(x,y+1); cout << stickman[1];
          gotoxy(x,y+2); cout << stickman[2];

          Sleep(500);
          system("cls");
     }
}



void gotoxy(int x, int y)
{
     COORD coord;
     coord.X = x;
     coord.Y = y;
     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void set_console_size()
{
     HANDLE hOut;
     SMALL_RECT DisplayArea = {0, 0, 0, 0};
     //set x and y to whatever ye' want
     int x = 125;
     int y = 55;

     hOut = GetStdHandle(STD_OUTPUT_HANDLE);
     DisplayArea.Right  = x;
     DisplayArea.Bottom = y;

     SetConsoleWindowInfo(hOut, TRUE, &DisplayArea);
}

However you still use system("cls"); :icon_smile:

system("cls") is cheap and easy. just like my women.

commented: Hehe :) +17

By the way it is moving diagonially because you set the move in accordance to i . Each time the variable is increased in the for loop, your "character" is moving both at x and y directions.

system("cls") is cheap and easy. just like my women.

Easy, yes. Cheap, no. The 'work' needed to call the operating system, find and execute the pause command, then exit the O/S and return to the program is more like a $1000 call girl. It works well, but is far from cheap.

Microsoft Support KB article 99261 gives two methods of clearing the Windows console in C++: using system("cls"); and using a function which, while complicated, should be more or less drop-in: the most difficult part of it is the need to get the console handle, which can be gotten from GetStdHandle().

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);

    cls(console);

Mind you, if you're using Managed C++ (or any of the .Net languages), you would also have access to the Console.Clear() method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.