Advertisement
jbn6972

CS5041-P1 John code

Oct 4th, 2023 (edited)
838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Primary micro-bit code
  2.  
  3. let lifes = 0
  4. let vals: string[] = []
  5. let X = 0
  6. let Y = 0
  7.  
  8. radio.setGroup(18)
  9. serial.setTxBufferSize(128)
  10. serial.setRxBufferSize(128)
  11. let values: { [key: string]: string } = {};
  12.  
  13.  
  14. control.inBackground(()=> {
  15.     serial.onDataReceived("d:g", function () {
  16.         vals = serial.readBuffer(75).toString().split('\n')
  17.         vals.forEach(pair => {
  18.             const [key, value] = pair.split(':');
  19.             values[key] = value;
  20.         });
  21.         if (values.d != undefined) {
  22.             radio.sendString(values.d.charAt(0))
  23.         }
  24.  
  25.         if (values.l != undefined) {
  26.             basic.showString(values.l)
  27.         }
  28.     })
  29. });
  30.  
  31.  
  32. radio.onReceivedString(function (control) {
  33.     // basic.showString(control);
  34.     serial.writeLine(control)
  35. });
  36.  
  37.  
  38. function getGameState(){
  39.     return values.d;
  40. }
  41.  
  42. basic.forever(()=>{
  43.     if (getGameState() === 'g') {
  44.         controlGame(values);
  45.     }
  46. })
  47.  
  48. function controlGame (vals:{[key:string] : string}) {
  49.  
  50.     X = input.acceleration(Dimension.X)
  51.     Y = input.acceleration(Dimension.Y)
  52.     if (Y > 250) {
  53.         thrustOn()
  54.     } else {
  55.         thrustOff()
  56.     }
  57.     if (X < -300) {
  58.         // left
  59.         rotateLeft()
  60.     } else if (X > 300) {
  61.         // right
  62.         rotateRight()
  63.     } else {
  64.         halt()
  65.     }
  66.     if (input.buttonIsPressed(Button.A)) {
  67.         serial.writeLine("a:1")
  68.     } else {
  69.         serial.writeLine("a:0")
  70.     }
  71.     if (input.buttonIsPressed(Button.B)) {
  72.         serial.writeLine("b:1")
  73.     } else {
  74.         serial.writeLine("b:0")
  75.     }
  76. }
  77.  
  78. function thrustOn() {
  79.     serial.writeLine("t:-1")
  80. }
  81.  
  82. function thrustOff() {
  83.     serial.writeLine("t:0")
  84. }
  85.  
  86.  
  87. function rotateLeft() {
  88.     serial.writeLine("l:1")
  89.     serial.writeLine("r:0")
  90. }
  91. function halt() {
  92.     serial.writeLine("l:0")
  93.     serial.writeLine("r:0")
  94.     serial.writeLine("t:0")
  95. }
  96. function rotateRight() {
  97.     serial.writeLine("l:0")
  98.     serial.writeLine("r:1")
  99. }
  100.  
  101.  
  102. //Secondary micro-bit code
  103. radio.setGroup(18);
  104. radio.onReceivedNumber((number)=>{
  105.     basic.showNumber(number)
  106. });
  107.  
  108. radio.onReceivedString((value)=>{
  109.     basic.showString(value);
  110. })
  111.  
  112. let paused = false;
  113. let shield = false;
  114.  
  115. basic.forever(()=>{
  116.     if(input.buttonIsPressed(Button.A)){
  117.         radio.sendString("s:1")
  118.     }else{
  119.         radio.sendString("s:0")
  120.     }
  121.     if(input.buttonIsPressed(Button.B)){
  122.         radio.sendString("u:1")
  123.     }else{
  124.         radio.sendString("u:0")
  125.     }
  126. })
  127.  
  128.  
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement