Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- w, h = term.getSize()
- startX = math.floor( w / 2 )
- startY = math.floor( h / 2 )
- x = math.floor( w / 2 )
- y = math.floor( h / 2 )
- fruitFunc = true
- counter = 0
- gameSpeed = 0.2
- function score()
- paintutils.drawLine(1, 1, w, 1, colors.black)
- term.setCursorPos(1, 1)
- term.setTextColor(colors.yellow)
- write("Score: " .. counter .. " | Current x/y: " .. x .. " " .. y - 1 .. " | Fruit x/y: " .. fruitCoord["x"] .. " " .. fruitCoord["y"])
- end
- function controls()
- event, key = os.pullEvent("key")
- return key
- end
- function direction()
- local cont = controls()
- if cont == 17 then return {0, -1}
- elseif cont == 31 then return {0, 1}
- elseif cont == 32 then return {1, 0}
- elseif cont == 30 then return {-1, 0}
- end
- end
- term.clear()
- paintutils.drawPixel(startX, startY, colors.red)
- tab = {0, 0}
- function smb()
- tab = direction()
- sleep(gameSpeed)
- end
- function flow()
- paintutils.drawPixel(-1, -1, colors.black)
- x = x + tab[1]
- y = y + tab[2]
- term.clear()
- if x > w then x = 1
- elseif x < 1 then x = w
- elseif y > h then y = 2
- elseif y < 2 then y = h
- end
- if fruitCoord["isExists"] == true then
- fruitFunc = false
- else fruitFunc = true end
- fruit(fruitFunc)
- paintutils.drawPixel(x, y, colors.red)
- fruitCatch()
- score()
- sleep(gameSpeed)
- return x, y
- end
- fruitCoord = {}
- function fruit(state)
- if state == true then
- fruitCoord["x"] = math.random(1, w)
- fruitCoord["y"] = math.random(2, h)
- fruitCoord["isExists"] = true
- end
- paintutils.drawPixel(fruitCoord["x"], fruitCoord["y"], colors.green)
- end
- function fruitCatch()
- if fruitCoord["x"] == x then
- if fruitCoord["y"] == y then
- counter = counter + 1
- fruitCoord["isExists"] = false
- end
- end
- end
- while true do
- parallel.waitForAny(flow, smb)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement