Snake Tail Issues

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
WowsoEdgy
Prole
Posts: 3
Joined: Mon Sep 26, 2016 8:42 pm

Snake Tail Issues

Post by WowsoEdgy »

So I've been trying to make a snake game, nothing special or original, just a basic snake game. But the tail movement has turned out to be quite the road block, I've set up the movement of the head (mostly) as well as a randomly appearing "pellet". I'm pretty new, I've just learned LUA in the past month.

I'm trying to approach the tail by having the "head" be an independently moving unit and having the tail be confined to a map. Whenever the head passes over a map unit I'm trying to set that unit with a "tail.length" value, and every 10 ticks each map unit should decrease by one until it hits zero. If you remove the "makeTail" function from love.update then you'll better see what I'm going for. However, this hasn't really worked properly. I'd prefer help making this option work over a whole new design, but any input is good input. Thanks!
Attachments
main.lua
(8.83 KiB) Downloaded 140 times
Snake.zip
(1.11 KiB) Downloaded 135 times
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Snake Tail Issues

Post by HugoBDesigner »

I've worked on snake before, and in my opinion, the best way to set it up is by having a table that contains each "block" of the snake's body. for the movement, you'd just remove the last item from that table, but then insert a new one where the head is going. If you need an example, feel free to check my Minigame Simulator to check the code for Snake. I dunno if that's of much help though, as your method is still not clear for me :T
@HugoBDesigner - Twitter
HugoBDesigner - Blog
WowsoEdgy
Prole
Posts: 3
Joined: Mon Sep 26, 2016 8:42 pm

Re: Snake Tail Issues

Post by WowsoEdgy »

Yeah I really don't know how to do that yet. The only reason I was doing it the way I was doing it was because I started it and felt obligated to finish it, but if you say that way is harder than it needs to be then I'll take your word for it. If at all possible could you do a quick tables rundown for me?
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Snake Tail Issues

Post by HugoBDesigner »

Well, to simplify it (excluding self collision, food-eating, wall collision, etc).
Assuming you created a table "snake" for your snake, and assuming each item on the table is a {x, y} table, and assuming you want the latest item on the table to be the head, and the first to be the tail, and assuming you're calling this function with "up", "down", "left" or "right" whenever you want the snake to move:

Code: Select all

function moveSnake(dir)
	table.remove(snake, 1)
	local lastX, lastY = unpack(snake[#snake])
	if dir == "up" then
		table.insert(snake, {lastX, lastY-1})
	elseif dir == "down" then
		table.insert(snake, {lastX, lastY+1})
	elseif dir == "left" then
		table.insert(snake, {lastX-1, lastY})
	elseif dir == "right" then
		table.insert(snake, {lastX+1, lastY})
	end
end
@HugoBDesigner - Twitter
HugoBDesigner - Blog
WowsoEdgy
Prole
Posts: 3
Joined: Mon Sep 26, 2016 8:42 pm

Re: Snake Tail Issues

Post by WowsoEdgy »

Ok, so I get the gist of what this code is intended to do; however, I don't really know how to make it functional in my code. I'm going to just blame it on only having a month of experience. I've made a snake table, but when I run it results it "bad argument #1 to 'unpack' (table expected, got nil)", and I'm not quite sure why snake table isn't being recognized. I'm, sure it's a really easy fix but what can I say? I'm a fool. :crazy:
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Snake Tail Issues

Post by s-ol »

HugoBDesigner wrote:Well, to simplify it (excluding self collision, food-eating, wall collision, etc).
Assuming you created a table "snake" for your snake, and assuming each item on the table is a {x, y} table, and assuming you want the latest item on the table to be the head, and the first to be the tail, and assuming you're calling this function with "up", "down", "left" or "right" whenever you want the snake to move:
you haven't defined the table at all, or you are trying to access the table before creating it.

And your quick table rundown: https://www.lua.org/pil/2.5.html (feel free to start from the beginning, try to read until the end).

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Post Reply

Who is online

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