Your question is “how to fix this code”, not “give me the right code”. I will not give you the right code. I will answer the original question. The answer to that question is this:
The problem is that you do cout << "***"
. This will alwys draw three asterisks horizontally. That command will never do anything else. To draw items vertically, or which ever shape the snake may make, you have to draw each asterisk separatelly, on its own coordinate.
Instead of having only one coordinate (variables x
and y
) you have to have the coordinates of every part of the snake. Use std::queue<COORD>
to remember the coordinate of every part of the snake. To make the snake move draw space (” “) on the last item in the queue and remove the last item from the back of the queue (pop_back
), and add new item in the front of the queue (push_front
) and draw asterisk there (“*”).
When you want to make the snake bigger don’t remove items from the queue, but just add new.
3
solved How to change direction in snake game [closed]