Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local w, h = term.getSize()
- a = 0
- timer = os.startTimer(0)
- --variables
- snakecolor = colors.green
- nInterval = 0.3
- fruitcolor = colors.red
- textcolor = colors.yellow
- edgecolor = colors.blue
- HIGHSCORE = 0
- HIGHSCOREP = "NoOne"
- --functions
- function drawpixel(x, y, color)
- term.setCursorPos(x, y)
- term.setBackgroundColor(color)
- term.write(" ")
- term.setBackgroundColor(colors.black)
- end
- local function printCentred(y, s)
- local x = math.floor((w - string.len(s)) / 2)
- term.setCursorPos(x, y)
- --term.clearLine()
- term.write(s)
- end
- local function addwall()
- yes = 0
- while true do
- randomx = math.random(1, w)
- randomy = math.random(2, h)
- for i = 1, #sx do
- if sx[i] == randomx and sy[i] == randomy then
- yes = yes + 1
- end
- end
- for i = 1, #fruitx do
- if fruitx[i] == randomx and fruity[i] == randomy then
- yes = yes + 1
- end
- end
- if yes == 0 then
- break
- end
- end
- wallx[#wallx + 1] = randomx
- wally[#wally + 1] = randomy
- end
- local function addfruit(p1)
- yes = 0
- while true do
- randomx = math.random(1, w)
- randomy = math.random(2, h)
- for i = 1, #sx do
- if sx[i] == randomx and sy[i] == randomy then
- yes = yes + 1
- end
- end
- if yes == 0 then
- break
- end
- end
- fruitx[p1] = randomx
- fruity[p1] = randomy
- end
- function drawline(xc, yc, xv, yv, color)
- if xc == xv then
- if yc < yv then
- for i = 1, yv - yc + 1 do
- drawpixel(xc, yc + i - 1, color)
- end
- else
- for i = 1, yc - yv + 1 do
- drawpixel(xc, yc - i + 1, color)
- end
- end
- elseif yc == yv then
- if xc < xv then
- for i = 1, xv - xc + 1 do
- drawpixel(xc + i - 1, yc, color)
- end
- else
- for i = 1, xc - xv + 1 do
- drawpixel(xc - i + 1, yc, color)
- end
- end
- end
- end
- function edge()
- drawline(1, 1, 1, h, edgecolor)
- drawline(1, 1, w, 1, edgecolor)
- drawline(w, 1, w, h, colors.blue)
- drawline(1, h, w, h, edgecolor)
- end
- function drawgame()
- term.clear()
- for i = 1, #sx do
- term.setCursorPos(sx[i], sy[i])
- term.setBackgroundColor(snakecolor)
- term.write(" ")
- term.setBackgroundColor(colors.black)
- end
- for i = 1, #fruitx do
- term.setCursorPos(fruitx[i], fruity[i])
- term.setBackgroundColor(fruitcolor)
- term.write(" ")
- end
- for i = 1, #wallx do
- term.setCursorPos(wallx[i], wally[i])
- term.setBackgroundColor(edgecolor)
- term.write(" ")
- end
- term.setBackgroundColor(colors.black)
- if diffeculty >= 2 then
- edge()
- end
- drawline(1,1,w,1,edgecolor)
- term.setBackgroundColor(edgecolor)
- term.setCursorPos(1, 1)
- term.write("score:" .. score*(diffeculty+1))
- term.write(" ".."HIGHSCORE:"..HIGHSCORE.." Holder:\""..HIGHSCOREP.."\"")
- term.setCursorPos(w-string.len("Difficulty:" ..diffeculty), 1)
- term.write("Difficulty:" ..diffeculty)
- term.setBackgroundColor(colors.black)
- end
- function menu(list,clears,where)
- selected = 1
- if not clears then term.clear() end
- term.setTextColor(textcolor)
- for i=1,#list do
- if selected == i then
- printCentred(h/2 - #list/2 + (i-1), "["..list[i].."]")
- else
- printCentred(h/2 - #list/2 + (i-1), list[i])
- end
- end
- if where == 1 then printCentred(1,"press BackSpace (<----) to puase") end
- while true do
- local event, o1, o2 = os.pullEvent("key")
- term.clear()
- if clears then drawgame() end
- if where == 1 then printCentred(1,"press BackSpace (<----) to puase") end
- if o1 == keys.up then
- selected = selected + -1
- end
- if o1 == keys.down then
- selected = selected - -1
- end
- if selected > #list then
- selected = 1
- end
- if selected < 1 then
- selected = #list
- end
- if o1 == keys.enter then
- break
- end
- for i=1,#list do
- if selected == i then
- printCentred(h/2 - #list/2 + (i-1), "["..list[i].."]")
- else
- printCentred(h/2 - #list/2 + (i-1), list[i])
- end
- end
- end
- return selected
- end
- function start()
- --menu
- diffeculty = menu({"easy","normal","HARD","VERY HARD!","IMPOSIBLE!","Leave"},false,1) - 1
- --game
- nInterval = 0.3
- wallx = {}
- wally = {}
- sx = {6, 5, 4}
- sy = {5, 5, 5}
- dx = 1
- dy = 0
- die = false
- fb = 0
- die = false
- if diffeculty > 0 then
- fb = 0.2
- end
- if diffeculty >= 3 then
- fb = 0.4
- end
- fruitx = {math.floor(w / 2)}
- fruity = {math.floor(h / 2)}
- nInterval = nInterval - fb
- local timer = os.startTimer(nInterval)
- move = 0
- if diffeculty ~= 5 then
- while true do
- event, o1, o2 = os.pullEvent()
- if event == "timer" and o1 == timer then
- timer = os.startTimer(nInterval)
- end
- if event == "key" then
- if o1 == keys.right and dx == 0 and move == 0 then
- dx = 1
- dy = 0
- move = 1
- end
- if o1 == keys.left and dx == 0 and move == 0 then
- dx = -1
- dy = 0
- move = 1
- end
- if o1 == keys.up and dy == 0 and move == 0 then
- dx = 0
- dy = -1
- move = 1
- end
- if o1 == keys.down and dy == 0 and move == 0 then
- dx = 0
- dy = 1
- move = 1
- end
- if o1 == keys.backspace then
- if menu({"Resume","Difficulty Select"},true) == 2 then
- break
- end
- timer = os.startTimer(nInterval)
- end
- if o1 == keys.space then
- addfruit(#fruitx + 1)
- end
- end
- if event == "timer" then
- move = 0
- table.insert(sx, 1, sx[1] + dx)
- table.insert(sy, 1, sy[1] + dy)
- for i = 1, #sx - 1 do
- if sx[1] == sx[i + 1] and sy[1] == sy[i + 1] then
- die = true
- end
- end
- for i = 1, #wallx do
- if sx[1] == wallx[i] and sy[1] == wally[i] then
- die = true
- end
- end
- for i = 1, #fruitx do
- if fruitx[i] == sx[1] and sy[1] == fruity[i] then
- addfruit(i)
- if diffeculty == 4 then addwall() end
- grow = true
- end
- end
- if #fruitx == 0 then
- addfruit(1)
- end
- if not grow then
- sx[#sx] = nil
- sy[#sy] = nil
- end
- grow = false
- if diffeculty < 2 then
- if sx[1] > w then
- sx[1] = 1
- end
- if sx[1] < 1 then
- sx[1] = w
- end
- if sy[1] > h then
- sy[1] = 2
- end
- if sy[1] < 2 then
- sy[1] = h
- end
- else
- if sx[1] > w - 1 then
- die = true
- end
- if sx[1] < 2 then
- die = true
- end
- if sy[1] > h - 1 then
- die = true
- end
- if sy[1] < 2 then
- die = true
- end
- end
- score = #sx -3
- drawgame()
- if score*(diffeculty+1) > HIGHSCORE then
- HIGHSCOREP = "you"
- HIGHSCORE = score*(diffeculty+1)
- end
- if die then
- score = score*(diffeculty+1)
- term.clear()
- printCentred(math.floor(h / 2) - 1, "you died")
- printCentred(math.floor(h / 2), "score:" .. score)
- if HIGHSCOREP == "you" then
- HIGHSCOREP = "__"
- printCentred(math.floor(h / 2) + 1, "NEW HIGH SCORE!")
- printCentred(math.floor(h / 2) + 2, "EnterName:"..HIGHSCOREP)
- shift = 0
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.leftShift or key == keys.rightShift then
- shift = 1 - shift
- end
- kes = keys.getName(key)
- if shift == 1 then kes = string.upper(kes) end
- if #kes == 1 and string.sub(HIGHSCOREP,1,1) ~= "_" then
- HIGHSCOREP = string.sub(HIGHSCOREP,1,1)
- HIGHSCOREP = HIGHSCOREP..kes
- end
- if #kes == 1 and string.sub(HIGHSCOREP,1,1) == "_" then
- HIGHSCOREP = kes.."_"
- end
- if key == keys.backspace and string.sub(HIGHSCOREP,2,2) == "_" then
- HIGHSCOREP = "__"
- end
- if key == keys.backspace and string.sub(HIGHSCOREP,2,2) ~= "_" then
- HIGHSCOREP = string.sub(HIGHSCOREP,1,1)
- HIGHSCOREP = HIGHSCOREP.."_"
- end
- if key == keys.enter and string.sub(HIGHSCOREP,2,2) ~= "_" then
- break
- end
- printCentred(math.floor(h / 2) + 2, "EnterName:"..HIGHSCOREP)
- end
- printCentred(math.floor(h / 2) + 1, "NEW HIGH SCORE!")
- printCentred(math.floor(h / 2) + 2, "press ENTER to play agian")
- else
- printCentred(math.floor(h / 2) + 1, "press ENTER to play agian")
- end
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.enter then
- break
- end
- end
- break
- end
- end
- end
- start()
- end
- term.clear()
- term.setCursorPos(1,1)
- end
- start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement