Advertisement
Ewgeniy

piano

Sep 24th, 2021
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.15 KB | None | 0 0
  1. local c = require("component")
  2. local event = require("event")
  3. local gpu = c.gpu
  4.  
  5. local xSize, ySize = gpu.getResolution()
  6.  
  7. local colors = {
  8.     whiteKeyColor = 0xffffff,
  9.     blackKeyColor = 0x000000,
  10.     background = 0x262626,
  11. }
  12.  
  13. local sizes = {
  14.     widthOfWhiteKey = 3,
  15.     heightOfWhiteKey = 10,
  16.     widthOfBlackKey = 3,
  17.     heightOfBlackKey = 5,
  18.     doubleKeyBlockConst = 7,
  19. }
  20. sizes.yStartOfKeys = ySize - sizes.heightOfWhiteKey + 1
  21. sizes.xStartOfKeys = 3
  22. sizes.countOfKeysOnScreen = 0
  23.  
  24. local frequencyMultiplyer
  25. local minFrequency, maxFrequency = 20, 2000
  26.  
  27. ------------------------------------------------------------
  28.  
  29. --СОЗДАНИЕ ОБЪЕКТОВ
  30. local obj = {}
  31. local function newObj(class, name, ...)
  32.     obj[class] = obj[class] or {}
  33.     obj[class][name] = {...}
  34. end
  35.  
  36. local function calculateFrequencyMultiplyer()
  37.     local difference = maxFrequency - minFrequency
  38.     frequencyMultiplyer = difference / sizes.countOfKeysOnScreen
  39. end
  40.  
  41. local function drawKeysBlock(x, y, countOfWhite, countOfBlack)
  42.     local xPos = x
  43.     for i = 1, countOfWhite do
  44.         ecs.square(xPos, y, sizes.widthOfWhiteKey, sizes.heightOfWhiteKey, colors.whiteKeyColor)
  45.        
  46.         sizes.countOfKeysOnScreen = sizes.countOfKeysOnScreen + 1
  47.  
  48.         newObj("Keys", sizes.countOfKeysOnScreen, xPos, y + sizes.heightOfBlackKey, xPos + sizes.widthOfWhiteKey - 1, y + sizes.heightOfWhiteKey - 1, true)
  49.  
  50.         xPos = xPos + sizes.widthOfWhiteKey + 1    
  51.     end
  52.     xPos = x + 2
  53.     for i = 1, countOfBlack do
  54.         ecs.square(xPos, y, sizes.widthOfBlackKey, sizes.heightOfBlackKey, colors.blackKeyColor)
  55.  
  56.         newObj("Keys", sizes.countOfKeysOnScreen + 1, xPos, y, xPos + sizes.widthOfBlackKey - 1, y + sizes.heightOfBlackKey - 1, false)
  57.  
  58.         sizes.countOfKeysOnScreen = sizes.countOfKeysOnScreen + 1
  59.  
  60.         xPos = xPos + sizes.widthOfBlackKey + 1
  61.     end
  62. end
  63.  
  64. local function drawTwoKeysBlock(x, y)
  65.     drawKeysBlock(x, y, 3, 2)
  66. end
  67.  
  68. local function drawThreeKeysBlock(x, y)
  69.     drawKeysBlock(x, y, 4, 3)
  70. end
  71.  
  72. local function drawDoubleKeyBlock(x, y)
  73.     sizes.countOfKeysOnScreen = 0
  74.     local xPos = x
  75.     for i = 1, 3 do
  76.         drawTwoKeysBlock(xPos, y)
  77.         xPos = xPos + 3 * (sizes.widthOfWhiteKey + 1)
  78.         drawThreeKeysBlock(xPos, y)
  79.         xPos = xPos + 4 * (sizes.widthOfWhiteKey + 1)
  80.     end
  81.     drawTwoKeysBlock(xPos, y)
  82.  
  83.     calculateFrequencyMultiplyer()
  84. end
  85.  
  86. local function setFrequencyToKeys()
  87.     calculateFrequencyMultiplyer()
  88.     local justBecomeBlack = false
  89.     local whiteCounter = 0
  90.     local currentWhiteFrequency, currentBlackFrequency = minFrequency, minFrequency + frequencyMultiplyer
  91.     for i = 1, #obj["Keys"] do
  92.         if obj["Keys"][i][5] then
  93.             whiteCounter = whiteCounter + 1
  94.             justBecomeBlack = true
  95.             currentWhiteFrequency = currentWhiteFrequency + frequencyMultiplyer
  96.             obj["Keys"][i][6] = currentWhiteFrequency
  97.         else
  98.             if justBecomeBlack then currentBlackFrequency = currentWhiteFrequency - frequencyMultiplyer * (whiteCounter - 1); justBecomeBlack = false end
  99.             currentBlackFrequency = currentBlackFrequency + frequencyMultiplyer
  100.             obj["Keys"][i][6] = currentBlackFrequency
  101.             whiteCounter = 0
  102.         end
  103.     end
  104. end
  105.  
  106. local function drawAll()
  107.     ecs.clearScreen(colors.background)
  108.     drawDoubleKeyBlock(sizes.xStartOfKeys, sizes.yStartOfKeys)
  109. end
  110.  
  111. drawAll()
  112. setFrequencyToKeys()
  113.  
  114. while true do
  115.     local e = {event.pull()}
  116.     if e[1] == "touch" then
  117.         for key, val in pairs(obj["Keys"]) do
  118.             if ecs.clickedAtArea(e[3], e[4], obj["Keys"][key][1], obj["Keys"][key][2], obj["Keys"][key][3], obj["Keys"][key][4]) then
  119.                 if obj["Keys"][key][5] then
  120.                     ecs.square(obj["Keys"][key][1], obj["Keys"][key][2], sizes.widthOfWhiteKey, sizes.heightOfWhiteKey - sizes.heightOfBlackKey, ecs.colors.green)
  121.                 else
  122.                     ecs.square(obj["Keys"][key][1], obj["Keys"][key][2], sizes.widthOfBlackKey, sizes.heightOfBlackKey, ecs.colors.green)
  123.                 end
  124.  
  125.                 c.computer.beep(obj["Keys"][key][6])
  126.  
  127.                 if obj["Keys"][key][5] then
  128.                     ecs.square(obj["Keys"][key][1], obj["Keys"][key][2], sizes.widthOfWhiteKey, sizes.heightOfWhiteKey - sizes.heightOfBlackKey, colors.whiteKeyColor)
  129.                 else
  130.                     ecs.square(obj["Keys"][key][1], obj["Keys"][key][2], sizes.widthOfBlackKey, sizes.heightOfBlackKey, colors.blackKeyColor)
  131.                 end
  132.  
  133.                 break
  134.             end
  135.         end
  136.     end
  137. end
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement