Advertisement
GaryScripts

micro:bit Collectathon Rush Version 1

Oct 12th, 2021
1,257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Collectathon Rush:
  3.  
  4. FOR SIMULATED PLAY
  5. Just run in your browser sandbox! Simple does it.
  6.  
  7. FOR PHYSICAL PLAY ON REAL HARDWARE
  8. You need a working micro:bit with a Micro-USB Connection, two alligator clips, and a buzzer that can play sound effects.
  9. *optional: battery pack*
  10.  
  11. Links to purchase them across different platforms:
  12. Buzzer: https://www.adafruit.com/product/1536?gclid=Cj0KCQjw5JSLBhCxARIsAHgO2SfBh41s9bN6mcovQsB6yxvBV8Bfoq8y0ClzVbHU1YThy01veJggZjwaAlmZEALw_wcB
  13. micro:bit: https://microbit.org/buy
  14. Alligator Clips: https://www.adafruit.com/product/1008
  15.  
  16. CONNECTING EVERYTHING UP:
  17. Buzzer: Take two alligator clips. Clip one of them to P0. Take the other one and connect it to the GND pin. Afterwards, take GND and connect it to the minus wire, and take P0 and connect it to the positive wire.
  18. micro:bit: Do I have to explain this one? You all already know how to do it.
  19.  
  20. DOWNLOADING SOFTWARE
  21. Connect your micro:bit up to your computer with a Micro-USB cable, connect the micro:bit to your browser, and download the software onto it!
  22.  
  23. ADDITIONAL INFO:
  24. This game is cross compatible with V1 and V2 micro:bit machines!
  25.  
  26. CONTROLS:
  27. A to move horizontally.
  28. B to move vertically.
  29. A+B to sweep across the board in either vertical, or horizontal direction. (somewhat broken, will fix soon enough)
  30. */
  31.  
  32. let player = game.createSprite(2, 2)
  33. let collectable = game.createSprite(randint(0, 4), randint(0, 4))
  34. let isMovingVertically = false //you need this, it's important ;)
  35. let sweepTime = 5000 //in pause time
  36. let playerX = 2;
  37. let playerY = 2;
  38. let movingOpposite = false
  39.  
  40. input.onButtonPressed(Button.A, () => {
  41.     if (isMovingVertically == true) {
  42.         player.turnLeft(90)
  43.         isMovingVertically = false
  44.  
  45.         if (player.isTouchingEdge()) {
  46.             if (movingOpposite == false) {
  47.                 movingOpposite = true
  48.             } else {
  49.                 movingOpposite = false
  50.             }
  51.         }
  52.  
  53.         if (movingOpposite == true) {
  54.             player.ifOnEdgeBounce()
  55.             player.move(1)
  56.             music.playTone(Note.B, 25)
  57.  
  58.             playerX--
  59.         } else {
  60.             player.ifOnEdgeBounce()
  61.             player.move(1)
  62.             music.playTone(Note.B, 25)
  63.  
  64.             playerX++
  65.         }
  66.     } else {
  67.  
  68.         if (player.isTouchingEdge()) {
  69.             if (movingOpposite == false) {
  70.                 movingOpposite = true
  71.             } else {
  72.                 movingOpposite = false
  73.             }
  74.         }
  75.  
  76.         if (movingOpposite == true) {
  77.             player.ifOnEdgeBounce()
  78.             player.move(1)
  79.             music.playTone(Note.B, 25)
  80.  
  81.             playerX--
  82.         } else {
  83.             player.ifOnEdgeBounce()
  84.             player.move(1)
  85.             music.playTone(Note.B, 25)
  86.  
  87.             playerX++
  88.         }
  89.     }
  90. })
  91.  
  92. input.onButtonPressed(Button.B, () => {
  93.     if (isMovingVertically == false) {
  94.         player.turnRight(90)
  95.         isMovingVertically = true
  96.  
  97.         if (player.isTouchingEdge()) {
  98.             if (movingOpposite == false) {
  99.                 movingOpposite = true
  100.             } else {
  101.                 movingOpposite = false
  102.             }
  103.         }
  104.  
  105.         if (movingOpposite == true) {
  106.             player.ifOnEdgeBounce()
  107.             player.move(1)
  108.             music.playTone(Note.B, 25)
  109.  
  110.             playerY--
  111.         } else {
  112.             player.ifOnEdgeBounce()
  113.             player.move(1)
  114.             music.playTone(Note.B, 25)
  115.  
  116.             playerY++
  117.         }
  118.     } else {
  119.  
  120.         if (player.isTouchingEdge()) {
  121.             if (movingOpposite == false) {
  122.                 movingOpposite = true
  123.             } else {
  124.                 movingOpposite = false
  125.             }
  126.         }
  127.  
  128.         if (movingOpposite == true) {
  129.             player.ifOnEdgeBounce()
  130.             player.move(1)
  131.             music.playTone(Note.B, 25)
  132.  
  133.             playerY--
  134.         } else {
  135.             player.ifOnEdgeBounce()
  136.             player.move(1)
  137.             music.playTone(Note.B, 25)
  138.  
  139.             playerY++
  140.         }
  141.     }
  142. })
  143.  
  144. input.onButtonPressed(Button.AB, () => {
  145.     if (isMovingVertically == true && sweepTime == 5000) {
  146.         if (movingOpposite == true) {
  147.             player.goTo(playerX, 0)
  148.         } else {
  149.             player.goTo(playerX, 4)
  150.         }
  151.  
  152.     } else if (sweepTime == 5000) {
  153.         if (movingOpposite == true) {
  154.             player.goTo(4, playerY)
  155.         } else {
  156.             player.goTo(0, playerY)
  157.         }
  158.  
  159.     }
  160. })
  161.  
  162. basic.forever(() => {
  163.     if (player.isTouching(collectable)) {
  164.         game.addScore(1)
  165.         collectable.goTo(randint(0, 4), randint(0, 4))
  166.  
  167.         music.playTone(Note.FSharp5, 100);
  168.     }
  169. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement