Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local filename = 'extreme_snake_highscore.dat'
- if not fs.exists(filename) then
- local file = fs.open(filename,'w')
- file.write('0')
- file.close()
- end
- local file = fs.open(filename,'r')
- local hscore = file.readLine()
- if not tonumber(hscore) then
- hscore = 0
- local file = fs.open(filename,'w')
- file.write('0')
- file.close()
- end
- hscore = tonumber(hscore)
- local length = {}
- local fruit = {{13,6,false},{17,8,false}}
- local paused = false
- for num = 1,6 do
- table.insert(length,{(7-num)+6,6})
- end
- local speed = 1
- local size = 6
- local x, y = 12,6
- local direction = 0
- --Settings
- local color = colors.lime
- local color2 = colors.green
- --
- local timer = 0
- local dead = false
- local width, height = term.getSize()
- local score = -100
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(1,1)
- term.clearLine()
- local function pauseHandler()
- while true do
- local ev = {os.pullEvent('key')}
- if ev[2] == 28 then
- if paused then
- paused = false
- else
- paused = true
- term.setCursorPos((width/2)-4,height/2)
- term.setTextColor(colors.yellow)
- term.setBackgroundColor(colors.black)
- write('[PAUSED]')
- end
- end
- end
- end
- local function display()
- while true do
- repeat sleep(0.05) until paused == false
- term.setBackgroundColor(colors.black)
- for curheightc = 2,height do
- term.setCursorPos(1,curheightc)
- term.clearLine()
- end
- for i,v in pairs(fruit) do
- if v[3] then
- paintutils.drawPixel(v[1],v[2],colors.yellow)
- else
- paintutils.drawPixel(v[1],v[2],colors.red)
- end
- end
- for i,v in pairs(length) do
- if (i+1)%3 == 0 then
- paintutils.drawPixel(v[1],v[2],color2)
- else
- paintutils.drawPixel(v[1],v[2],color)
- end
- end
- end
- end
- local function lose()
- repeat sleep(0.05) until dead
- end
- local function update()
- while true do
- timer = timer + 1
- if timer%speed == 0 then
- if direction == 0 then
- x = x + 1
- elseif direction == 1 then
- y = y + 1
- elseif direction == 2 then
- x = x - 1
- elseif direction == 3 then
- y = y - 1
- end
- if length[size] then
- table.remove(length,size)
- end
- table.insert(length,1,{x,y})
- end
- repeat sleep(0.05) until paused == false
- for i,v in pairs(length) do
- if i ~= 1 then
- if v[1] == x and v[2] == y then
- dead = true
- end
- end
- end
- if x < 1 or x > width or y < 2 or y > height then
- dead = true
- end
- for i,v in pairs(fruit) do
- if v[1] == x and v[2] == y then
- table.remove(fruit,i)
- if v[3] then
- size = size + 5
- score = score + 500
- else
- size = size + 1
- score = score + 100
- end
- local special = math.random(1,5)
- if special == 1 then
- special = true
- else
- special = false
- end
- local xposn = math.random(2,width-1)
- local yposn = math.random(3,height-1)
- for i,v in pairs(fruit) do
- if xposn == v[1] then
- if xposn ~= 2 then
- xposn = xposn - 1
- else
- xposn = xposn + 1
- end
- end
- if yposn == v[2] then
- if yposn ~= 3 then
- yposn = yposn - 1
- else
- yposn = yposn + 1
- end
- end
- end
- table.insert(fruit,{xposn,yposn,special})
- end
- end
- end
- end
- local function scoreCount()
- while true do
- sleep(0.05)
- term.setTextColor(colors.cyan)
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(width-#('SCORE: '..score),1)
- write('SCORE: '..score)
- term.setCursorPos(2,1)
- write('LENGTH: '..#length)
- term.setCursorPos(20,1)
- write('HIGHSCORE: '..hscore)
- end
- end
- local function control()
- while true do
- sleep(0.01)
- local ev = {os.pullEvent('key')}
- local num = 0
- if ev[2] == keys.right and direction ~= 2 then
- direction = 0
- elseif ev[2] == keys.down and direction ~= 3 then
- direction = 1
- elseif ev[2] == keys.left and direction ~= 0 then
- direction = 2
- elseif ev[2] == keys.up and direction ~= 1 then
- direction = 3
- end
- end
- end
- parallel.waitForAny(pauseHandler,lose,display,update,control,scoreCount)
- term.setBackgroundColor(colors.black)
- if score > hscore then
- term.setCursorPos((width/2)-7,height/2)
- term.setTextColor(colors.yellow)
- print('[New highscore!]')
- local file = fs.open(filename,'w')
- file.writeLine(score)
- file.close()
- sleep(0.2)
- os.pullEvent('key')
- else
- term.setCursorPos((width/2)-4,height/2)
- term.setTextColor(colors.red)
- print('[You died!]')
- sleep(0.2)
- os.pullEvent('key')
- end
- term.setCursorPos(1,1)
- term.clear()
Advertisement
Add Comment
Please, Sign In to add comment