--Vars
width, height = term.getSize()
i = 0
i_max = width*height
x = 1
y = 1
score = 0
tun_y = math.ceil(height/2)-4
tun_h = 7
s_time = 10
timer2 = 5+math.ceil(math.random(0,s_time))
dir = 0
timer3 = 400
lives = 3
--Highscore
if not fs.exists("tunnel_hs") then
local tmp=io.open("tunnel_hs", "w")
tmp:write("Name")
tmp:write(1)
tmp:close()
end
hs = io.open("tunnel_hs","r")
hs_name = hs:read("*l")
hs_score = hs:read("*l")
hs:close()
--Table
screen = {}
for a=1,width-1,1 do
screen[a] = {}
end
for a=1,width-1,1 do
for b=3,height-2,1 do
screen[a][b] = " "
end
end
--Functions
function drawPlayer()
term.setCursorPos(2,y)
write(">")
if y == 3 then
term.setCursorPos(2,y-1)
write("-")
end
if y == height-2 then
term.setCursorPos(2,y+1)
write("-")
end
end
function tunnel()
for a=1,width-2,1 do
for b=3,height-2,1 do
screen[a][b] = screen[a+1][b]
term.setCursorPos(a,B)
write(screen[a][b])
end
end
for a=1,height-2,1 do
screen[width-1][a] = "#"
if a >= tun_y and a <= tun_y+tun_h then screen[width-1][a] = " " end
term.setCursorPos(width-1,a)
if screen[a][width-1] then write(screen[a][width-1]) end
end
if timer2 == 0 then
dir = math.ceil(math.random(0,2))
if dir == 1 then tun_y = tun_y + 1 end
if dir == 2 then tun_y = tun_y - 1 end
if tun_y == 2 then tun_y = 3 end
if tun_y == height-1-tun_h then tun_y = height-2-tun_h end
timer2 = 5+math.ceil(math.random(0,s_time))
end
timer2 = timer2 - 1
if timer3 == 0 then
tun_h = tun_h - 1
timer3 = 160+tun_h*40
if tun_h == 1 then
tun_h = 2
s_time = s_time-1
if s_time == 0 then s_time = 1 end
end
end
timer3 = timer3 - 1
end
function gameover()
lives = lives-1
timer3 = 400
tun_h = 7
tun_y = math.ceil(height/2)-4
y = math.ceil(height/2)
s_time = 10
if lives == -1 then
term.clear()
term.setCursorPos(math.ceil(width/2)-5,math.ceil(height/2)-4)
write("Game Over!")
term.setCursorPos(math.ceil(width/2)-10,math.ceil(height/2))
write("Your score is: "..score)
term.setCursorPos(math.ceil(width/2)-13,math.ceil(height/2)+2)
if score > tonumber(hs_score) then
write("You have beaten \n "..hs_name.."'s highscore of "..hs_score.."!")
term.setCursorPos(math.ceil(width/2)-10,math.ceil(height/2)+5)
write("Please enter your name: ")
hs = io.open("tunnel_hs","w")
name = io.read()
hs:write(name.."\n")
hs:write(score.."\n")
hs:close()
else
write("Too bad, you didn't beat \n "..hs_name.."'s highscore of "..hs_score..".")
end
term.setCursorPos(1,height)
write("Press <Enter> to continue.")
read()
score = 0
else
term.setCursorPos(1,1)
write("Score: 0 ")
for a=1,width-1,1 do
for b=3,height-2,1 do
screen[a][b] = " "
term.setCursorPos(a,B)
write(screen[a][b])
end
end
end
end
function timerupdate()
tunnel()
timer1 = os.startTimer(.05)
end
--Intro
term.clear()
term.setCursorPos(1,1)
print("")
print("")
print(" X==---------------------------------==X")
print(" | CC Games: Tunnel, made by Chub1337! |")
print(" X==---------------------------------==X")
print("")
print("")
print("")
print(" Controls:")
print("")
print(" UP: ArrowKey-Up")
print(" DOWN: ArrowKey-Down")
print("")
print(" Current Highscore:")
print(" "..hs_name.." with "..hs_score)
print("")
print("")
write("Press <Enter> to continue.")
io.read()
term.clear()
term.setCursorPos(1,1)
--Screen
write("Score: "..score)
for i=1,width-1,1 do
term.setCursorPos(i,2)
write("-")
end
term.setCursorPos(1,height-1)
for i=1,width-1,1 do
term.setCursorPos(i,height-1)
write("-")
end
--Tunnel
x = 2
y = math.ceil(height/2)
timer1 = os.startTimer(.05)
while lives~=-1 do
event, par = os.pullEvent()
if event == "timer" and par == timer1 then timerupdate() end
if event == "key" then
if par == 200 then
y = y - 1
if y < 3 then
y = 3
end
term.setCursorPos(2,y+1)
write(" ")
elseif par == 208 then
y = y + 1
if y > height-2 then
y = height-2
end
term.setCursorPos(2,y-1)
write(" ")
end
end
drawPlayer()
score = score+1
term.setCursorPos(1,1)
write("Score: "..score)
term.setCursorPos(math.ceil(width/2),1)
write("Lives: "..lives)
term.setCursorPos(1,height)
--write("tun_h: "..tun_h.." s_time: "..s_time.." timer3: "..timer3.." ")
write("Highscore: "..hs_score.." by "..hs_name..".")
if screen[2][y] == "#" then
gameover()
end
end
Comments