Advertisement
serafim7

snake (игра змейка) [OpenComputers]

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