--Speeder

local X = 24
local Y = 16
local gO=0
local rcX = 30
local rcY = 2
local score = 0
local rcarOn = 0
local localY = 4
local cMode = 128
local trafficCar = math.max(math.random(1,5),0)
local bootCONF = kernal.getTable(".boot/.conf")
local rDir = string.sub(bootCONF[2],string.find(bootCONF[2],":")+2)

if not fs.exists(rDir.."/.apps/games/speeder/top") then
kernal.fwrite(rDir.."/.apps/games/speeder/top", "0")
end

local topscore = tostring(kernal.getLine(rDir.."/.apps/games/speeder/top", 1))

local tprint = function(txt)
term.setBackgroundColor(cMode)
term.setTextColour(8)
write(txt)
end

local function redCar()
if score >= tonumber(topscore) then
trafficCar = math.max(math.random(1,5),0)
end
redcar = paintutils.loadImage(rDir.."/.apps/games/speeder/sprites/"..trafficCar..".nfp")
paintutils.drawImage(redcar,rcX ,rcY)
	if rcY >= 16 then 
	rcY = 2 
	rcarOn = 0 
	end
		if rcY <= 2 then rcY=2 end
		if rcX <= 17 then rcX=17 end
		if rcX >= 31 then rcX = 31 end
end

local function drawCar()
	if rcarOn == 0 then
		trafficCar = math.max(math.random(1,5),0)
		local carPos = math.max(math.random(18,30),0)
		rcX = carPos
		rcarOn = 1
end

if rcarOn == 1 then
	rcY = rcY + 1
	redCar()
end

end


term.setBackgroundColor(cMode)
term.clear()
local function road()
world = paintutils.loadImage(rDir.."/.apps/games/speeder/world/road.nfp")
paintutils.drawImage(world,1 ,1)

end


local function size()
if Y >= 16 then Y=16 end
if Y <= 3 then Y=3 end
if X <= 17 then X=17 end
if X >= 31 then X=31 end

end
road()
player = paintutils.loadImage(rDir.."/.apps/games/speeder/sprites/player.nfp")
paintutils.drawImage(player,X ,Y)

drawCar()


local gameOver = false

local function game()
while true do
		
if X >= rcX-4 and X <= rcX+4 and Y >= rcY-2 and Y <= rcY+2 then
		
		term.setCursorPos(5,4)
		tprint("GameOver ")
		term.setCursorPos(1,19)
			if score >= tonumber(topscore) then
				kernal.fwrite(rDir.."/.apps/games/speeder/top", score)
			end
		gameOver = true
		break
		
end
size()
road()
player = paintutils.loadImage(rDir.."/.apps/games/speeder/sprites/player.nfp")
paintutils.drawImage(player,X ,Y)
term.setCursorPos(5,5)
tprint("Score")
term.setCursorPos(5,6)
tprint(score)

local id,key = os.pullEvent()

		if id=="key" then
			if key==keys.left then
			X = X-1
			end
				if key==keys.right then
				X = X+1
				end
					if key==keys.enter then 
					break
					end
					if key==keys.up then
						Y=Y-1
						road()
						end
							if key==keys.down then
							Y=Y+1
							end
								



end				
end				
end
local function speed()
while not gameOver do

player = paintutils.loadImage(rDir.."/.apps/games/speeder/sprites/player.nfp")
paintutils.drawImage(player,X ,Y)
drawCar()
sleep(0.06)
score = score+0.05
term.setCursorPos(5,5)
tprint("Score")
term.setCursorPos(5,6)
tprint(score)

term.setCursorPos(41, 4)
tprint("Top Score")

term.setCursorPos(41, 5)
tprint(tonumber(topscore))

if score >= tonumber(topscore) then
topscore = score
end

end
end

local function para()
	term.setCursorPos(1,5)
	write("key to start")
local e = {os.pullEvent()}
if e[1] == "key" then
		sleep(0.5)
		term.clear()
while not gameOver do
parallel.waitForAny(game, speed)
end
end
end
para()

sleep(3)
os.queueEvent("terminate")			