Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local debug = false
- local oLog = fs.open("console", "w")
- local function log(string)
- if debug then
- local string = string or ""
- oLog.writeLine(string)
- oLog.flush()
- end
- end
- local tArgs = {...}
- local type, prevtype = 1, 1
- local sChanged = true
- local tickToggle = true
- local tickNum = 0
- local distance = 0
- local theme = "default"
- local w, h = term.getSize()
- local roadString = " " .. string.rep("- ", w/2)
- local waterString = " " .. string.rep(" ~", w/2)
- local midW, midH = math.floor(w/2), math.floor(h/2)
- local timers = {
- ["gametick"] = os.startTimer(.05),
- ["screenscroll"] = os.startTimer(2)
- }
- local pos = {
- ["x"] = midW,
- ["y"] = 5
- }
- local themes = {
- --["theme name"] = {grass = grass color, tree = {leaves, trunk}, road = road color, dashes = color of road dashes, car = {any colors that cars could be}, water = water color, waves = color of tildes in water, log = log color, player = {color of square, color of "O"}},
- ["default"] = {grass = colors.lime, tree = {colors.green, colors.brown}, road = colors.gray, dashes = colors.yellow, car = {2^0, 2^2, 2^3, 2^4, 2^6, 2^8, 2^9, 2^10, 2^14, 2^15}, water = colors.blue, waves = colors.lightBlue, log = colors.brown, player = {colors.orange, colors.black}},
- ["hell"] = {grass = colors.red, tree = {_, colors.gray}, road = colors.gray, dashes = colors.magenta, car = {colors.black}, water = colors.orange, waves = colors.yellow, log = colors.lightGray, player = {colors.white, colors.black}},
- ["snow"] = {grass = colors.white, tree = {_, colors.green}, road = colors.gray, dashes = colors.yellow, car = {colors.red, colors.green}, water = colors.lightBlue, waves = colors.white, log = colors.brown, player = {colors.green, colors.red}},
- ["fallout"] = {grass = colors.brown, tree = {_, colors.black}, road = colors.gray, dashes = colors.lightGray, car = {colors.black, colors.magenta, colors.pink}, water = colors.lime, log = colors.green, waves = colors.green, player = {colors.red, colors.black}},
- ["space"] = {grass = colors.black, tree = {_, colors.white}, road = colors.black, dashes = colors.yellow, car = {colors.brown, colors.white, colors.gray, colors.lightGray}, water = colors.purple, waves = colors.magenta, log = colors.black, player = {colors.black, colors.yellow}},
- ["sky"] = {grass = colors.white, tree = {colors.lightGray, colors.gray}, road = colors.yellow, dashes = colors.yellow, car = {colors.orange}, water = colors.lightBlue, log = colors.white, waves = colors.lightBlue, player = {colors.white, colors.yellow}},
- ["candy"] = {grass = colors.magenta, tree = {colors.pink, colors.red}, road = colors.red, dashes = colors.magenta, car = {colors.orange, colors.purple, colors.blue}, water = colors.pink, waves = colors.magenta, log = colors.yellow, player = {colors.yellow, colors.pink}},
- ["cave"] = {grass = colors.lightGray, tree = {colors.gray, colors.black}, road = colors.gray, dashes = colors.lightGray, car = {colors.green, colors.lime, colors.white, colors.black}, water = colors.orange, waves = colors.yellow, log = colors.red, player = {colors.blue, colors.lime}},
- ["end"] = {grass = colors.yellow, tree = {colors.black, colors.purple}, road = colors.white, dashes = colors.purple, car = {colors.purple, colors.magenta, colors.black}, water = colors.black, waves = colors.lightBlue, log = colors.purple, player = {colors.black, colors.purple}},
- ["village"] = {grass = colors.lime, tree = {colors.brown, colors.yellow}, road = colors.lightGray, dashes = colors.gray, car = {colors.brown, colors.green, colors.purple, colors.white, colors.gray}, water = colors.lightBlue, waves = colors.blue, log = colors.brown, player = {colors.blue, colors.white}}
- }
- if themes[tArgs[1]] then
- theme = tArgs[1]
- end
- local player = {
- ["isAlive"] = true,
- ["onWater"] = false,
- ["onCar"] = false,
- ["onLog"] = false
- }
- local map = {"grass", "grass", "grass", "grass", "grass"}
- local mapDirs = {false, false, false, false, false}
- local mapItems = {} --# = tree colornum = car or log
- for i = 1, 5 do
- mapItems[#mapItems + 1] = {}
- for n = 1, w do
- local choice = math.random(1, 4)
- if choice == 1 and not (i == 5 and n == midW) then
- mapItems[#mapItems][n] = "#"
- end
- end
- end
- local function resetTimers()
- timers = {
- ["gametick"] = os.startTimer(.05),
- ["screenscroll"] = os.startTimer(2)
- }
- end
- local function draw(string, xPos, yPos, txtcol, bakcol)
- local string = string or ""
- local txtcol = txtcol or colors.white
- local bakcol = bakcol or colors.black
- term.setCursorPos(xPos, yPos)
- term.setBackgroundColor(bakcol)
- term.setTextColor(txtcol)
- term.write(string)
- end
- local function generateNewChunk()
- repeat
- type = math.random(1, 3)
- until type ~= prevtype
- local length = math.random(1, 5)
- prevtype = type
- if type == 1 then type = "grass" end
- if type == 2 then type = "road" end
- if type == 3 then type = "water" end
- local currentLen = #map
- for i = 1, length do
- map[#map+1] = type
- mapItems[#mapItems + 1] = {}
- mapDirs[#mapDirs + 1] = false
- if type == "grass" then
- for n = 1, w do
- local choice = math.random(1, 4)
- if choice == 1 then
- mapItems[#mapItems][n] = "#"
- else
- mapItems[#mapItems][n] = false
- end
- end
- elseif type == "road" then
- local choice = math.random(0, 1)
- if choice == 0 then choice = "left" else choice = "right" end
- mapDirs[#mapDirs] = choice
- mapItems[#mapItems] = {}
- for n = 1, w do
- local choice = math.random(1, 4)
- if choice == 1 then
- mapItems[#mapItems][n] = themes[theme].car[math.random(1, #themes[theme].car)]
- else
- mapItems[#mapItems][n] = false
- end
- end
- elseif type == "water" then
- local choice = math.random(0, 1)
- if choice == 0 then choice = "left" else choice = "right" end
- mapDirs[#mapDirs] = choice
- mapItems[#mapItems] = {}
- for n = 1, w do
- local choice = math.random(1, 4)
- if choice == 1 then
- mapItems[#mapItems][n] = 128
- else
- mapItems[#mapItems][n] = false
- end
- end
- end
- log(type)
- end
- return length
- end
- local function initialMapGen()
- local genNum = 5
- while genNum < h + 3 do
- genNum = genNum + generateNewChunk()
- end
- end
- local function moveScreenUp()
- sChanged = true
- table.remove(map, 1)
- table.remove(mapItems, 1)
- table.remove(mapDirs, 1)
- pos.y = pos.y - 1
- end
- local function checkCollision(dir)
- if dir == 1 then xOff, yOff = 0, 1 end
- if dir == 2 then xOff, yOff = 1, 0 end
- if dir == 3 then xOff, yOff = 0, -1 end
- if dir == 4 then xOff, yOff = -1, 0 end
- if mapItems[pos.y + yOff][pos.x + xOff] and mapItems[pos.y + yOff][pos.x + xOff] == "#" then
- return false
- end
- return true
- end
- local function eventHandler(events)
- sChanged = true
- if events[2] == keys.up and checkCollision(1) then
- if pos.y == 5 then
- moveScreenUp()
- timers.screenscroll = os.startTimer(2)
- end
- pos.y = pos.y + 1
- distance = distance + 1
- elseif events[2] == keys.down and (pos.y == 1 or checkCollision(3)) then
- pos.y = pos.y - 1
- distance = distance - 1
- elseif events[2] == keys.left and pos.x > 1 and checkCollision(4) then
- pos.x = pos.x - 1
- elseif events[2] == keys.right and pos.x < w and checkCollision(2) then
- pos.x = pos.x + 1
- end
- end
- local function updateGame()
- --== player stuff ==--
- if map[pos.y] == "water" then
- if mapItems[pos.y][pos.x] then
- player.onWater = false
- player.onLog = true
- else
- player.onWater = true
- player.onLog = false
- end
- else
- player.onWater = false
- player.onLog = false
- end
- if player.onLog and tickToggle and player.isAlive then
- if mapDirs[pos.y] == "right" and pos.x < w then pos.x = pos.x + 1 end
- if mapDirs[pos.y] == "left" and pos.x > 1 then pos.x = pos.x - 1 end
- end
- if map[pos.y] == "road" and mapItems[pos.y][pos.x] then
- player.onCar = true
- else
- player.onCar = false
- end
- if pos.y == 0 or player.onCar or player.onWater then
- player.isAlive = false
- end
- if not map[22] then
- generateNewChunk()
- end
- --== cars and logs ==--
- if tickToggle then
- for i = 1, h do
- local choice
- if map[i] == "road" then
- choice = math.random(1, 10)
- elseif map[i] == "water" then
- choice = math.random(1, 2)
- end
- if map[i] == "road" or map[i] == "water" then
- if mapDirs[i] == "left" then
- for n = 1, w - 1 do
- mapItems[i][n] = mapItems[i][n + 1]
- end
- if mapItems[i][w - 1] then
- local chance = map[i] == "road" and math.random(0, 2) or math.random(1, 3)
- mapItems[i][w] = chance > 1 and mapItems[i][w - 1]
- elseif choice == 1 then
- mapItems[i][w] = themes[theme].car[math.random(1, #themes[theme].car)]
- else
- mapItems[i][w] = false
- end
- elseif mapDirs[i] == "right" then
- for n = w, 2, -1 do
- mapItems[i][n] = mapItems[i][n - 1]
- end
- if mapItems[i][2] then
- local chance = map[i] == "road" and math.random(0, 2) or math.random(1, 3)
- mapItems[i][1] = chance > 1 and mapItems[i][2]
- elseif choice == 1 then
- mapItems[i][1] = themes[theme].car[math.random(1, #themes[theme].car)]
- else
- mapItems[i][1] = false
- end
- end
- end
- end
- tickToggle = false
- else
- tickToggle = true
- end
- --== stuff done differently once dead ==--
- if not player.isAlive and not tickToggle and map[pos.y] == "water" then
- if mapDirs[pos.y] == "right" and pos.x < w + 1 then pos.x = pos.x + 1 end
- if mapDirs[pos.y] == "left" and pos.x > 0 then pos.x = pos.x - 1 end
- end
- end
- local function drawPlayer()
- term.setCursorPos(pos.x, h - pos.y + 1)
- if player.isAlive then
- term.setBackgroundColor(themes[theme].player[1])
- term.setTextColor(themes[theme].player[2])
- term.write("O")
- else
- term.setBackgroundColor(themes[theme][map[pos.y]])
- term.setTextColor(themes[theme].player[1])
- term.write("X")
- end
- end
- local function drawInfo()
- draw("Score", w-4, 1, themes[theme].player[2], themes[theme].player[1])
- draw(" ", w-4, 2, themes[theme].player[2], themes[theme].player[1])
- draw(tostring(distance), w-4, 2, themes[theme].player[2], themes[theme].player[1])
- end
- local function drawDeath()
- term.setBackgroundColor(colors.red)
- term.setCursorPos(1, midH)
- term.clearLine()
- term.setCursorPos(1, midH + 1)
- term.clearLine()
- draw("You Died", (w/2) - 3, midH, colors.white, colors.red)
- draw("Score: " .. tostring(distance), (w/2) - (7 + #tostring(distance))/2 + 1, midH + 1, colors.white, colors.red)
- end
- local function updateScreen()
- local start
- for i = 1, h do
- term.setBackgroundColor(themes[theme][map[i]])
- term.setCursorPos(1, h - i + 1)
- if map[i] == "road" then
- term.setTextColor(themes[theme].dashes)
- term.write(roadString)
- --term.blit(roadstring, string.rep("2", w), string.rep("0", w)) --change 2 and 0 to texturepack controlled variables
- elseif map[i] == "water" then
- term.setTextColor(themes[theme].waves)
- term.write(waterString)
- else
- term.clearLine()
- end
- if i == pos.y and not player.isAlive and map[pos.y] == "road" then
- drawPlayer()
- start = true
- end
- if map[i] == "grass" then
- term.setBackgroundColor(themes[theme].tree[1] or themes[theme].grass)
- term.setTextColor(themes[theme].tree[2])
- for k, v in pairs(mapItems[i]) do
- if mapItems[i][k] == "#" then
- term.setCursorPos(k, h - i + 1)
- term.write("#")
- end
- end
- elseif map[i] == "road" then
- for k, e in pairs(mapItems[i]) do
- if tonumber(mapItems[i][k]) then
- term.setCursorPos(k, h - i + 1)
- term.setBackgroundColor(mapItems[i][k])
- term.write(" ")
- end
- end
- elseif map[i] == "water" then
- for k, e in pairs(mapItems[i]) do
- if tonumber(mapItems[i][k]) then
- term.setCursorPos(k, h - i + 1)
- term.setBackgroundColor(themes[theme].log)
- term.write(" ")
- end
- end
- end
- if i == pos.y and not start then
- drawPlayer()
- end
- end
- if player.isAlive then
- drawInfo()
- else
- drawDeath()
- end
- end
- if tArgs[1] == "list" then
- print("")
- for i in pairs(themes) do
- write(i .. " ")
- end
- else
- initialMapGen()
- while player.isAlive do
- local events = {os.pullEvent()}
- if events[1] == "timer" then
- if events[2] == timers.gametick then
- sChanged = true
- updateGame()
- if sChanged then
- updateScreen()
- sChanged = false
- end
- timers.gametick = os.startTimer(.05)
- elseif events[2] == timers.screenscroll then
- moveScreenUp()
- timers.screenscroll = os.startTimer(2)
- end
- elseif events[1] == "key" then
- eventHandler(events)
- end
- end
- term.setBackgroundColor(colors.red)
- term.clear()
- sleep(.2)
- resetTimers()
- timers.deathTimer = os.startTimer(1)
- local exitable = false
- while true do
- local events = {os.pullEvent()}
- if events[1] == "timer" then
- if events[2] == timers.gametick then
- sChanged = true
- updateGame()
- if sChanged then
- updateScreen()
- sChanged = false
- end
- timers.gametick = os.startTimer(.05)
- end
- if events[2] == timers.deathTimer then
- exitable = true
- end
- elseif events[1] == "key" and exitable then
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- break
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement