Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not term.isColor() or not term.isColor then
- error("This game requires an advanced computer!")
- end
- local running = true
- local maintimer = os.startTimer(0.09)
- local randomtimer = 0
- local platforms = {}
- local gamestate = 0
- local scroll = 0
- local playingarea_y1 = 4
- local playingarea_y2 = 19 - 4
- local player_x = 1
- local player_y = 1
- local player_gravity = 0
- local canchange = true
- local meters = 0
- local width, height = term.getSize()
- function point_to_aabb(x, y, x2, y2, w2, h2)
- return (
- x >= x2 and
- x <= x2 + w2 and
- y >= y2 and
- y <= y2 + h2
- )
- end
- function _vertical_line_aabb(x, y1, y2, xb1, yb1, xbw, ybh)
- if y1 > y2 then
- return (
- x >= xb1 and
- x <= xb1 + xbw and
- y1 >= yb1 and
- y2 <= yb1 + ybh
- )
- else
- return (
- x >= xb1 and
- x <= xb1 + xbw and
- y2 >= yb1 and
- y1 <= yb1 + ybh
- )
- end
- end
- function vertical_line_aabb(x, y1, y2)
- for _, v in pairs(platforms) do
- if (_vertical_line_aabb(x, y1, y2, v.x, v.y, v.width, v.height)) then
- return true
- end
- end
- return false
- end
- function writeout(x, y, text, color)
- term.setCursorPos(x, y)
- term.setTextColor(color)
- term.write(text)
- end
- function createplatform(xx, yy, w, h)
- local platform = {x = xx; y = yy; width = w; height = h;}
- platform.color = 2 ^ math.random(1, 14)
- if platform.color == colors.lightBlue then
- while platform.color == colors.lightBlue do
- platform.color = 2 ^ math.random(1, 14)
- end
- end
- table.insert(platforms, platform)
- end
- function drawplayer()
- term.setCursorPos(player_x - scroll, player_y + playingarea_y1 - 1)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.write((player_gravity == 0) and "v" or "^")
- end
- function moveplayer()
- player_x = player_x + 1
- scroll = scroll + 1
- if player_gravity == 0 then
- local down = false
- for _, v in pairs(platforms) do
- if point_to_aabb(player_x, player_y+1, v.x, v.y, v.width, v.height) then
- down = true
- break;
- end
- end
- player_y = player_y + ((down) and 0 or 1)
- for _, v in pairs(platforms) do
- if point_to_aabb(player_x, player_y + 1, v.x, v.y, v.width, v.height) or point_to_aabb(player_x, player_y, v.x, v.y, v.width, v.height) then
- player_y = v.y - 1
- canchange = true
- break;
- end
- end
- else
- local up = false
- for _, v in pairs(platforms) do
- if point_to_aabb(player_x, player_y, v.x, v.y, v.width, v.height) then
- up = true
- break;
- end
- end
- player_y = player_y - ((up) and 0 or 1)
- for _, v in pairs(platforms) do
- if point_to_aabb(player_x, player_y + 1, v.x, v.y, v.width, v.height) or point_to_aabb(player_x, player_y - 1, v.x, v.y, v.width, v.height) then
- player_y = v.y + 1
- canchange = true
- break;
- end
- end
- end
- end
- function drawplatform(platform)
- paintutils.drawFilledBox(platform.x - scroll, platform.y + playingarea_y1 - 1, platform.x + platform.width - scroll, platform.y + platform.height + playingarea_y1 - 2, platform.color)
- end
- function drawplatforms()
- for _, v in pairs(platforms) do
- drawplatform(v)
- end
- end
- local levelwidth = 51
- local _random_dir = 0
- local last_random_platform_x = 20
- local last_random_platform_y = 8
- local randomy = 0
- function randomplatform()
- last_random_platform_y = randomy
- randomy = math.random(2, 10)
- while math.abs(randomy - last_random_platform_y) < 4 do
- randomy = math.random(2, 10)
- end
- createplatform(
- last_random_platform_x,
- randomy,
- math.random(4, 16),
- 1
- )
- last_random_platform_x = platforms[#platforms].x + platforms[#platforms].width + math.random(0, 3)
- _random_dir = (_random_dir == 0) and 1 or 0
- end
- function initgame()
- meters = 0
- _random_dir = 0
- last_random_platform_x = 20
- term.clear()
- platforms = {}
- createplatform(4, 8, 16, 1)
- player_y = platforms[1].y - 1
- player_x = platforms[1].x + 1
- for i = 1, 6 do
- randomplatform()
- end
- player_gravity = 0
- end
- function initgamestart()
- meters = 0
- term.clear()
- player_y = platforms[1].y - 1
- player_x = platforms[1].x + 1
- player_gravity = 0
- end
- function updategame()
- if gamestate == 1 then
- moveplayer()
- meters = meters + 1
- if player_y > (playingarea_y2 - playingarea_y1)+2 or player_y < 0 then gamestate = 2 end
- randomtimer = randomtimer + 0.2
- if randomtimer >= 1 then
- randomplatform()
- randomtimer = 0
- end
- end
- end
- function drawgame()
- if gamestate == 0 then
- local title1 = "Grav"
- local title2 = "Jump"
- local start = "Press <SPACE> to start!"
- local quitstr = "Press <Q> to quit."
- paintutils.drawFilledBox(1, playingarea_y1 + 1, width, playingarea_y2 - 1, colors.lightBlue)
- paintutils.drawFilledBox(1, 1, width, playingarea_y1, colors.gray)
- paintutils.drawFilledBox(1, playingarea_y2, width, height, colors.gray)
- drawplatforms()
- drawplayer()
- term.setBackgroundColor(colors.gray)
- writeout(math.floor(width / 2) + 1, 2, title2, colors.red)
- writeout(math.floor(width / 2) - #title2, 2, title1, colors.lime)
- writeout(math.floor(width / 2) - math.floor(#quitstr / 2), height - 2, quitstr, colors.white)
- writeout(math.floor(width / 2) - math.floor(#start / 2), height - 3, start, colors.white)
- elseif gamestate == 1 then
- paintutils.drawFilledBox(1, playingarea_y1 + 1, width, playingarea_y2 - 1, colors.lightBlue)
- drawplatforms()
- drawplayer()
- paintutils.drawFilledBox(1, 1, width, playingarea_y1, colors.gray)
- paintutils.drawFilledBox(1, playingarea_y2, width, height, colors.gray)
- writeout(math.floor(width / 2) - math.ceil(#tostring(meters)/2), 2, tostring(meters), colors.white)
- elseif gamestate == 2 then
- local gameover = "You died!"
- local tryagain = "Try again?"
- local restart = "Press <SPACE> to restart!"
- local quit = "Press <Q> to quit."
- paintutils.drawFilledBox(1, playingarea_y1 + 1, width, playingarea_y2 - 1, colors.lightBlue)
- drawplatforms()
- term.setBackgroundColor(colors.lightBlue)
- writeout(math.ceil(width / 2) - math.ceil(#gameover / 2), math.floor(height / 2) - 2, gameover, colors.red)
- writeout(math.floor(width / 2) - math.floor(#tryagain / 2), math.floor(height / 2), tryagain, colors.red)
- writeout(math.floor(width / 2) - math.floor(#restart / 2), math.floor(height / 2) + 2, restart, colors.black)
- writeout(math.floor(width / 2) - math.floor(#quit / 2), math.floor(height / 2) + 3, quit, colors.black)
- paintutils.drawFilledBox(1, 1, width, playingarea_y1, colors.gray)
- paintutils.drawFilledBox(1, playingarea_y2, width, height, colors.gray)
- writeout(math.floor(width / 2) - math.ceil(#tostring(meters)/2), 2, tostring(meters), colors.white)
- end
- end
- function input(e)
- if gamestate == 0 then
- if e[1] == "key" then
- if e[2] == keys.space then
- scroll = 0
- initgamestart()
- gamestate = 1
- paintutils.drawFilledBox(1, playingarea_y1 + 1, width, playingarea_y2 - 1, colors.lightBlue)
- elseif e[2] == keys.q then
- running = false
- end
- end
- elseif gamestate == 1 then
- if e[1] == "key" then
- if e[2] == keys.space and (vertical_line_aabb(player_x, player_y, 16 * ((player_gravity == 0) and 1 or -1)) or vertical_line_aabb(player_x - 1, player_y, 16 * ((player_gravity == 0) and 1 or -1)) or vertical_line_aabb(player_x + 1, player_y, 16 * ((player_gravity == 0) and 1 or -1))) then
- if canchange then
- player_gravity = (player_gravity == 0) and 1 or 0
- canchange = false
- end
- end
- end
- elseif gamestate == 2 then
- if e[1] == "key" then
- if e[2] == keys.space then
- scroll = 0
- initgame()
- gamestate = 1
- paintutils.drawFilledBox(1, playingarea_y1 + 1, width, playingarea_y2 - 1, colors.lightBlue)
- math.randomseed(math.ceil(os.time() * 10))
- end
- end
- end
- end
- local mon = window.create(term.current(), 1, 1, width, height)
- local oldterm = term.redirect(mon)
- initgame()
- while running do
- local e = {os.pullEvent()}
- input(e)
- if e[1] == "timer" then
- if e[2] == maintimer then
- updategame()
- mon.setVisible(true)
- mon.setVisible(false)
- drawgame()
- maintimer = os.startTimer(0.09)
- end
- elseif e[1] == "key" then
- if gamestate == 2 then
- if e[2] == keys.q then running = false; term.setCursorPos(1, 1); term.setBackgroundColor(colors.black); term.clear() end
- end
- end
- end
- term.redirect(oldterm)
- local msg = "Thank you for playing GravJump!"
- term.setBackgroundColor(colors.black)
- term.clear()
- if width < #msg then
- term.setCursorPos(1, 1)
- term.setTextColor(colors.blue)
- print(msg)
- else
- writeout(math.floor(width / 2) - math.floor(#msg / 2), 1, msg, colors.blue)
- term.setCursorPos(1, 2)
- end
Add Comment
Please, Sign In to add comment