Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- directions = {up = {x = 0, y = -1}, down = {x = 0, y = 1}, left = {x = -1, y = 0}, right = {x = 1, y = 0}}
- function love.load()
- game = {}
- game.width = love.graphics.getWidth()
- game.height = love.graphics.getHeight()
- game.running = true
- player = {}
- player.x = game.width / 2
- player.y = game.height / 2
- player.dir = "left"
- player.speed = 150
- player.color = {255, 255, 255}
- player.points = {}
- player.max_length = 100
- love.graphics.setBackgroundColor(0, 0, 0)
- end
- function love.update(dt)
- if game.running then
- -- Figure out the next position for the snake's head
- player.x = player.x + directions[player.dir].x * player.speed * dt
- player.y = player.y + directions[player.dir].y * player.speed * dt
- table.insert(player.points, 1, {x = player.x, y = player.y})
- -- Is the player running into a wall?
- if player.x <= 0 or player.x >= game.width or player.y <= 0 or player.y >= game.height then
- game.running = false
- end
- -- Keep the snake no larger than the max_length
- if #player.points >= player.max_length then
- table.remove(player.points)
- end
- end
- end
- function love.draw()
- if game.running then
- -- Draw the snake's segments
- love.graphics.setColor(unpack(player.color))
- for _, pos in pairs(player.points) do
- love.graphics.point(pos.x, pos.y)
- end
- else
- -- Game Over Screen
- love.graphics.printf("Game Over", game.width / 2, game.height / 2, 0, "center")
- end
- end
- function love.keypressed(key)
- if directions[key] then
- player.dir = key
- end
- if key == "escape" then
- love.event.push("q")
- end
- end
Add Comment
Please, Sign In to add comment