How to store/extend the snake in a recreation of the classic

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
flinnburgess
Prole
Posts: 5
Joined: Thu Apr 03, 2014 7:56 pm

How to store/extend the snake in a recreation of the classic

Post by flinnburgess »

Hey guys, hope I'm posting this in the right place, I apologise in advance if this topic has come up before.
I'm trying to recreate the classic game snake to get to grips with Love2D. So far I've got a single square moving around the grid at a desirable speed. I'm trying to implement the snake growth mechanics now but I'm not sure what the best way to go about it is. My current idea is to store the coordinates of each segment of the snake in a 2d table and then when the head collides with a "dot", I would duplicate the final element of the table and update the remaining elements, if that makes sense:

{{3, 0}, {2, 0}, {1, 0}, {1, 1}, {1, 2}} would then change to
{{3, 0}, {2, 0}, {1, 0}, {1, 1}, {1, 2} {1, 2}} which would then become
{{4, 0}, {3, 0}, {2, 0}, {1, 0}, {1, 1} {1, 2}}

Is this an effective method of doing it? I can't seem to think of any effective alternatives but I just don't feel like it would be very easy to implement. I'm going to see if I can but I'd appreciate any advice you guys could give me in the means time.
flinnburgess
Prole
Posts: 5
Joined: Thu Apr 03, 2014 7:56 pm

Re: How to store/extend the snake in a recreation of the cla

Post by flinnburgess »

Hey guys, just a quick update.

I've pretty much finished writing the code for the game to the point where it almost resembles the original snake. The main problem now is that the snake isn't growing when it picks up food. I've included the lua file so anyone can have a look at it, my guess would be that the main areas of interest regarding this question would be the check_collision function on line 131 and snake_draw on line 43. Sorry about putting it all in one file, I was having some problems with splitting it across multiple files. snake_move might be of some interest as well, I'm not sure.

If anyone can help me figure out why this might be happening I'd be really grateful. I've put a little debug counter in so I know that it's adding tables to the snake segment table. I thought maybe love.draw wasn't updating, or there's a problem with the for loop in snake_draw?
Attachments
snake.love
(1.27 KiB) Downloaded 123 times
mikeisinlove
Prole
Posts: 44
Joined: Sun Mar 31, 2013 11:55 am

Re: How to store/extend the snake in a recreation of the cla

Post by mikeisinlove »

I've only skimmed the code but judging from what you've said and what I saw in the collision function I think you're going about it in a really cumbersome way. Have a variable for the current length (lets say n) then every frame push the current position in and pop the nth one out, then when you eat something just increase n by one. Right now it looks like you're manually trying to manage things you don't need to.
flinnburgess
Prole
Posts: 5
Joined: Thu Apr 03, 2014 7:56 pm

Re: How to store/extend the snake in a recreation of the cla

Post by flinnburgess »

That worked fantastically, thanks! Ignorance on the way tables work on my part.
Now i'm having an issue with food spawning on the snake which I can't seem to get rid of. I've tried to in the food-check function but it doesn't seem to be working and I'm not sure why not. Would it make more sense to have a 2d table of 1's and 0's to denote which squares are already taken, which i would then compare to the food coordinates?
Attachments
snake.love
(1.32 KiB) Downloaded 346 times
mikeisinlove
Prole
Posts: 44
Joined: Sun Mar 31, 2013 11:55 am

Re: How to store/extend the snake in a recreation of the cla

Post by mikeisinlove »

flinnburgess wrote:That worked fantastically, thanks! Ignorance on the way tables work on my part.
Now i'm having an issue with food spawning on the snake which I can't seem to get rid of. I've tried to in the food-check function but it doesn't seem to be working and I'm not sure why not. Would it make more sense to have a 2d table of 1's and 0's to denote which squares are already taken, which i would then compare to the food coordinates?
There are lots of things you could do, though generating random coords and comparing it something doesn't sound ideal, consider what would happen if you had filled up 90% with the snakes body: it generates coords, taken, generate, taken, generate, taken you could potentially hang the system.

You might as well just have a table that goes {{1,1},{1,2}... {2,1},{2,2}... etc generated for whatever the size the grid is then each time you want to add, loop through and remove any coords that match any of the snake coords, then you're left with essentially one long list of every possible coordinate that wont collide with the snake, so picking any of them is guaranteed to work.
flinnburgess
Prole
Posts: 5
Joined: Thu Apr 03, 2014 7:56 pm

Re: How to store/extend the snake in a recreation of the cla

Post by flinnburgess »

I've tried to implement what you suggested but Love doesn't seem very happy with it. I've included the file so you can have a look if you like, my main two problems at the moment are the one I just underlined which you can see if you uncomment the two lines in food.lua, and once you've gone off the main menu buttons seem to become unresponsive. You can keep track of the gamestate if you uncomment line 90 in main.lua, I can't figure out why button_click isn't working for all of them.
Attachments
snake.love
(3.84 KiB) Downloaded 113 times
mikeisinlove
Prole
Posts: 44
Joined: Sun Mar 31, 2013 11:55 am

Re: How to store/extend the snake in a recreation of the cla

Post by mikeisinlove »

Fixed that up, cleaned the moving around stuff (to my personal taste) and threw in self-collision checking while I was at it.

When using multiple files please try and make a habbit of clearly naming things, like in file.lua have file:Function() or something like that so you know which file to jump to to find something straight away.
Attachments
snake 2.love
(7.02 KiB) Downloaded 132 times
flinnburgess
Prole
Posts: 5
Joined: Thu Apr 03, 2014 7:56 pm

Re: How to store/extend the snake in a recreation of the cla

Post by flinnburgess »

Brilliant, seems to have worked well for the most part. One bug I found which I think was that the food was spawning just outside of the screen. Think I fixed it by changing the code in main to

Code: Select all

for y=0, height/16 -1 do  -- was y = 1, height/16
		for x=0, width/16 - 1 do -- same as above
			table.insert(snake_coords_clear, {x*16, y*16})
		end
	end
Still having issues with the menu which I can't seem to figure out, don't suppose you noticed it? Buttons on any page other than the menu are completely unresponsive.
Thanks for the tip about function naming, I'm new to all this (hence the incessant questions), so any tips and help is very much appreciated!
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 4 guests