/** * ChaiLove: Snake * http://github.com/libretro/libretro-chailove * * This is an implementation of the classic Snake game, using ChaiLove. * * @license MIT * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the “Software”), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ /** * Globals */ global WIDTH = 32 global HEIGHT = 32 global LEN = 5 global gridScale = 10 global S global dir global t = 0.0f global T /** * ChaiLove Callback; Configure the game. */ def conf(t) { t.window.width = WIDTH * gridScale t.window.height = HEIGHT * gridScale t.version = "0.27.0" } /** * ChaiLove Callback; Reset the game. */ def reset() { S = [] LEN = 5 for (var i = 0; i < LEN; ++i) { S.push_back([WIDTH / 2, HEIGHT / 2]) } dir = 0 t = 0 T = [love.math.random(WIDTH - 2), love.math.random(HEIGHT - 2)] } /** * ChaiLove Callback; Initialize the game. */ def load() { love.graphics.setBackgroundColor(133, 188, 22) reset() } /** * ChaiLove Callback; Update the game state. */ def update(dt) { t = t + dt if (love.joystick[0].isDown("up")) { if (dir != 2) { dir = 1 } } if (love.joystick[0].isDown("down")) { if (dir != 1) { dir = 2 } } if (love.joystick[0].isDown("left")) { if (dir != 4) { dir = 3 } } if (love.joystick[0].isDown("right")) { if (dir != 3) { dir = 4 } } if (t >= 0.08f) { t = 0.0f } else { return } var head = S.back() switch (dir) { case (1) { S.push_back([head[0] + 0, head[1] - 1]) break } case (2) { S.push_back([head[0] + 0, head[1] + 1]) break } case (3) { S.push_back([head[0] - 1, head[1] + 0]) break } case (4) { S.push_back([head[0] + 1, head[1] + 0]) break } } head = S.back() if (head[0] < 0 || head[0] == WIDTH || head[1] < 0 || head[1] == HEIGHT) { reset() } for (var i = 0; i < S.size() - 1; ++i) { if (head[0] == S[i][0] && head[1] == S[i][1] && dir > 0) { reset() } } if (head[0] == T[0] && head[1] == T[1]) { T = [love.math.random(WIDTH - 2), love.math.random(HEIGHT - 2)] } else if (dir > 0) { var newS = [] for (var sTime = 1; sTime < S.size(); ++sTime) { newS.push_back(S[sTime]) } S = newS } } /** * ChaiLove Callback; Render the game. */ def draw() { // Display the background grid. love.graphics.setColor(134, 201, 0) for (var y = 0; y < HEIGHT; ++y) { for (var x = 0; x < WIDTH; ++x) { love.graphics.rectangle("fill", x*gridScale, y*gridScale, gridScale - 2, gridScale - 2) } } // Render the snake. love.graphics.setColor(216, 6, 0) for (var i = 0; i < S.size(); ++i) { love.graphics.rectangle("fill", S[i][0]*gridScale, S[i][1]*gridScale, gridScale - 2, gridScale - 2) } // Show the food. love.graphics.setColor(2, 116, 132) love.graphics.rectangle("fill", T[0]*gridScale, T[1]*gridScale, gridScale - 2, gridScale - 2) } /** * ChaiLove Callback; Load the game state from a given JSON string. */ def loadstate(data) { var info = from_json(data) LEN = info["number"] // The state loaded correctly, so return true. return true } /** * ChaiLove Callback; Save the game state to a JSON string. */ def savestate() { // Construct the JSON data. var data = ["number": LEN] // Tell the system to save the JSON data. return to_json(data) }