Hello!
How do I make my snake tutorial game to reset?
I’ve made the grow food on the right places step, made a button and a reset function which is the same as the init function but the problem is that it gets the same as where it ended. I dunno what function/code to use here.
local function put_food(self)
self.food = {x = math.random(2, 47), y = math.random(2, 47)}
local tile = tilemap.get_tile("#grid", "layer1", self.food.x, self.food.y)
if tile == 2 or tile == 4 then
put_food(self)
else
tilemap.set_tile("#grid", "layer1", self.food.x, self.food.y, 3)
end
end
function init(self)
msg.post(".", "acquire_input_focus")
msg.post("/button#button", "disable")
self.segments = {
{x = 7, y = 24},
{x = 8, y = 24},
{x = 9, y = 24},
{x = 10, y = 24} }
self.dir = {x = 1, y = 0}
self.dirqueue = {}
self.speed = 7.0
self.alive = true
self.t = 0
math.randomseed(socket.gettime())
put_food(self)
end
function reset(self)
msg.post("/button#button", "disable")
self.segments = {
{x = 7, y = 24},
{x = 8, y = 24},
{x = 9, y = 24},
{x = 10, y = 24} }
self.dir = {x = 1, y = 0}
self.dirqueue = {}
self.speed = 7.0
self.alive = true
self.t = 0
math.randomseed(socket.gettime())
put_food(self)
end
function update(self, dt)
self.t = self.t + dt
if self.t >= 1.0 / self.speed and self.alive then
local newdir = table.remove(self.dirqueue, 1)
if newdir then
local opposite = newdir.x == -self.dir.x or newdir.y == -self.dir.y
if not opposite then
self.dir = newdir
end
end
local head = self.segments[#self.segments]
local newhead = {x = head.x + self.dir.x, y = head.y + self.dir.y}
table.insert(self.segments, newhead)
local tile = tilemap.get_tile("#grid", "layer1", newhead.x, newhead.y)
if tile == 2 or tile == 4 then
self.alive = false
msg.post("/button#button", "enable")
elseif tile == 3 then
self.speed = self.speed + 1
put_food(self)
msg.post("/gui#ui", "add_score", {score = 10})
else
local tail = table.remove(self.segments, 1)
tilemap.set_tile("#grid", "layer1", tail.x, tail.y, 0)
end
for i, s in ipairs(self.segments) do
tilemap.set_tile("#grid", "layer1", s.x, s.y, 2)
end
self.t = 0
end
end
function on_input(self, action_id, action)
if action_id == hash("up") and action.pressed then
table.insert(self.dirqueue, {x = 0, y = 1})
elseif action_id == hash ("down") and action.pressed then
table.insert(self.dirqueue, {x = 0, y = -1})
elseif action_id == hash ("left") and action.pressed then
table.insert(self.dirqueue, {x = -1, y = 0})
elseif action_id == hash ("right") and action.pressed then
table.insert(self.dirqueue, {x = 1, y = 0})
end
end
function on_message(self, message_id, message, sender)
if message_id == hash("reset") then
reset(self)
end
end