KRITSADA

Games

Apr 8th, 2022
1,530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let y = 0
  2. let x = 0
  3. let tempY = 0
  4. let tempX = 0
  5. let neighbours = 0
  6. let cellCount = 0
  7. let init = true
  8. let smiley = true
  9.  
  10. let map =
  11.         [[false, false, false, false, false],
  12.         [false, true, false, true, false],
  13.         [false, false, false, false, false],
  14.         [true, false, false, false, true],
  15.         [false, true, true, true, false]
  16. ];
  17.  
  18. RefreshScreen()
  19.  
  20. function RefreshScreen() {
  21.     x = 0
  22.     cellCount = 0
  23.     while (x < 5) {
  24.         y = 0
  25.         while (y < 5) {
  26.             if (map[y][x]) {
  27.                 led.plot(x, y)
  28.                 cellCount+=1
  29.             } else {
  30.                 led.unplot(x, y);
  31.             }
  32.             y += 1
  33.         }
  34.         x += 1
  35.     }
  36.  
  37.     if (!init) {
  38.         if (!map[y,x]) {
  39.             led.plotBrightness(tempX, tempY, 10)
  40.         } else {
  41.             led.plotBrightness(tempX, tempY, 255)
  42.         }
  43.     }
  44. }
  45.  
  46. function ConwayAlgo() {
  47.     x = 0
  48.     while (x < 5) {
  49.         y = 0
  50.         while (y < 5) {
  51.             CountNeighbours(x, y)
  52.             if (led.point(x, y)) {
  53.                 if (neighbours < 2 || neighbours > 3) {
  54.                     map[y][x] = false
  55.                 }
  56.             } else {
  57.                 if (neighbours === 3) {
  58.                     map[y][x] = true
  59.                 }
  60.             }
  61.             y += 1
  62.         }
  63.         x += 1
  64.     }
  65. }
  66.  
  67. function CountNeighbours(i = 0, j = 0) {
  68.     neighbours = 0
  69.     if (i < 4) {
  70.         if (led.point(i + 1, j)) {
  71.             neighbours++
  72.         }
  73.  
  74.         if (j < 4) {
  75.             if (led.point(i + 1, j + 1)) {
  76.                 neighbours++
  77.             }
  78.         }
  79.  
  80.         if (j > 0) {
  81.             if (led.point(i + 1, j - 1)) {
  82.                 neighbours++
  83.             }
  84.         }
  85.     }
  86.  
  87.     if (i > 0) {
  88.         if (led.point(i - 1, j)) {
  89.             neighbours++
  90.         }
  91.  
  92.         if (j < 4) {
  93.             if (led.point(i - 1, j + 1)) {
  94.                 neighbours++
  95.             }
  96.         }
  97.  
  98.         if (j > 0) {
  99.             if (led.point(i - 1, j - 1)) {
  100.                 neighbours++
  101.             }
  102.         }
  103.     }
  104.  
  105.     if (j < 4) {
  106.         if (led.point(i, j + 1)) {
  107.             neighbours++
  108.         }
  109.     }
  110.  
  111.     if (j > 0) {
  112.         if (led.point(i, j - 1)) {
  113.             neighbours++
  114.         }
  115.     }
  116. }
  117. basic.forever(function () {
  118.     if (init) {
  119.         music.playTone(Note.F4, 50)
  120.         RefreshScreen()
  121.         if (smiley) {
  122.             music.playTone(Note.F5, 50)
  123.             basic.pause(1000)
  124.             smiley = false
  125.         }
  126.         ConwayAlgo()
  127.         basic.pause(300)
  128.         if (cellCount === 0) {
  129.             basic.pause(100)
  130.             music.playTone(Note.C3, 50)
  131.             basic.pause(100)
  132.             music.playTone(Note.A3, 50)
  133.             init = false;
  134.             RefreshScreen()
  135.         }
  136.     }
  137. })
  138.  
  139. input.onButtonPressed(Button.A, function () {
  140.     if (!init) {
  141.         map[tempY][tempX] = !map[tempY][tempX]
  142.  
  143.         RefreshScreen()
  144.         music.playTone(Note.A4, 50)
  145.     }
  146. })
  147.  
  148. input.onButtonPressed(Button.B, function () {
  149.     if (!init) {
  150.         tempX += 1
  151.         if (tempX > 4) {
  152.             tempX = 0
  153.             tempY += 1
  154.             if (tempY > 4) {
  155.                 tempY = 0
  156.             }
  157.         }
  158.         RefreshScreen()
  159.         music.playTone(Note.C4, 50)
  160.  
  161.     }
  162. })
  163.  
  164. input.onButtonPressed(Button.AB, function () {
  165.     if (!smiley) {
  166.     init = !init
  167.     RefreshScreen()
  168.     if(init) {
  169.     music.playTone(Note.E5, 100)
  170.     } else {
  171.         music.playTone(Note.E3, 100)
  172.     }
  173.     }
  174. })
  175.  
Advertisement