Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local term = require("term")
- local computer = require("computer")
- local gpu = component.gpu
- gpu.setResolution(150,50)
- term.clear()
- local W,H = gpu.getResolution()
- local FLOOR = H-4
- -------------------------------------------------
- local ladders = {
- {x=40, y1=15, y2=FLOOR},
- {x=75, y1=20, y2=FLOOR},
- {x=110,y1=15, y2=FLOOR}
- }
- local dkArt = {
- " ███████████ ",
- " ███ DK ███",
- " ███ ███████ ███",
- "███ ██ ██ ██ ███",
- "███ ██ ██ ███",
- " ███ ██████ ███",
- " ███████████"
- }
- -------------------------------------------------
- local function center(y,t)
- gpu.set(math.floor(W/2-#t/2),y,t)
- end
- -------------------------------------------------
- -- MENU
- -------------------------------------------------
- local function menu()
- term.clear()
- center(15,"DONKEY KONG - OpenComputers")
- center(18,"A/D move W/S ladders")
- center(19,"W or SPACE jump")
- center(20,"Q quit")
- center(23,"Press ENTER")
- while true do
- local _,_,_,k=event.pull("key_down")
- if k==28 then return end
- if k==16 then os.exit() end
- end
- end
- -------------------------------------------------
- local player,barrels,score,lives,spawnTimer
- local gravity=0.15
- local jumpPower=-2.2
- local function reset()
- player={x=10,y=FLOOR,vy=0,onGround=true,onLadder=false}
- barrels={}
- score=0
- lives=3
- spawnTimer=0
- end
- -------------------------------------------------
- local function onLadder(px,py)
- for _,l in ipairs(ladders) do
- if math.abs(px-l.x)<1 and py>=l.y1 and py<=l.y2 then
- return true
- end
- end
- return false
- end
- -------------------------------------------------
- local function draw()
- term.clear()
- -- DK
- for i=1,#dkArt do
- gpu.set(math.floor(W/2-#dkArt[i]/2),3+i,dkArt[i])
- end
- -- ladders
- for _,l in ipairs(ladders) do
- for y=l.y1,l.y2 do
- gpu.set(l.x,y,"#")
- end
- end
- -- floor
- for x=1,W do gpu.set(x,FLOOR+1,"=") end
- -- player
- gpu.set(math.floor(player.x),math.floor(player.y),"@")
- -- barrels
- for _,b in ipairs(barrels) do
- gpu.set(math.floor(b.x),math.floor(b.y),"O")
- end
- gpu.set(2,2,"Score: "..score)
- gpu.set(2,3,"Lives: "..lives)
- end
- -------------------------------------------------
- local function spawnBarrel()
- table.insert(barrels,{x=W/2,y=12,vy=0,passed=false})
- end
- local function hit(a,b)
- return math.abs(a.x-b.x)<1 and math.abs(a.y-b.y)<1
- end
- -------------------------------------------------
- local function gameOver()
- term.clear()
- center(22,"GAME OVER")
- center(24,"Score: "..score)
- center(27,"Press ENTER")
- while true do
- local _,_,_,k=event.pull("key_down")
- if k==28 then return end
- end
- end
- -------------------------------------------------
- while true do
- menu()
- reset()
- local last=computer.uptime()
- while lives>0 do
- local now=computer.uptime()
- local dt=now-last
- last=now
- local e={event.pull(0)}
- if e[1]=="key_down" then
- local k=e[4]
- if k==16 then os.exit() end
- if k==30 then player.x=math.max(2,player.x-1) end
- if k==32 then player.x=math.min(W-2,player.x+1) end
- player.onLadder=onLadder(player.x,player.y)
- if k==17 then -- W
- if player.onLadder then player.y=player.y-1
- elseif player.onGround then player.vy=jumpPower end
- end
- if k==31 and player.onLadder then player.y=player.y+1 end -- S
- if k==57 and not player.onLadder then -- SPACE
- if player.onGround then player.vy=jumpPower end
- end
- end
- if not player.onLadder then
- player.vy=player.vy+gravity
- player.y=player.y+player.vy
- end
- if player.y>=FLOOR then
- player.y=FLOOR
- player.vy=0
- player.onGround=true
- else
- player.onGround=false
- end
- spawnTimer=spawnTimer+dt
- if spawnTimer>1.4 then
- spawnTimer=0
- spawnBarrel()
- end
- for i=#barrels,1,-1 do
- local b=barrels[i]
- b.vy=b.vy+gravity
- b.y=b.y+b.vy
- b.x=b.x-0.35
- if b.y>=FLOOR then b.y=FLOOR b.vy=0 end
- if not b.passed and b.x<player.x then
- b.passed=true
- score=score+100
- end
- if hit(player,b) then
- lives=lives-1
- player.x=10 player.y=FLOOR player.vy=0
- table.remove(barrels,i)
- break
- end
- if b.x<1 then table.remove(barrels,i) end
- end
- draw()
- os.sleep(0.03)
- end
- gameOver()
- end
Advertisement
Add Comment
Please, Sign In to add comment