Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let y = 0
- let x = 0
- let tempY = 0
- let tempX = 0
- let neighbours = 0
- let cellCount = 0
- let init = true
- let smiley = true
- let map =
- [[false, false, false, false, false],
- [false, true, false, true, false],
- [false, false, false, false, false],
- [true, false, false, false, true],
- [false, true, true, true, false]
- ];
- RefreshScreen()
- function RefreshScreen() {
- x = 0
- cellCount = 0
- while (x < 5) {
- y = 0
- while (y < 5) {
- if (map[y][x]) {
- led.plot(x, y)
- cellCount+=1
- } else {
- led.unplot(x, y);
- }
- y += 1
- }
- x += 1
- }
- if (!init) {
- if (!map[y,x]) {
- led.plotBrightness(tempX, tempY, 10)
- } else {
- led.plotBrightness(tempX, tempY, 255)
- }
- }
- }
- function ConwayAlgo() {
- x = 0
- while (x < 5) {
- y = 0
- while (y < 5) {
- CountNeighbours(x, y)
- if (led.point(x, y)) {
- if (neighbours < 2 || neighbours > 3) {
- map[y][x] = false
- }
- } else {
- if (neighbours === 3) {
- map[y][x] = true
- }
- }
- y += 1
- }
- x += 1
- }
- }
- function CountNeighbours(i = 0, j = 0) {
- neighbours = 0
- if (i < 4) {
- if (led.point(i + 1, j)) {
- neighbours++
- }
- if (j < 4) {
- if (led.point(i + 1, j + 1)) {
- neighbours++
- }
- }
- if (j > 0) {
- if (led.point(i + 1, j - 1)) {
- neighbours++
- }
- }
- }
- if (i > 0) {
- if (led.point(i - 1, j)) {
- neighbours++
- }
- if (j < 4) {
- if (led.point(i - 1, j + 1)) {
- neighbours++
- }
- }
- if (j > 0) {
- if (led.point(i - 1, j - 1)) {
- neighbours++
- }
- }
- }
- if (j < 4) {
- if (led.point(i, j + 1)) {
- neighbours++
- }
- }
- if (j > 0) {
- if (led.point(i, j - 1)) {
- neighbours++
- }
- }
- }
- basic.forever(function () {
- if (init) {
- music.playTone(Note.F4, 50)
- RefreshScreen()
- if (smiley) {
- music.playTone(Note.F5, 50)
- basic.pause(1000)
- smiley = false
- }
- ConwayAlgo()
- basic.pause(300)
- if (cellCount === 0) {
- basic.pause(100)
- music.playTone(Note.C3, 50)
- basic.pause(100)
- music.playTone(Note.A3, 50)
- init = false;
- RefreshScreen()
- }
- }
- })
- input.onButtonPressed(Button.A, function () {
- if (!init) {
- map[tempY][tempX] = !map[tempY][tempX]
- RefreshScreen()
- music.playTone(Note.A4, 50)
- }
- })
- input.onButtonPressed(Button.B, function () {
- if (!init) {
- tempX += 1
- if (tempX > 4) {
- tempX = 0
- tempY += 1
- if (tempY > 4) {
- tempY = 0
- }
- }
- RefreshScreen()
- music.playTone(Note.C4, 50)
- }
- })
- input.onButtonPressed(Button.AB, function () {
- if (!smiley) {
- init = !init
- RefreshScreen()
- if(init) {
- music.playTone(Note.E5, 100)
- } else {
- music.playTone(Note.E3, 100)
- }
- }
- })
Advertisement