90 lines
1.8 KiB
Lua
Raw Normal View History

2016-11-25 03:52:53 -07:00
require "Tserial"
function move(void)
2016-11-25 14:31:39 -07:00
-- with acceleration
if player.current_speed < player.speed then
player.current_speed = player.current_speed + (player.speed*player.accel)
end
return player.current_speed
2016-11-25 14:31:39 -07:00
end
2016-11-25 03:52:53 -07:00
function love.load()
love.filesystem.setIdentity("tommy")
saveFile = "test.lua"
-- Check if Save file exists
save_exists = love.filesystem.exists(saveFile)
player = {}
player.x = 100
player.y = 100
player.w = 25
player.h = 25
player.speed = 500
2016-11-25 14:31:39 -07:00
player.current_speed = 0
--Must be less than 1
--if I understand correctly, the second number should be how many frames
--it takes to get to top speed
player.accel = 1/120
2016-11-25 03:52:53 -07:00
end
function love.keyreleased(key)
if key == "q" then
love.event.quit()
end
-- save
if key == "s" then
love.filesystem.write(saveFile, Tserial.pack(player, false, true))
end
-- Load
if key == "l" then
if save_exists then
player = Tserial.unpack( love.filesystem.read( saveFile ) )
end
end
end
function love.update(dt)
--Move character left or right
if love.keyboard.isDown("right") then
move()
player.x = player.x + player.current_speed*dt
2016-11-25 14:31:39 -07:00
2016-11-25 03:52:53 -07:00
elseif love.keyboard.isDown("left") then
move()
player.x = player.x - player.current_speed*dt
2016-11-25 14:31:39 -07:00
--Reset current speed if key is released
2016-11-25 03:52:53 -07:00
end
--Also able to move or up down at the same time as one of the above
if love.keyboard.isDown("up") then
move()
player.y = player.y - player.current_speed*dt
2016-11-25 03:52:53 -07:00
elseif love.keyboard.isDown ("down") then
move()
player.y = player.y + player.current_speed*dt
2016-11-25 03:52:53 -07:00
end
2016-11-25 14:31:39 -07:00
if not love.keyboard.isDown("right", "left", "up", "down") then
player.current_speed = 0
end
2016-11-25 03:52:53 -07:00
end
function love.draw()
--draw the rectangle. Fill/line for the type,
--(x,y, width, height)
love.graphics.rectangle("line", player.x, player.y, player.w, player.h)
end