0,]']±>Sï}WÿÍu§ðp8·d%qy)6o;]ÉA¦ösï÷ôôô”°ÂVl†3 3 then return 1 end return val end local printCentered printCentered = function(text, y, color, fixed, scale, smallfont) if y == nil then y = 0 end if color == nil then color = 15 end if fixed == nil then fixed = false end if scale == nil then scale = 1 end if smallfont == nil then smallfont = false end local len len = print(text, WIDTH, HEIGHT, color, fixed, scale, smallfont) return print(text, (WIDTH - len) // 2, y, color, fixed, scale, smallfont) end local Snake do local _class_0 local _base_0 = { pushInput = function(self, d) if self.buf[1] then if self.buf[2] then self.buf[1] = self.buf[2] self.buf[2] = d else self.buf[2] = d end else self.buf[1] = d end end, popInput = function(self) if self.buf[2] then self.dir = self.buf[1] self.buf[1] = self.buf[2] self.buf[2] = nil elseif self.buf[1] then self.dir = self.buf[1] self.buf[1] = nil end end, lastInput = function(self) if self.buf[2] then return self.buf[2] elseif self.buf[1] then return self.buf[1] else return self.dir end end } _base_0.__index = _base_0 _class_0 = setmetatable({ __init = function(self) self.x = 2 self.y = 2 self.len = 2 self.dir = "r" self.buf = { } end, __base = _base_0, __name = "Snake" }, { __index = _base_0, __call = function(cls, ...) local _self_0 = setmetatable({}, _base_0) cls.__init(_self_0, ...) return _self_0 end }) _base_0.__class = _class_0 Snake = _class_0 end local State do local _class_0 local _base_0 = { tic = function(self) return self[self.screen](self) end, genFood = function(self) local posx, posy posx = math.random(1, w) posy = math.random(1, h) while self.grid[posx][posy] ~= gEMPTY do posx = math.random(1, w) posy = math.random(1, h) end self.grid[posx][posy] = gFOOD end, resetGrid = function(self) self.grid = self.grid or { } for x = 1, w do self.grid[x] = self.grid[x] or { } for y = 1, h do self.grid[x][y] = gEMPTY end end end, init = function(self) pmem(mems.diff, self.diff) self.snake = Snake() self.speed = speeds[self.diff] self:resetGrid() self.grid[self.snake.x][self.snake.y] = gHEAD return self:genFood() end, title = function(self) cls(15) local color color = 2 printCentered("Snek", HEIGHT // 3, color, false, 3) printCentered("High Score " .. tostring(diff_names[self.diff]) .. ": " .. tostring(self.hs[self.diff]), HEIGHT * (4 / 7), color) printCentered("press a(z on keyboard) to start.", HEIGHT * (2 / 3), color) if btnp(0) then self.diff = self.diff - 1 if self.diff == 0 then self.diff = 3 end local speed = speeds[self.diff] end if btnp(1) then self.diff = self.diff + 1 if self.diff == 4 then self.diff = 1 end end if btn(4) then self.screen = "playing" return self:init() end end, moveSnake = function(self) local c for x = 1, w do for y = 1, h do c = self.grid[x][y] if c > 0 then local _update_0, _update_1 = x, y self.grid[_update_0][_update_1] = self.grid[_update_0][_update_1] - 1 end end end end, extendSnake = function(self) local c for x = 1, w do for y = 1, h do c = self.grid[x][y] if c > 0 then local _update_0, _update_1 = x, y self.grid[_update_0][_update_1] = self.grid[_update_0][_update_1] + 1 end end end end, drawGrid = function(self) local c for x = 1, w do for y = 1, h do c = self.grid[x][y] local _exp_0 = c if gHEAD == _exp_0 then local rot local _exp_1 = self.snake.dir if "r" == _exp_1 then rot = 0 elseif "d" == _exp_1 then rot = 3 elseif "l" == _exp_1 then rot = 2 elseif "u" == _exp_1 then rot = 1 end spr(256, (x - 1) * 8, (y - 1) * 8, 0, 1, 1, rot) elseif gFOOD == _exp_0 then spr(258, (x - 1) * 8, (y - 1) * 8, 0) elseif gEMPTY == _exp_0 then local _ = "" else spr(257, (x - 1) * 8, (y - 1) * 8, 0) end end end end, snakeTic = function(self) self:moveSnake() self.snake:popInput() local d d = dirs[self.snake.dir] self.grid[self.snake.x][self.snake.y] = self.snake.len self.snake.x = self.snake.x + d.x if self.snake.x > w then self.snake.x = 1 end if self.snake.x == 0 then self.snake.x = w end self.snake.y = self.snake.y + d.y if self.snake.y > h then self.snake.y = 1 end if self.snake.y == 0 then self.snake.y = h end self.snake.last = opposite[self.snake.dir] local c c = self.grid[self.snake.x][self.snake.y] if c == gFOOD then self.snake.len = self.snake.len + 1 self:extendSnake() self:genFood() elseif c > 0 then self.screen = "gameover" self.t = 0 local score score = self.snake.len - 2 if score > self.hs[self.diff] then self.hs[self.diff] = score pmem(mems[diff_names[self.diff]], score) end return end self.grid[self.snake.x][self.snake.y] = gHEAD cls(13) self:drawGrid() return print("SCORE: " .. tostring(self.snake.len - 2)) end, playing = function(self) self.t = self.t + 1 local dir dir = self.snake.dir if btnp(0) and "u" ~= opposite[self.snake:lastInput()] then self.snake:pushInput("u") end if btnp(1) and "d" ~= opposite[self.snake:lastInput()] then self.snake:pushInput("d") end if btnp(2) and "l" ~= opposite[self.snake:lastInput()] then self.snake:pushInput("l") end if btnp(3) and "r" ~= opposite[self.snake:lastInput()] then self.snake:pushInput("r") end if self.t % self.speed == 0 then return self:snakeTic() end end, gameover = function(self) cls(2) print("GAME OVER :(", 84, 84) self.t = self.t + 1 if self.t >= 110 or (self.t > 30 and (btnp(0 or btnp(1 or btnp(2 or btnp(3)))))) then self.screen = "title" end end } _base_0.__index = _base_0 _class_0 = setmetatable({ __init = function(self) self.t = 0 self.hs = loadHiScores() self.diff = loadDiff() self.screen = "title" end, __base = _base_0, __name = "State" }, { __index = _base_0, __call = function(cls, ...) local _self_0 = setmetatable({}, _base_0) cls.__init(_self_0, ...) return _self_0 end }) _base_0.__class = _class_0 State = _class_0 end local state BOOT = function() state = State() end TIC = function() return state:tic() end