Advertisement
qwertyMAN_rus

Snake [OC]

Jan 24th, 2016 (edited)
1,940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.42 KB | None | 0 0
  1. -- Автор: qwertyMAN
  2. -- Версия: 0.1 бета
  3.  
  4. local term              = require("term")
  5. local event             = require("event")
  6. local component         = require("component")
  7. local gpu               = component.gpu
  8. local display           = {gpu.getResolution()}
  9. local players           = {}        -- свойства игроков
  10. local players_vector    = {}        -- направления игроков
  11. local area              = {}        -- игровое поле
  12. local turn              = {}        -- таблица поворота
  13. local target            = {}        -- координаты цели
  14. local border            = {0,0}     -- неиспользуемый отступ от края экрана
  15. local t                 = 1/8       -- скорость игры
  16. local exit_game         = false     -- для выхода из игры
  17. local size              = {math.modf((display[1]-border[1])/2), math.modf(display[2]-border[2])} -- размер игрового поля
  18. math.randomseed(os.time())
  19.  
  20. turn[1]={0,-1}
  21. turn[2]={0,1}
  22. turn[3]={-1,0}
  23. turn[4]={1,0}
  24.  
  25. local command={}                    -- управление:
  26. command[200]=function(nick)         -- вверх
  27.     if players_vector[nick] ~= 2 then
  28.         players_vector[nick] = 1
  29.     end
  30. end
  31. command[208]=function(nick)         -- вниз
  32.     if players_vector[nick] ~= 1 then
  33.         players_vector[nick] = 2
  34.     end
  35. end
  36. command[203]=function(nick)         -- влево
  37.     if players_vector[nick] ~= 4 then
  38.         players_vector[nick] = 3
  39.     end
  40. end
  41. command[205]=function(nick)         -- вправо
  42.     if players_vector[nick] ~= 3 then
  43.         players_vector[nick] = 4
  44.     end
  45. end
  46.  
  47. -- генерация поля
  48. for i=1, size[1] do
  49.     area[i]={}
  50.     for j=1, size[2] do
  51.         area[i][j]=false
  52.     end
  53. end
  54.  
  55. local function conv_cord(sx,sy)
  56.     return sx*2-1+border[1], sy+border[2]
  57. end
  58.  
  59. local function gen_target()
  60.     while true do
  61.         local x,y = math.random(1,size[1]), math.random(1,size[2])
  62.         if not area[x][y] then
  63.             target = {x,y}
  64.             gpu.setBackground(0x0000ff)
  65.             local rezerv = {conv_cord(x,y)}
  66.             gpu.set(rezerv[1], rezerv[2], "  ")
  67.             gpu.setBackground(0x000000)
  68.             break
  69.         end
  70.     end
  71. end
  72.  
  73. local function keyboard(_,_,_,key,nick)
  74.     local swich = true
  75.     for i=1, #players do
  76.         if nick==players[i].name then
  77.             swich = false
  78.         end
  79.     end
  80.     if swich and (key==200 or key == 203 or key == 205 or key == 208) then
  81.         -- если игрока нет в списке
  82.         players[#players+1]={name=nick,number=5,cord={5,5}}
  83.         area[players[#players].cord[1]][players[#players].cord[2]]=players[#players].number
  84.     end
  85.     if key == 16 then -- выход
  86.         exit_game = true
  87.     elseif command[key] then
  88.         command[key](nick)
  89.     end
  90. end
  91.  
  92. local function update()
  93.     -- проверка есть ли препятствие
  94.     for i=#players, 1, -1  do
  95.         local cord = turn[players_vector[players[i].name]]
  96.         local cord_2 = {players[i].cord[1]+cord[1],players[i].cord[2]+cord[2]}
  97.         gpu.setBackground(0xffffff)
  98.         local rezerv = {conv_cord(players[i].cord[1], players[i].cord[2])}
  99.         gpu.set(rezerv[1], rezerv[2], "  ")
  100.         gpu.setBackground(0x000000)
  101.         if cord_2[1]>size[1] then
  102.             cord_2[1] = 1
  103.         elseif cord_2[1] < 1 then
  104.             cord_2[1] = size[1]
  105.         elseif cord_2[2]>size[2] then
  106.             cord_2[2] = 1
  107.         elseif cord_2[2] < 1 then
  108.             cord_2[2] = size[2]
  109.         end
  110.         if not area[cord_2[1]][cord_2[2]] then
  111.             players[i].cord[1]=cord_2[1]
  112.             players[i].cord[2]=cord_2[2]
  113.             area[players[i].cord[1]][players[i].cord[2]]=players[i].number
  114.             gpu.setBackground(0x00ff00)
  115.             gpu.setForeground(0x000000)
  116.             local rezerv = {conv_cord(players[i].cord[1], players[i].cord[2])}
  117.             gpu.set(rezerv[1], rezerv[2], string.sub(players[i].name,1,2))
  118.             if target[1]==players[i].cord[1] and target[2]==players[i].cord[2] then
  119.                 players[i].number = players[i].number+1
  120.                 gen_target()
  121.             end
  122.         else
  123.             table.remove(players,i)
  124.         end
  125.         gpu.setBackground(0x000000)
  126.         gpu.setForeground(0xffffff)
  127.     end
  128.    
  129.     -- обновление и добавление ячеек
  130.     for i=1, size[1] do
  131.         for j=1, size[2] do
  132.             if area[i][j] then
  133.                 if area[i][j]>0 then
  134.                     area[i][j]=area[i][j]-1
  135.                 else
  136.                     area[i][j]=false
  137.                     gpu.setBackground(0x000000)
  138.                     local rezerv = {conv_cord(i,j)}
  139.                     gpu.set(rezerv[1], rezerv[2], "  ")
  140.                 end
  141.             end
  142.            
  143.         end
  144.     end
  145. end
  146.  
  147. -- очищаем экран
  148. gpu.setBackground(0x000000)
  149. gpu.setForeground(0xffffff)
  150. term.clear()
  151. event.listen("key_down", keyboard)
  152.  
  153. gen_target()
  154.  
  155. -- тело игры
  156. while true do
  157.     os.sleep(t)
  158.     if exit_game then
  159.         term.clear()
  160.         print("Exit game")
  161.         os.sleep(2)
  162.         term.clear()
  163.         return
  164.     end
  165.     update()
  166. end
  167.  
  168. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement