Hi all, came across this forum recently and seems like a good resource for me to learn and ask for help when needed, so today I joined.
Anyway, I am new to C++. I am attempting to build a ASCII style snake game and currently have been stuck for the past day or so figuring out how to get my snake to move. Quite frustrating, however I am motivated to overcome this.
So far I have wall class with a nested for loop which draws my border 20 by 30. I have this in a function which I call from my source.cpp.
The next step for me is to draw a snake on the screen, preferably within the border I have drawn. I also need to make it move. I think I need a 2D Array to do this, I have looked into fixed arrays and I know I will need to make one big enough.
I have created a very basic snake class with a 2D array within it also with two int variables, snakeX & snakeY to control its movement. I'm not quite sure where to go from here, some guidance would be great please!
Thanks in advance.
void Wall::drawWall()
{
for (int i = 0; i < across; i++)
{
for (int j = 0; j < down; j++)
{
if (i == 0 || i == across - 1)
{
cout << "*";
}
else
{
if (j == 0 || j == down - 1)
cout << "+";
else
cout << " ";
}
}
cout << "\n";
}
}
^ That is how I am drawing the borders currently.
Snake class code:
private:
constint maxSnake = 1000;
char snakeBody[maxSnake][maxSnake];
int snakeX;
int snakeY;
1.Make a 2D array to help you store the apples and snake movements
2.Use this function to move cursor on console and print snake parts
void gotoxy( int column, int line )
{
COORD coord;
coord.X = column;
coord.Y = line;
SetConsoleCursorPosition(
GetStdHandle( STD_OUTPUT_HANDLE ),
coord
);
}
3.Start with the head of the snake, create a variable that holds the direction the snake head is moving currently , create a function that gets input from keyboard like w a s d or arrow keys then sets the direction in which the head should move.
4.After getting direction move snake head using gotoxy() function i just provided you (tip: use Sleep() function to set a pause between every move made by the snake so the snake wont move instantly )
5.Now here is the hardest part, you have to make an array that stores past positions where the head was so when you start eating apples and growing your body, you can make your body follow the head.
I think I gave you enough tips.
Oh almost forgot use rand() function to generate random position for your apples to spawn , remember to include <cstdlib> for rand(), and <windows> for Sleep() function.
Thanks for the reply, its really helped me make some progress.
I have a function within my snake class which I am calling from my source.cpp to allow me to move the snake. I have default constructor which at the moment is drawing the head of the snake and placing it at element[0] in the array.
I then call the gotoxy function and I have two variables that I place in as parameters.
(headX, headY);
I have also an input function for the snake to test its movement. However it only seems to move once only when I press the W key. No other input is working. I'll paste some code below to try and help you understand.
/* Code */
Private variables from snake class:
char snakeBody[maxSnake]; maxSnake = 1000;
char snakeHead = 'O';
int headX; headX & headY are set in the default constructor to 10 at the moment.
int headY;
--------------------------------------------------------------
snake::snake()
{
snakeBody[0] = snakeHead;
int headX = 10;
int headY = 10;
gotoxy(headX, headY);
cout << snakeHead;
}
--------------------------------------------------------------
void snake::Input()
{
if (_kbhit())
{
I have created a s & d also. Which just change the headX or headY depending on user input.
}
I then call the Input function continuously with an instance of snake from my source.cpp. I'll go over my code to see if I can spot the errors, but thanks for the help so far!
while(gameRunning == true)
{
staticint dir = getInput() // function which returns dir value which is the direction of snake
// the function returns last dir value if no other key was pressed
goto(dir) // function that moves the snake based on dir value
Sleep(200);
}
the only way i can think of doing it without multy threading, the loop is very fast thats why i put Sleep(200) to make a pause of 200 ms after every move
so I have to get the input from the class and then input into a function in the class if i understand correctly?
I tried two different functions just now, but I think I did not understand you.