Advertisement
qwertyMAN_rus

Snake

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