manio143 / Snake

Simple console game in C++ with ncurses
MIT License
0 stars 1 forks source link

How the snake body grow #1

Open hbtalha opened 4 years ago

hbtalha commented 4 years ago

The game works really well, but the code is a bit hard to understand. I have created a similar game with ncurses too, however I can't seem to figure how to make the snake grow longer as it eats the fruits. In your I haven't yet been able to find how the funtions work to make it happen. An insight would be appreciated

manio143 commented 4 years ago

The Snake object has a field body that is a list of points on the screen. During every move we call makeMove for the head and bodyMove for the rest. In bodyMove we call snakeSwap, the same method that is called if snake eats an apple (in checkForApple). The point of snakeSwap is to move part i+1 of the body to the current position of i (and it is done from the end). If snakeSwap is called with a number that is greater than any index in list body then a new element is added at the end of the snake, at the same position as the current last element. So in that moment we can't see the added element. During the next iteration the added last element will stay in the same place, but the now second to last element will move to the position of the third to last and we'll get the feeling of snake growth.

hbtalha commented 4 years ago

I have read your explaination a few times while looking at the code and trying to put the pieces together, it is going slow (there are a lot of functions), and it is impossible to debug it and see how the program is working. I am getting something new after each read and I am sure I will eventually get it all. Any question I will be sure to post it here. Thanks

hbtalha commented 4 years ago

The code is proving hard for me to understand it. I have a couple of questions:

manio143 commented 4 years ago

The idea was to return an array of elements [widthheight]. table[x + y width] tells the contents of field (x,y). It's updated each cycle by getTable() and later used by draw(). Field of table is either 'space', 'a' (apple), 'h' (head), 'b' (body). So the idea is to have snake and the map representation independent of the UI library used (more or less). Why is table part of snake? Because I didn't want to allocate memory for it every cycle, so it's allocated once in the constructor.

hbtalha commented 4 years ago

The code seems pretty reasonable but the part where it makes the snake grow is still a puzzle for me, I think that because it is practically impossible to debug and see follow the inner workings, that I can't fully understand it. Besides that I really enjoyed reading the code of your game. I was able to finish my version of the snake game, was able to make the snake grow. The game source code took two video tutorials that helped put it all together.

https://www.youtube.com/watch?v=ucmigNoLPeg&list=PL2U2TQ__OrQ8jTf0_noNKtHMuYlyxQl4v&index=10&t=0s

https://www.youtube.com/playlist?list=PLrjEQvEart7dPMSJiVVwTDZIHYq6eEbeL The tutorials from the playlist made me able to make the snake grow

Here is the source of the game I wrote, please take a look and tell me what you think https://drive.google.com/open?id=1ZE3WneNzzNnyNIwGQbxWUVeicxFgfnbN