Advertisement
BobMe

micro bit

Feb 13th, 2020
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.41 KB | None | 0 0
  1. // MAJOR TROUBLES:
  2. /*
  3. Radio was too basic, and the messages the micro:bit was sending were too advanced.
  4. To fix this issue, I assigned a value to every single LED by giving them a value that ranged from 1 to 25
  5. Top left being 1, bottom right being 25. By doing this, I can send a single value rather than a complicated string
  6. to change the connected player's location.
  7.  
  8. The client would randomly be killed even though the connected player is more than 2 pixels away.
  9. This was a complicated issue, and hasn't even fully been resolved yet.
  10. The only way I could completly fix this issue is if I were to rewrite the code entirely, which I'd rather not do.
  11. So, I fixed as much as I could, and minimized the bug a lot.
  12. To fix the issue, I allowed a variable to dictate whether or not the player is able to die or not, and when the player
  13. has died this variable sets itself to true, to not allow the player to die. And it remains true as the player respawns,
  14. and stays true until the player moves again, or idles for too long.
  15.  
  16.  
  17. Since both sprites, local player and connected player, looked exactly the same. It was always hard to tell which one
  18. was which.
  19. To solve this problem, I developed a simple animation to softly blink the local player's sprite (making it fade in and
  20. out)
  21. At first, it didn't go dim enough so it was impossible to notice any change in the MICRO:BIT itself.
  22. After minor ajustments, it looked pretty okay, and so I kept it.
  23. */
  24.  
  25. // MINOR TROUBLES:
  26. /*
  27. When the integer grid system was just developed, the local player was able to no-clip through the pixel boundaries
  28. which allowed them to easily kill the connected player, while the connected player couldn't kill them. This bug was
  29. fixed using simple logic.
  30.  
  31. It was once hard to tell when the connected player has been killed. To fix this, when the connected player dies, it
  32. sends a message through the radio, which is picked up by the client, which then knows the player has been killed.
  33. Then, it plays an explosion animation (that's what it looks like to me) at the position where the local player thinks
  34. the connected player had died. (I wasn't sure if I could send a complicated string over radio, so I didnt take any
  35. chances)
  36.  
  37.  
  38. */
  39.  
  40.  
  41.  
  42.  
  43. // DEFINING VARIABLES
  44. let posx = Math.randomRange(0, 4)
  45. let posy = Math.randomRange(0, 4)
  46. let radx = 2
  47. let rady = 2
  48. let controllock = false
  49. let antispam = false
  50. let rannum = Math.randomRange(0, 127)
  51. let deaths = 0
  52. let bright = 100
  53. let bright_on = true
  54. let can_die = true
  55. let reseter = false
  56. let connections = true
  57. radio.setGroup(20)
  58. radio.sendString("pljoi" + rannum.toString()) // announce to connected player that local player is joining.
  59.  
  60. led.plot(posx, posy) // Plot the local character's randomized position
  61.  
  62. //FUNCTIONS:
  63. function getPos() { // change a position to a interger
  64. let y = posy
  65. let x = posx
  66. return (posy * 5) + posx // equation to find the player's position
  67. }
  68.  
  69. function blink(x: number, y: number) {
  70. for (let i = 0; i < 5; i++) { // blink the current position 10 times
  71. led.plot(x, y)
  72. basic.pause(150)
  73. led.unplot(x, y)
  74. basic.pause(150)
  75. }
  76. led.plot(x, y)
  77. }
  78.  
  79. function GAME() {
  80. if (can_die == true) {
  81. can_die = false // disable local player's ability to die
  82. rady = -10 // make the connected player's position impossible (only on the client side)
  83. radx = -10
  84. controllock = true // turn off controls
  85. bright_on = false // turn off flashing animation
  86. wipescren() // begin the wipescreen animation
  87. deaths = deaths + 1 // add 1 to deaths
  88. basic.pause(100) // sleep for 100 miliseconds
  89. basic.showString(deaths.toString()) // show the local player their amount of deaths
  90. basic.pause(250) // sleep for another 250 miliseconds
  91. basic.showString("") // show nothing on screen
  92. posx = Math.randomRange(0, 4) // get a random number from 0, 4 (store it to local position)
  93. posy = Math.randomRange(0, 4)
  94. blink(posx, posy) // run the "blink" animation on the position that was just generated.
  95. controllock = false // turn controls back on
  96. bright_on = true // re-enable the flash animation
  97. radio.sendString(getPos().toString()) // send local player's position to connected player
  98. basic.pause(200) // sleep for 200 miliseconds to avoid being spawn killed.
  99. can_die = true // re-enable local player's ability to die.
  100. }
  101. }
  102.  
  103. function wipescren() { // death screen animation
  104. // define variables
  105. let xxx = 0
  106. let yyy = 0
  107. // while "yyy" is not equal to 5, do (there are 5 boxes on the y axis; independent)
  108. while (yyy != 5) {
  109. basic.pause(20) // sleep for 20 miliseconds
  110. for (let i = 0; i < 5; i++) { // repeat 5 times (there are 5 boxes on the x axis; dependent)
  111. basic.pause(20) // sleep for 20 miliseconds
  112. led.plot(xxx, yyy) // plot the current variables
  113. xxx++ // add to "xxx" by 1
  114. }
  115. yyy = yyy + 1 // add to "yyy" by 1
  116. xxx = 0 // reset "xxx" back to 0 (new line)
  117. }
  118. xxx = 0 // reset both "xxx" and "yyy" back to 0 (taking away the plots using the same animation)
  119. yyy = 0
  120. while (yyy != 5) { // do the exact same thing all over again, to take away the plotted points.
  121. basic.pause(20)
  122. for (let i = 0; i < 5; i++) {
  123. basic.pause(20)
  124. led.unplot(xxx, yyy)
  125. xxx++
  126. }
  127. yyy = yyy + 1
  128. xxx = 0
  129. }
  130. }
  131.  
  132. function changepos(x: number, y: number) {
  133. if (controllock == false) { // if controls are not locked due to local player death or a reboot, then
  134. led.unplot(posx, posy) //unplot the old local player's position.
  135. posx = posx + x
  136. posy = posy + y
  137. led.plotBrightness(posx, posy, bright) // plot a point of the player's new position depending on the flashing animation.
  138. radio.sendString(getPos().toString()) // send the local player's position to connected player.
  139. }
  140. }
  141.  
  142.  
  143. function radchangepos(pos: number) { // pos is an interger, we need to change it into a position. (connected player positioning)
  144. let x = pos % 5 // we can find X by simply getting the remainder of pos divided by 5.
  145. let y = Math.floor(pos / 5) // then we round downwards the division of pos divided by 5 to get the Y value.
  146. if (controllock == false) { // if controls are not locked due to local player death or a reboot, then
  147. if (radx == posx && rady == posy) { // if the local player has been killed, then
  148. let x1 = radx - x
  149. let y1 = rady - y
  150. let k = 0
  151. if (x1 < 0) { // if the number is negative, change it to positive (x - axis)
  152. x1 = x1 * -1
  153. }
  154. if (y1 < 0) { // if the number is negative, change it to positive (y - axis)
  155. y1 = y1 * 1
  156. }
  157. if (x1 > 1) { // if the connected player is more than 1 block away on the x axis, make K equal 1
  158. k = 1
  159. }
  160. if (y1 > 1) { // if the connected player is more than 1 block away on the y axis, add k by 1
  161. k = k + 1
  162. }
  163. if (k < 2) { // if the connected player is only 1 block away from the kill position, kill the player.
  164. radio.sendString("death")
  165. GAME()
  166. }
  167. led.unplot(radx, rady) // move the connected player's position
  168. radx = x
  169. rady = y
  170. led.plot(radx, rady)
  171. } else { // If the local player hasn't been killed, then
  172. led.unplot(radx, rady) // move the connected player's position
  173. radx = x
  174. rady = y
  175. led.plot(radx, rady)
  176. }
  177. }
  178. }
  179.  
  180. function dAnim(x: number, y: number) {
  181. let xx1 = x // defining variables to animate in four directions
  182. let xx2 = x
  183. let yy1 = y
  184. let yy2 = y
  185. let boop = 0
  186. for (let i = 0; i < 5; i++) { // loop 5 times due to there being a maximum of 5 pixels in between the sprite, and the edge
  187. xx1 = xx1 + 1 // offset x by +1 (right)
  188. xx2 = xx2 - 1 // offset x by -1 (left)
  189. yy1 = yy1 + 1 // offset y by +1 (up)
  190. yy2 = yy2 - 1 // offset y by -1 (down)
  191. led.plotBrightness(xx1, y, 30) // plotting the offsets
  192. led.plotBrightness(xx2, y, 30)
  193. led.plotBrightness(x, yy1, 30)
  194. led.plotBrightness(x, yy2, 30)
  195. if (boop == 0) { // plot the 4 initial corners of the animation
  196. led.plotBrightness(xx1, y - 1, 30)
  197. led.plotBrightness(xx1, y + 1, 30)
  198. led.plotBrightness(xx2, y - 1, 30)
  199. led.plotBrightness(xx2, y + 1, 30)
  200. boop = 1
  201. }
  202. if (i == 4) { // if we're at the end of the loop, only pause for 10 miliseconds, otherwise wait 65 miliseconds
  203. basic.pause(10)
  204. } else {
  205. basic.pause(65)
  206. }
  207. }
  208. xx1 = x
  209. xx2 = x
  210. yy1 = y
  211. yy2 = y
  212. for (let i = 0; i < 5; i++) {
  213. xx1 = xx1 + 1
  214. xx2 = xx2 - 1
  215. yy1 = yy1 + 1
  216. yy2 = yy2 - 1
  217. led.unplot(xx1, y)
  218. led.unplot(xx2, y)
  219. led.unplot(x, yy1)
  220. led.unplot(x, yy2)
  221. if (boop == 1) {
  222. led.unplot(xx1, y - 1)
  223. led.unplot(xx1, y + 1)
  224. led.unplot(xx2, y - 1)
  225. led.unplot(xx2, y + 1)
  226. boop = 0
  227. }
  228. basic.pause(65)
  229. }
  230. }
  231.  
  232. // INPUTS:
  233. input.onButtonPressed(Button.A, function () { // press A to go left, cannot go past grid.
  234. if (posx != 0) {
  235. changepos(-1, 0)
  236. can_die = true
  237. }
  238. })
  239. input.onButtonPressed(Button.B, function () { // press B to go right, cannot go past grid.
  240. if (posx != 4) {
  241. changepos(1, 0)
  242. can_die = true
  243. }
  244. })
  245. input.onButtonPressed(Button.AB, function () { // press both A and B to go up, cannot go past grid.
  246. if (posy != 0) {
  247. changepos(0, -1)
  248. can_die = true
  249. }
  250. })
  251. input.onGesture(Gesture.Shake, function () { // shake the MICRO:BIT to go down, cannot go past grid.
  252. if (posy != 4) {
  253. changepos(0, 1)
  254. can_die = true
  255. }
  256. })
  257. input.onGesture(Gesture.ScreenDown, function () { // once the MICRO:BIT detects it's facing downwards; execute reset()
  258. reset()
  259. })
  260.  
  261. //RADIO
  262. radio.onReceivedString(function (receivedString: string) {
  263. if (connections == true) {
  264. if (receivedString.substr(0, 5) == "pljoi") { // If a player is joining..
  265. let kek: number = parseInt(receivedString.substr(5)) // require the position of the newly joined player.
  266. radio.sendString(getPos().toString()) // send the local player's position through radio.
  267. } else if (receivedString == "death") {
  268. dAnim(radx, rady)
  269. } else { // if a player isn't joining, then the radio message must be a position
  270. let xx = parseInt(receivedString)
  271. radchangepos(xx) // find the position of the player
  272. }
  273. }
  274. })
  275. basic.pause(20)
  276. radio.sendString(getPos().toString())
  277.  
  278. function reset() { // if the MICRO:BIT has bugged, flip it over to have the screen faced down, and it will reboot.
  279. if (reseter == false) {
  280. reseter = true
  281. basic.pause(5000) // wait 5 seconds
  282. if (input.isGesture(Gesture.ScreenDown)) { // if the screen is still down, reset the MICRO:BIT
  283. connections = false
  284. can_die = false
  285. bright_on = false
  286. controllock = true
  287. animate()
  288. }
  289. reseter = false
  290. }
  291. }
  292. //SECONDARY FUNCTIONS
  293. function animate() {
  294. wipescren()
  295. for (let i = 0; i < 10; i++) { // A snake animation that was poorly built; looks like a loading screen.
  296. led.plotBrightness(2, 1, 255 * 7 / 7)
  297. led.plotBrightness(1, 1, 255 * 6 / 7)
  298. led.plotBrightness(1, 2, 255 * 5 / 7)
  299. led.plotBrightness(1, 3, 255 * 4 / 7)
  300. led.plotBrightness(2, 3, 255 * 3 / 7)
  301. led.plotBrightness(3, 3, 255 * 2 / 7)
  302. led.plotBrightness(3, 2, 255 * 1 / 7)
  303. led.plotBrightness(3, 1, 255 * 0 / 7)
  304. basic.pause(100)
  305. led.plotBrightness(2, 1, 255 * 6 / 7)
  306. led.plotBrightness(1, 1, 255 * 5 / 7)
  307. led.plotBrightness(1, 2, 255 * 4 / 7)
  308. led.plotBrightness(1, 3, 255 * 3 / 7)
  309. led.plotBrightness(2, 3, 255 * 2 / 7)
  310. led.plotBrightness(3, 3, 255 * 1 / 7)
  311. led.plotBrightness(3, 2, 255 * 0 / 7)
  312. led.plotBrightness(3, 1, 255 * 7 / 7)
  313. basic.pause(100)
  314. led.plotBrightness(2, 1, 255 * 5 / 7)
  315. led.plotBrightness(1, 1, 255 * 4 / 7)
  316. led.plotBrightness(1, 2, 255 * 3 / 7)
  317. led.plotBrightness(1, 3, 255 * 2 / 7)
  318. led.plotBrightness(2, 3, 255 * 1 / 7)
  319. led.plotBrightness(3, 3, 255 * 0 / 7)
  320. led.plotBrightness(3, 2, 255 * 7 / 7)
  321. led.plotBrightness(3, 1, 255 * 6 / 7)
  322. basic.pause(100)
  323. led.plotBrightness(2, 1, 255 * 4 / 7)
  324. led.plotBrightness(1, 1, 255 * 3 / 7)
  325. led.plotBrightness(1, 2, 255 * 2 / 7)
  326. led.plotBrightness(1, 3, 255 * 1 / 7)
  327. led.plotBrightness(2, 3, 255 * 0 / 7)
  328. led.plotBrightness(3, 3, 255 * 7 / 7)
  329. led.plotBrightness(3, 2, 255 * 6 / 7)
  330. led.plotBrightness(3, 1, 255 * 5 / 7)
  331. basic.pause(100)
  332. led.plotBrightness(2, 1, 255 * 3 / 7)
  333. led.plotBrightness(1, 1, 255 * 2 / 7)
  334. led.plotBrightness(1, 2, 255 * 1 / 7)
  335. led.plotBrightness(1, 3, 255 * 0 / 7)
  336. led.plotBrightness(2, 3, 255 * 7 / 7)
  337. led.plotBrightness(3, 3, 255 * 6 / 7)
  338. led.plotBrightness(3, 2, 255 * 5 / 7)
  339. led.plotBrightness(3, 1, 255 * 4 / 7)
  340. basic.pause(100)
  341. led.plotBrightness(2, 1, 255 * 2 / 7)
  342. led.plotBrightness(1, 1, 255 * 1 / 7)
  343. led.plotBrightness(1, 2, 255 * 0 / 7)
  344. led.plotBrightness(1, 3, 255 * 7 / 7)
  345. led.plotBrightness(2, 3, 255 * 6 / 7)
  346. led.plotBrightness(3, 3, 255 * 5 / 7)
  347. led.plotBrightness(3, 2, 255 * 4 / 7)
  348. led.plotBrightness(3, 1, 255 * 3 / 7)
  349. basic.pause(100)
  350. led.plotBrightness(2, 1, 255 * 1 / 7)
  351. led.plotBrightness(1, 1, 255 * 0 / 7)
  352. led.plotBrightness(1, 2, 255 * 7 / 7)
  353. led.plotBrightness(1, 3, 255 * 6 / 7)
  354. led.plotBrightness(2, 3, 255 * 5 / 7)
  355. led.plotBrightness(3, 3, 255 * 4 / 7)
  356. led.plotBrightness(3, 2, 255 * 3 / 7)
  357. led.plotBrightness(3, 1, 255 * 2 / 7)
  358. basic.pause(100)
  359. led.plotBrightness(2, 1, 255 * 0 / 7)
  360. led.plotBrightness(1, 1, 255 * 7 / 7)
  361. led.plotBrightness(1, 2, 255 * 6 / 7)
  362. led.plotBrightness(1, 3, 255 * 5 / 7)
  363. led.plotBrightness(2, 3, 255 * 4 / 7)
  364. led.plotBrightness(3, 3, 255 * 3 / 7)
  365. led.plotBrightness(3, 2, 255 * 2 / 7)
  366. led.plotBrightness(3, 1, 255 * 1 / 7)
  367. basic.pause(100)
  368. }
  369. control.reset()
  370. }
  371.  
  372. while (true) {
  373. bright = 100
  374. for (let i = 0; i < 155; i++) { // flashing of the local player's sprite.
  375. if (bright_on == true) {
  376. bright = bright + 1
  377. led.plotBrightness(posx, posy, bright)
  378. }
  379. basic.pause(3)
  380. }
  381. for (let i = 0; i < 155; i++) {
  382. if (bright_on == true) {
  383. bright = bright - 1
  384. led.plotBrightness(posx, posy, bright)
  385. }
  386. basic.pause(3)
  387. }
  388. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement