I tried to merge my snake.love with the love.exe(0.7.2), but raises an error
and i also tried to bind the simple 'hello world' .love file, it worked.
Error
main.lua:112: Could not set write directory.
Traceback
[C]: in function 'write'
main.lua:112: in function 'load'
[C]: in function 'xpcall'
this is snake.love / main.lua
Code: Select all
function start()
best_score = love.filesystem.read("score")
init()
end
function init()
pause = false
dead = false
x = 400
y = 300
direction = "right"
timestamp = os.clock()
body = {{x, y}}
directions = {}
point = {}
end
function die()
dead = true
love.filesystem.write("score", best_score)
end
function check_dead()
if x >= 800 or x < 0 or y >= 600 or y < 0 then die() end
for dummy, value in pairs(body) do
if value[1] == x and value[2] == y then
die()
end
end
return dead
end
function change_direction()
if #directions ~= 0 then
for dummy, value in pairs(directions) do
table.remove(directions, 1)
if (value ~= direction) and
((value == "left" and direction ~= "right") or
(value == "right" and direction ~= "left") or
(value == "up" and direction ~= "down") or
(value == "down" and direction ~= "up")) then
direction = value
break
end
end
end
end
function next_position()
if direction == "left" then
x = -block_size + x
elseif direction == "right" then
x = block_size + x
elseif direction == "up" then
y = -block_size + y
elseif direction == "down" then
y = block_size + y
end
end
function check_point()
if x == point[1] and y == point[2] then
local result = true
point = {}
-- body length more than best_score
if #body > tonumber(best_score) then
best_score = #body
end
else
local result = false
table.remove(body, #body)
end
table.insert(body, 1, {x, y})
return result
end
function create_point()
local new_point = false
while not new_point do
maxX = 800 / block_size - 1
maxY = 600 / block_size - 1
point = {block_size * math.random(0, maxX), block_size * math.random(0, maxY)}
for key, value in pairs(body) do
if value[1] == point[1] and value[2] == point[2] then
new_point = false
break
end
new_point = true
end
end
end
function love.load()
love.graphics.setCaption("Snake")
math.randomseed(os.time())
directions_memory_max = 2
if not love.filesystem.exists("score") then
love.filesystem.newFile("score")
love.filesystem.write("score", 0)
end
-- 1, 5, 10, 20
block_size = 10
speed = 0.04
start()
end
function love.keypressed(key, unicode)
if key == "p" then
pause = not pause
end
-- restart the game.
if key == "return" and dead then
init()
end
if #directions <= directions_memory_max then
table.insert(directions, key)
end
end
function love.update(dt)
if dead or pause then return end
local _timestamp = os.clock()
if (_timestamp - timestamp) < speed then return end
timestamp = _timestamp
change_direction()
next_position()
if check_dead() then return end
if #point == 0 then create_point() end
check_point()
end
function love.draw()
if dead then
love.graphics.setColor(0, 255, 255, 255)
love.graphics.print("Press Enter to restart.", 340, 300)
return
end
if pause == true then
love.graphics.setColor(0, 255, 255, 255)
love.graphics.print("Press P to continue.", 340, 300)
end
-- draw the snake body
local alpha = 255
local down = true
for key, value in pairs(body) do
love.graphics.setColor(alpha, 255, 255, 255)
love.graphics.rectangle("fill", value[1], value[2], block_size, block_size)
if down then
alpha = alpha * 0.95
if alpha < 127 then alpha = 127 end
else
alpha = alpha * 1.05
if alpha > 255 then alpha = 255 end
end
if alpha <= 127 and down then down = not down end
if alpha >= 255 and not down then down = not down end
end
-- draw the point
if #point ~= 0 then
love.graphics.setColor(255, 255, 0, 255)
love.graphics.rectangle("fill", point[1], point[2], block_size, block_size)
end
-- show best score
love.graphics.setColor(0, 255, 0, 255)
love.graphics.print("Best Score : " .. best_score, 0, 0)
-- show current score
love.graphics.setColor(255, 0, 255, 255)
love.graphics.print("Score : " .. #body - 1, 0, 20)
end