Advertisement
MoonlightOwl

Find moon treasure!

Jun 15th, 2016
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.75 KB | None | 0 0
  1. import
  2.   getResolution
  3.   setForeground
  4.   setBackground
  5.   set
  6.   fill
  7.   from require('component').gpu
  8. import pull from require 'event'
  9. import ceil, random from math
  10. import rep from string
  11.  
  12. -- Размеры экрана
  13. width, height = getResolution()
  14. width /= 2    -- потому что по горизонтали наши клетки займут 2 символа
  15. height -= 1   -- потому что внизу будет статус
  16. -- Цвета
  17. white = 0xFFFFFF
  18. black = 0x000000
  19. gray  = 0x222222
  20. green = 0x00BB33
  21. yellow = 0xFFC04C
  22. red = 0xFF0000
  23. pink = 0xFF0074
  24. violet = 0xD600FF
  25. blue = 0x4E5AFF
  26. cyan = 0x4ED7FF
  27. teal = 0x00CC99
  28. -- Заготовка для сетки - один ряд клеток
  29. grid_line = rep("▒▒  ", ceil(width / 2))
  30.  
  31. -- Наша цель
  32. target = { x: 0, y: 0 }
  33. -- Количество попыток
  34. maxAttemts = ceil(width * height / 150)    -- 150 - магический коэффициент сложности, больше - сложнее, меньше - легче
  35. attempts = maxAttemts
  36.  
  37. -- Очищаем экран
  38. clear = () ->
  39.   setForeground white
  40.   setBackground black
  41.   fill 1, 1, width * 2, height + 1, ' '
  42.  
  43. -- Рисуем сетку
  44. grid = ->
  45.   setForeground gray
  46.   setBackground black
  47.   for y = 1, height
  48.     set (if y % 2 == 0 then 1 else 3), y, grid_line
  49.  
  50. -- Открываем одну клетку
  51. sign = (x, y) ->
  52.   if x == target.x and y == target.y then black, white, "[]"
  53.   elseif x == target.x and y < target.y then white, green, "▼▼"   -- по неведомой мне причине, стрелки вниз в новом шрифте ОС 1.6 нету =)
  54.   elseif x == target.x and y > target.y then white, violet, "↑↑"
  55.   elseif x < target.x and y < target.y then white, teal, "↘↘"
  56.   elseif x < target.x and y == target.y then white, cyan, "→→"
  57.   elseif x < target.x and y > target.y then white, blue, "↗↗"
  58.   elseif x > target.x and y < target.y then white, yellow, "↙↙"
  59.   elseif x > target.x and y == target.y then white, red, "←←"
  60.   elseif x > target.x and y > target.y then white, pink, "↖↖"
  61.  
  62. cell = (x, y) ->
  63.   fore, back, text = sign x, y
  64.   setForeground fore
  65.   setBackground back
  66.   set x * 2 - 1, y, text
  67.  
  68. -- Рисуем статус
  69. status = (state) ->
  70.   setForeground white
  71.   setBackground black
  72.   fill 1, height + 1, width * 2, height + 1, ' '
  73.   set 2, height + 1, "[Угадай, где клад!]"
  74.   switch state
  75.     when 'win'
  76.       setForeground green
  77.       set 24, height + 1, "Вы победили!"
  78.     when 'lose'
  79.       setForeground red
  80.       set 24, height + 1, "Вы проиграли!"
  81.     else
  82.       set 24, height + 1, "Попыток осталось: #{attempts}"
  83.   set width * 2 - 10, height + 1, "[R] [Q]"
  84.  
  85.  
  86. -- Генерируем цель
  87. setTarget = ->
  88.   target = { x: random(1, width), y: random(1, height) }
  89.  
  90. -- Инициализируем игру
  91. newGame = ->
  92.   attempts = maxAttemts
  93.   setTarget!
  94.   clear!
  95.   grid!
  96.   status!
  97.  
  98. -- Поехали!
  99. newGame!
  100.  
  101. while true
  102.   -- Ждем события
  103.   event, _, x, y = pull!
  104.   -- Обрабатываем его
  105.   switch event
  106.     when 'touch'   -- Если был клик
  107.       -- Открываем клетку, если остались попытки
  108.       if attempts > 0
  109.         x = ceil(x / 2)
  110.         cell x, y
  111.         attempts -= 1
  112.         -- Обновляем инфу
  113.         if x == target.x and y == target.y
  114.           attempts = 0
  115.           status('win')
  116.         elseif attempts == 0
  117.           status('lose')
  118.         else
  119.           status!
  120.  
  121.     when 'key_down'
  122.       switch x
  123.         when 113   -- Q: выход из игры
  124.           break
  125.         when 114   -- R: перезапуск
  126.           newGame!
  127.  
  128. clear!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement