Guest User

flappyBlock

a guest
Jan 25th, 2016
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.72 KB | None | 0 0
  1. --Floppy Block v.0.2
  2. --Автор: newbie
  3.  
  4. local term = require("term")
  5. local event = require("event")
  6. local computer = require("computer")
  7. local component = require("component")
  8. local fs = require("filesystem")
  9. local gpu = component.gpu
  10. local serialization = require("serialization")
  11. local xSize, ySize = gpu.getResolution()
  12. local width = 30
  13. local height = 22
  14. local startXPosPlayer = 8
  15. local tempPosPlayer = 10
  16. local nicknames
  17. local records
  18. local name
  19. local count = 0
  20. local tCount = 0
  21. local colors = {
  22.     player = 0x00FF00,
  23.     bg = 0x000000,
  24.     floor = 0xFF4500,
  25.     walls = 0xFFD700,
  26.     text = 0xFFFFFF,
  27.     button = 0x4F4F4F
  28. }
  29. local quit = false
  30. local game = true
  31. local fin = false
  32. local function start()
  33.     term.clear()
  34.     gpu.setForeground(colors.player)
  35.     gpu.set(6, 10, "Кликни чтоб начать")
  36.     gpu.set(5, 11, "Жми кнопки чтоб жить")
  37.     gpu.setForeground(colors.text)
  38.     local e = {event.pull("touch")}
  39.     name = e[6]
  40.     computer.addUser(name)--Эту строку лучше коментить если игру ставите на личный комп
  41. end
  42. local function paintWall()
  43.     local function up() --cлушалка
  44.     if tempPosPlayer <= 2 then --проверка на удар сверху
  45.         fin = true
  46.         game = false
  47.         event.ignore("key_down", up)
  48.     end
  49.     gpu.set(startXPosPlayer, tempPosPlayer, "  ")
  50.     tempPosPlayer = tempPosPlayer - 1
  51.     gpu.setBackground(colors.player)
  52.     gpu.set(startXPosPlayer, tempPosPlayer, "  ")
  53.     gpu.setBackground(colors.bg)
  54.     os.sleep(0.1)
  55.     end
  56.     tempPosPlayer = 10
  57.     while game do
  58.         gpu.set(2, 3, tostring(tCount))
  59.         --Делает нам на случайной высоте отвертие в 5 блоков
  60.         local randomY = math.modf(math.random(2,15))
  61.         for i = 1, 29 do
  62.             local a = 29 - i
  63.             gpu.setBackground(colors.walls)
  64.             for i=2, randomY do
  65.                 gpu.set(a, i, "  ")
  66.             end
  67.             for i = randomY + 5, 21 do
  68.                 gpu.set(a, i, "  ")
  69.             end
  70.             local function checkWall()
  71.                 rand = randomY + 5
  72.                 if startXPosPlayer + 1 == a then  --лобовое столкновение сверху
  73.                     if randomY>= tempPosPlayer -1 then
  74.                         tempPosPlayer = 21
  75.                     end
  76.                 elseif startXPosPlayer == a then  --удар в верхний угол задним пикселем
  77.                     if randomY>= tempPosPlayer - 1 then
  78.                         tempPosPlayer = 21
  79.                     end
  80.                 elseif startXPosPlayer == a+1 then  --совпадение второго пикселя с задним вверху
  81.                     if randomY>= tempPosPlayer-1 then
  82.                         tempPosPlayer = 21
  83.                     end
  84.                 elseif startXPosPlayer == a+2 then  --совпадение второго пикселя с задним вверху
  85.                     if randomY>= tempPosPlayer-1 then
  86.                         tempPosPlayer = 21
  87.                     end
  88.                 end
  89.                 if startXPosPlayer + 1 == a then  --лобовое столкновение снизу
  90.                     if tempPosPlayer+1 >= rand then
  91.                         tempPosPlayer = 21
  92.                     end
  93.                 elseif startXPosPlayer  == a then --удар в нижний угол задним пикселем
  94.                     if tempPosPlayer+1 >= rand then
  95.                         tempPosPlayer = 21
  96.                     end
  97.                 elseif startXPosPlayer == a+1 then  --совпадение второго пикселя с задним сверху
  98.                     if tempPosPlayer +1 >= rand then
  99.                         tempPosPlayer = 21
  100.                     end
  101.                 elseif startXPosPlayer == a+2 then  --совпадение второго пикселя с задним сверху
  102.                     if tempPosPlayer +1 >= rand then
  103.                         tempPosPlayer = 21
  104.                     end
  105.                 end
  106.             end
  107.                 checkWall()
  108.                 if tempPosPlayer>=21 then --проверка на удар снизу
  109.                     fin = true
  110.                     game = false
  111.                     event.ignore("key_down", up)
  112.                     break
  113.                 end
  114.                 --отрисовка, перерисовка игрока
  115.                 gpu.setBackground(colors.bg)
  116.                 gpu.set(startXPosPlayer, tempPosPlayer, "  ")
  117.                 tempPosPlayer = tempPosPlayer + 1
  118.                 gpu.setBackground(colors.player)
  119.                 gpu.set(startXPosPlayer, tempPosPlayer, "  ")
  120.                 gpu.setBackground(colors.bg)
  121.                 os.sleep(0.2)
  122.                 event.listen("key_down", up)
  123.                 if startXPosPlayer == a then
  124.                     tCount = tCount + 1
  125.                     gpu.set(2, 3, tostring(tCount))
  126.                 end
  127.             gpu.setBackground(colors.bg)
  128.             for i=2, randomY do
  129.                 gpu.set(a, i, "   ")
  130.             end
  131.             for i = randomY + 5, 21 do
  132.                 gpu.set(a, i, "   ")
  133.             end
  134.             if fin then
  135.                 break
  136.             end
  137.         end
  138.     end
  139. end
  140. local pathToRecords = "records.txt" --путь к файлу с рекордами
  141. local function saveRecord() --Сохраняем рекорды
  142.     local file = io.open(pathToRecords, "w")
  143.     local array = {["nicknames"] = nicknames, ["records"] = records}
  144.     file:write(serialization.serialize(array))
  145.     file:close()
  146. end
  147. local function loadRecord()  --Загружаем рекорды
  148.     if fs.exists(pathToRecords) then
  149.         local array = {}
  150.         local file = io.open(pathToRecords, "r")
  151.         local str = file:read("*a")
  152.         array = serialization.unserialize(str)
  153.         file:close()
  154.         nicknames = array.nicknames
  155.         records = array.records
  156.     else --или создаем новые дефолтные пустые таблицы
  157.         fs.makeDirectory(fs.path(pathToRecords))
  158.             nicknames = {}
  159.             records = {}
  160.             saveRecord()
  161.     end
  162. end
  163. local function checkName(name)  --Проверка на наличие имени в базе
  164.     for i =1, #nicknames do
  165.         if name == nicknames[i] then
  166.             count = records[i]
  167.             return false
  168.         end
  169.     end
  170.     return true
  171. end
  172. local function addPlayer()  --Создаем учетку пользователю если его нет в базе
  173.     if checkName(name) then
  174.         table.insert(nicknames, name)
  175.         table.insert(records, count)
  176.         saveRecord()
  177.     end
  178. end
  179. local function gameOver() --Игра окончена
  180.     gpu.setBackground(colors.bg)
  181.     term.clear()
  182.     gpu.setForeground(colors.player)
  183.     gpu.set(10,11,"GAME OVER!")
  184.     gpu.set(8,14,"You count:   "..tostring(tCount))
  185.     gpu.setForeground(colors.text)
  186.     count = 0
  187.     tCount = 0
  188.     game = true
  189.     fin = false
  190.     computer.removeUser(name) --опять же коментим эту строку если комп не публичный
  191. end
  192. local function saveCount() --сохраняем наши заработанные очки
  193.     for i = 1, #nicknames do
  194.         if name == nicknames[i] then
  195.             count = records[i]
  196.             if tCount > count then
  197.                 records[i] = tCount
  198.             end
  199.         end
  200.     end
  201.     saveRecord()
  202. end
  203. local function sortTop() --Сортируем Топ игроков
  204.     for i=1, #records do
  205.       for j=1, #records-1 do
  206.         if records[j] < records[j+1] then
  207.           local r = records[j+1]
  208.           local n = nicknames[j+1]
  209.           records[j+1] = records[j]
  210.           nicknames[j+1] = nicknames[j]
  211.           records[j] = r
  212.           nicknames[j] = n
  213.         end
  214.       end
  215.     end
  216.     saveRecord()
  217. end
  218. function paintScene() --Рисуем сцену
  219.     term.clear()
  220.     gpu.setBackground(colors.floor)
  221.     gpu.set(0,1,"                               ")
  222.     gpu.set(0,22,"                               ")
  223.     gpu.setBackground(colors.bg)
  224. end
  225. local function printRecords()  --Выводим рекорды на экран
  226.     term.clear()
  227.     local xPosName = 5
  228.     local xPosRecord = 20
  229.     local yPos = 1
  230.     loadRecord()
  231.         gpu.setForeground(colors.player)
  232.         gpu.set(11,1,"Top - 15")
  233.     if #nicknames <= 15 then
  234.     for i = 1, #nicknames do
  235.         yPos= yPos+1
  236.         gpu.set(xPosName, yPos, nicknames[i] )
  237.         gpu.set(xPosRecord, yPos, tostring(records[i]))
  238.     end
  239.     else
  240.         for i = 1, 15 do
  241.         yPos= yPos+1
  242.         gpu.set(xPosName, yPos, nicknames[i] )
  243.         gpu.set(xPosRecord, yPos, tostring(records[i]))
  244.         end
  245.     end
  246.     gpu.setForeground(colors.text)
  247.     os.sleep(10)
  248.     floppyBlock()
  249. end
  250. function main()
  251. start()
  252. addPlayer()
  253. paintScene()
  254. paintWall()
  255. saveCount()
  256. gameOver()
  257. os.sleep(3)
  258. floppyBlock()
  259. end
  260. function floppyBlock()
  261.     term.clear()
  262.     event.shouldInterrupt = function() return false end --Alt+ Ctrl + C не пашет, так же на ваше усмотрение
  263.     gpu.setResolution(width, height)
  264.     gpu.setForeground(colors.player)
  265.     loadRecord()
  266.     gpu.set(9,5,"Flappy Block")
  267.     gpu.setBackground(colors.button)
  268.     gpu.set(12,15," Play ")
  269.     gpu.set(11,17," Top-15 ")
  270.     gpu.set(12,20," Quit ")
  271.     gpu.setBackground(colors.bg)
  272.     while true do
  273.         local e = {event.pull("touch")}
  274.         if e[4] == 15 then
  275.             if e[3]>12 then
  276.                 if e[3]<18 then main() end 
  277.             end
  278.         elseif e[4] == 17 then
  279.             if e[3]>11 then
  280.                 if e[3]<19 then
  281.                     sortTop()
  282.                     printRecords()
  283.                 end
  284.             end
  285.         elseif e[4] == 20 then
  286.             if e[3]>12 then
  287.                 if e[3]<18 then
  288.                     if e[6] == "newbie" then --В эту строку заносим ник того кто может закрыть игру, если ненужно,
  289.                         --коментим ее
  290.                         gpu.setForeground(colors.text)
  291.                         gpu.setResolution(xSize,ySize)
  292.                         term.clear()
  293.                         quit = true
  294.                         break
  295.                     end --и тут
  296.                 end
  297.             end
  298.         end
  299.     if quit then break end
  300.     end
  301. end
  302. floppyBlock()
Add Comment
Please, Sign In to add comment