Help with shooter console game

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#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.
Here is some code with a moving character. It's been commented so you can see what is happening. ;)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>

using namespace std;

char board[8][8];
short int playerx, playery; //Keeps track of the position of the player, p.
short int i = 0, j = 0;
char move;

int main()
{
    while(i < 8) //Make a blank board
    {
        while(j < 8)
        {
            board[i][j] = '-';
            ++j;
        }
        j = 0;
        ++i;
    }
    board[0][0] = 'p'; //Put the player in the top corner.
    playerx = 0; //Player x is position is 0.
    playery = 0; //Player y position is 0.
    i = 0;
    while(1) //Infinite loop.THIS IS WHAT YOU WANT TO LOOK AT! ---------------------------------------------------------------------------
    {
        cin >> move;
        if(move == 'w' || move == 'W')//In case they have caps lock on, check for both.
        {
            if(playery == 0) //If we are already at the top of the board!
            {
                cout << "Already at board edge!" << endl; //We can't do this!
            }
            else //Otherwise
            {
                --playery; //We can move up the board one space.
            }
        }
        else if(move == 's' || move == 'S')//In case they have caps lock on, check for both.
        {
            if(playery == 7) //If we are already at the bottom of the board!
            {
                cout << "Already at board edge!" << endl; //We can't do this!
            }
            else //Otherwise
            {
                ++playery; //We can move down the board one space.
            }
        }
        else if(move == 'd' || move == 'D')//In case they have caps lock on, check for both.
        {
            if(playerx == 7) //If we are already at the left of the board!
            {
                cout << "Already at board edge!" << endl; //We can't do this!
            }
            else //Otherwise
            {
                ++playerx; //We can move across the board one space.
            }
        }
        else if(move == 'a' || move == 'A')//In case they have caps lock on, check for both.
        {
            if(playerx == 0) //If we are already at the edge of the board!
            {
                cout << "Already at board edge!" << endl; //We can't do this!
            }
            else //Otherwise
            {
                --playerx; //We can move across the board one space.
            }
        } //BELOW HERE IS JUST DRAWING ------------------------------------------------------------------------------------------------------
        i = 0;
        j = 0;
        while(i < 8) //Make a blank board
        {
            while(j < 8)
            {
                board[i][j] = '-';
                ++j;
            }
            j = 0;
            ++i;
        }
        board[playery][playerx] = 'p'; //Then OVERWRITE the square the player is in.
        i = 0;
        j = 0;
        while(i < 8)
        {
            while(j < 8)
            {
                cout << board[i][j] << " ";
                ++j;
            }
            cout << "\n\n";
            j = 0;
            ++i;
        }
        cout << "\n\n\n\n";
    }
    return 0;
}


Apply the same stuff to your code. You forgot stuff like preventing the character being able to move through the border of the board. Obviously, you have more issues to solve too, such as preventing the character and enemy being on the same square. When it comes to enemy movement, you can make it random by using the rand() function to generate random numbers you can make moves with, or use lots of if/else type statements to try to make the computer play well.
Thanks, could you give any tips on how to implement shooting? like if i type r how do i get a bullet to move across the screen at the enemy?
Last edited on
Console mode isn't really made for things like shooting bullets. You really want a game library like Allegro with it's timers and graphics.

The best you can do in console is probably to put an update() event. You could press for example, f, to fire your weapon. This would create a 'bullet' on screen facing the direction of your character. You can have an event update() that looks for existance of bullets and moves them in the direction they are going. This way, the bullet can move 2x as fast as players, or more, depending on what you want to do.

You can make update() look for bulletx == enemyx, bullety == enemyy and then remove 1 health from your enemy if the bullet hits them.

Man console mode is just not really made for game programming though. For proper animations, you will have to look further afield one day, to Allegro, SDL, OpenGL, for example.

ty for all the help. This is the first time i'm seeing update() before, can you explain how it works or i would i go about setting that up?
I mean writing your own function, void update()

It would look something like:

void update()
{
//Check to see if there are any bullets on the grid.

//Go through each bullet one by one as you find them.

//Move the bullet. If it 'hits' a wall, delete it. If it hits an enemy, take health away from them and delete the bullet.

//Cycle through to the next bullet.
}

This way, you can have bullets travelling faster than players, since you can specify how far the bullet moves each turn. You can't *really* make animations in console mode. For anything more fancy, you're going to have to get a library (I recommend Allegro for game development of this kind) and learn it.
Topic archived. No new replies allowed.