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. :)
#include <iostream>
usingnamespace std;
char board[8][8];
shortint playerx, playery; //Keeps track of the position of the player, p.
shortint 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.
}
}
elseif(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.
}
}
elseif(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.
}
}
elseif(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.
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.
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.