Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Snake 1.2 by Hilmvhal
- require "os"
- local term = require("term")
- local keyboard =require("keyboard")
- function init()
- score = 0
- Height = 12
- Width = 25
- facing = 0
- xpos = 5
- ypos = 6
- length = 3
- tailx = {}
- taily = {}
- fruitx = 10
- fruity = 10
- Gameover = false
- started = false
- end
- function fruit()
- fruitx = math.random(Width-2)+1
- fruity = math.random(Height-2)+1
- while fruitx == posx or fruity == posy or tail(fruitx, fruity) == true do
- fruitx = math.random(Width-2)+1
- fruity = math.random(Height-2)+1
- end
- length = length + 1
- score = score + 50
- end
- function input()
- if keyboard.isKeyDown(keyboard.keys.up) == true then facing = 1
- elseif keyboard.isKeyDown(keyboard.keys.right) == true then facing = 2
- elseif keyboard.isKeyDown(keyboard.keys.left) == true then facing = 3
- elseif keyboard.isKeyDown(keyboard.keys.down) == true then facing = 4
- end
- if facing > 0 then started = true end
- end
- function Gamelogic()
- local oldx = xpos
- local oldy = ypos
- if facing == 1 then ypos = ypos - 1
- elseif facing == 2 then xpos = xpos + 1
- elseif facing == 3 then xpos = xpos - 1
- elseif facing == 4 then ypos = ypos + 1
- end
- -- tail
- for z = 1 , length do
- tailx[z] = tailx[z+1]
- taily[z] = taily[z+1]
- end
- tailx[length] = oldx
- taily[length] = oldy
- collision()
- end
- function collision()
- if xpos == 1 or xpos == Width then Gameover = true end
- if ypos == 1 or ypos == Height then Gameover = true end
- if tail(xpos , ypos) == true then Gameover = true end
- if xpos == fruitx and ypos == fruity then fruit() end
- end
- function tail(x , y)
- for i=1 , length do
- if x == tailx[i] and y == taily[i] then return true
- end
- end
- return false
- end
- function Draw()
- local str = ""
- for j=1 , Height do
- str = ""
- for i=1 , Width do
- local chr = '.'
- if i==1 or i==Width or j==1 or j==Height then chr = '#'
- elseif i==xpos and j==ypos then chr = 'O'
- elseif tail(i , j) == true then chr = 'o'
- elseif i==fruitx and j==fruity then chr = '@'
- else chr = '.'
- end
- str = str .. chr
- end
- str = str
- print(str)
- end
- print("score: ", score)
- end
- function Start()
- local x = false
- while x == false do
- input();
- end
- function game()
- start();
- while Gameover == false do
- term.clear()
- input()
- Gamelogic()
- Draw()
- os.sleep(1/4)
- end
- print("GameOver Man")
- print("You scored "..score.." points!")
- end
- while quit == false do
- init()
- game()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement