Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MAJOR TROUBLES:
- /*
- Radio was too basic, and the messages the micro:bit was sending were too advanced.
- To fix this issue, I assigned a value to every single LED by giving them a value that ranged from 1 to 25
- Top left being 1, bottom right being 25. By doing this, I can send a single value rather than a complicated string
- to change the connected player's location.
- The client would randomly be killed even though the connected player is more than 2 pixels away.
- This was a complicated issue, and hasn't even fully been resolved yet.
- The only way I could completly fix this issue is if I were to rewrite the code entirely, which I'd rather not do.
- So, I fixed as much as I could, and minimized the bug a lot.
- 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
- has died this variable sets itself to true, to not allow the player to die. And it remains true as the player respawns,
- and stays true until the player moves again, or idles for too long.
- Since both sprites, local player and connected player, looked exactly the same. It was always hard to tell which one
- was which.
- To solve this problem, I developed a simple animation to softly blink the local player's sprite (making it fade in and
- out)
- At first, it didn't go dim enough so it was impossible to notice any change in the MICRO:BIT itself.
- After minor ajustments, it looked pretty okay, and so I kept it.
- */
- // MINOR TROUBLES:
- /*
- When the integer grid system was just developed, the local player was able to no-clip through the pixel boundaries
- which allowed them to easily kill the connected player, while the connected player couldn't kill them. This bug was
- fixed using simple logic.
- It was once hard to tell when the connected player has been killed. To fix this, when the connected player dies, it
- sends a message through the radio, which is picked up by the client, which then knows the player has been killed.
- Then, it plays an explosion animation (that's what it looks like to me) at the position where the local player thinks
- the connected player had died. (I wasn't sure if I could send a complicated string over radio, so I didnt take any
- chances)
- */
- // DEFINING VARIABLES
- let posx = Math.randomRange(0, 4)
- let posy = Math.randomRange(0, 4)
- let radx = 2
- let rady = 2
- let controllock = false
- let antispam = false
- let rannum = Math.randomRange(0, 127)
- let deaths = 0
- let bright = 100
- let bright_on = true
- let can_die = true
- let reseter = false
- let connections = true
- radio.setGroup(20)
- radio.sendString("pljoi" + rannum.toString()) // announce to connected player that local player is joining.
- led.plot(posx, posy) // Plot the local character's randomized position
- //FUNCTIONS:
- function getPos() { // change a position to a interger
- let y = posy
- let x = posx
- return (posy * 5) + posx // equation to find the player's position
- }
- function blink(x: number, y: number) {
- for (let i = 0; i < 5; i++) { // blink the current position 10 times
- led.plot(x, y)
- basic.pause(150)
- led.unplot(x, y)
- basic.pause(150)
- }
- led.plot(x, y)
- }
- function GAME() {
- if (can_die == true) {
- can_die = false // disable local player's ability to die
- rady = -10 // make the connected player's position impossible (only on the client side)
- radx = -10
- controllock = true // turn off controls
- bright_on = false // turn off flashing animation
- wipescren() // begin the wipescreen animation
- deaths = deaths + 1 // add 1 to deaths
- basic.pause(100) // sleep for 100 miliseconds
- basic.showString(deaths.toString()) // show the local player their amount of deaths
- basic.pause(250) // sleep for another 250 miliseconds
- basic.showString("") // show nothing on screen
- posx = Math.randomRange(0, 4) // get a random number from 0, 4 (store it to local position)
- posy = Math.randomRange(0, 4)
- blink(posx, posy) // run the "blink" animation on the position that was just generated.
- controllock = false // turn controls back on
- bright_on = true // re-enable the flash animation
- radio.sendString(getPos().toString()) // send local player's position to connected player
- basic.pause(200) // sleep for 200 miliseconds to avoid being spawn killed.
- can_die = true // re-enable local player's ability to die.
- }
- }
- function wipescren() { // death screen animation
- // define variables
- let xxx = 0
- let yyy = 0
- // while "yyy" is not equal to 5, do (there are 5 boxes on the y axis; independent)
- while (yyy != 5) {
- basic.pause(20) // sleep for 20 miliseconds
- for (let i = 0; i < 5; i++) { // repeat 5 times (there are 5 boxes on the x axis; dependent)
- basic.pause(20) // sleep for 20 miliseconds
- led.plot(xxx, yyy) // plot the current variables
- xxx++ // add to "xxx" by 1
- }
- yyy = yyy + 1 // add to "yyy" by 1
- xxx = 0 // reset "xxx" back to 0 (new line)
- }
- xxx = 0 // reset both "xxx" and "yyy" back to 0 (taking away the plots using the same animation)
- yyy = 0
- while (yyy != 5) { // do the exact same thing all over again, to take away the plotted points.
- basic.pause(20)
- for (let i = 0; i < 5; i++) {
- basic.pause(20)
- led.unplot(xxx, yyy)
- xxx++
- }
- yyy = yyy + 1
- xxx = 0
- }
- }
- function changepos(x: number, y: number) {
- if (controllock == false) { // if controls are not locked due to local player death or a reboot, then
- led.unplot(posx, posy) //unplot the old local player's position.
- posx = posx + x
- posy = posy + y
- led.plotBrightness(posx, posy, bright) // plot a point of the player's new position depending on the flashing animation.
- radio.sendString(getPos().toString()) // send the local player's position to connected player.
- }
- }
- function radchangepos(pos: number) { // pos is an interger, we need to change it into a position. (connected player positioning)
- let x = pos % 5 // we can find X by simply getting the remainder of pos divided by 5.
- let y = Math.floor(pos / 5) // then we round downwards the division of pos divided by 5 to get the Y value.
- if (controllock == false) { // if controls are not locked due to local player death or a reboot, then
- if (radx == posx && rady == posy) { // if the local player has been killed, then
- let x1 = radx - x
- let y1 = rady - y
- let k = 0
- if (x1 < 0) { // if the number is negative, change it to positive (x - axis)
- x1 = x1 * -1
- }
- if (y1 < 0) { // if the number is negative, change it to positive (y - axis)
- y1 = y1 * 1
- }
- if (x1 > 1) { // if the connected player is more than 1 block away on the x axis, make K equal 1
- k = 1
- }
- if (y1 > 1) { // if the connected player is more than 1 block away on the y axis, add k by 1
- k = k + 1
- }
- if (k < 2) { // if the connected player is only 1 block away from the kill position, kill the player.
- radio.sendString("death")
- GAME()
- }
- led.unplot(radx, rady) // move the connected player's position
- radx = x
- rady = y
- led.plot(radx, rady)
- } else { // If the local player hasn't been killed, then
- led.unplot(radx, rady) // move the connected player's position
- radx = x
- rady = y
- led.plot(radx, rady)
- }
- }
- }
- function dAnim(x: number, y: number) {
- let xx1 = x // defining variables to animate in four directions
- let xx2 = x
- let yy1 = y
- let yy2 = y
- let boop = 0
- 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
- xx1 = xx1 + 1 // offset x by +1 (right)
- xx2 = xx2 - 1 // offset x by -1 (left)
- yy1 = yy1 + 1 // offset y by +1 (up)
- yy2 = yy2 - 1 // offset y by -1 (down)
- led.plotBrightness(xx1, y, 30) // plotting the offsets
- led.plotBrightness(xx2, y, 30)
- led.plotBrightness(x, yy1, 30)
- led.plotBrightness(x, yy2, 30)
- if (boop == 0) { // plot the 4 initial corners of the animation
- led.plotBrightness(xx1, y - 1, 30)
- led.plotBrightness(xx1, y + 1, 30)
- led.plotBrightness(xx2, y - 1, 30)
- led.plotBrightness(xx2, y + 1, 30)
- boop = 1
- }
- if (i == 4) { // if we're at the end of the loop, only pause for 10 miliseconds, otherwise wait 65 miliseconds
- basic.pause(10)
- } else {
- basic.pause(65)
- }
- }
- xx1 = x
- xx2 = x
- yy1 = y
- yy2 = y
- for (let i = 0; i < 5; i++) {
- xx1 = xx1 + 1
- xx2 = xx2 - 1
- yy1 = yy1 + 1
- yy2 = yy2 - 1
- led.unplot(xx1, y)
- led.unplot(xx2, y)
- led.unplot(x, yy1)
- led.unplot(x, yy2)
- if (boop == 1) {
- led.unplot(xx1, y - 1)
- led.unplot(xx1, y + 1)
- led.unplot(xx2, y - 1)
- led.unplot(xx2, y + 1)
- boop = 0
- }
- basic.pause(65)
- }
- }
- // INPUTS:
- input.onButtonPressed(Button.A, function () { // press A to go left, cannot go past grid.
- if (posx != 0) {
- changepos(-1, 0)
- can_die = true
- }
- })
- input.onButtonPressed(Button.B, function () { // press B to go right, cannot go past grid.
- if (posx != 4) {
- changepos(1, 0)
- can_die = true
- }
- })
- input.onButtonPressed(Button.AB, function () { // press both A and B to go up, cannot go past grid.
- if (posy != 0) {
- changepos(0, -1)
- can_die = true
- }
- })
- input.onGesture(Gesture.Shake, function () { // shake the MICRO:BIT to go down, cannot go past grid.
- if (posy != 4) {
- changepos(0, 1)
- can_die = true
- }
- })
- input.onGesture(Gesture.ScreenDown, function () { // once the MICRO:BIT detects it's facing downwards; execute reset()
- reset()
- })
- //RADIO
- radio.onReceivedString(function (receivedString: string) {
- if (connections == true) {
- if (receivedString.substr(0, 5) == "pljoi") { // If a player is joining..
- let kek: number = parseInt(receivedString.substr(5)) // require the position of the newly joined player.
- radio.sendString(getPos().toString()) // send the local player's position through radio.
- } else if (receivedString == "death") {
- dAnim(radx, rady)
- } else { // if a player isn't joining, then the radio message must be a position
- let xx = parseInt(receivedString)
- radchangepos(xx) // find the position of the player
- }
- }
- })
- basic.pause(20)
- radio.sendString(getPos().toString())
- function reset() { // if the MICRO:BIT has bugged, flip it over to have the screen faced down, and it will reboot.
- if (reseter == false) {
- reseter = true
- basic.pause(5000) // wait 5 seconds
- if (input.isGesture(Gesture.ScreenDown)) { // if the screen is still down, reset the MICRO:BIT
- connections = false
- can_die = false
- bright_on = false
- controllock = true
- animate()
- }
- reseter = false
- }
- }
- //SECONDARY FUNCTIONS
- function animate() {
- wipescren()
- for (let i = 0; i < 10; i++) { // A snake animation that was poorly built; looks like a loading screen.
- led.plotBrightness(2, 1, 255 * 7 / 7)
- led.plotBrightness(1, 1, 255 * 6 / 7)
- led.plotBrightness(1, 2, 255 * 5 / 7)
- led.plotBrightness(1, 3, 255 * 4 / 7)
- led.plotBrightness(2, 3, 255 * 3 / 7)
- led.plotBrightness(3, 3, 255 * 2 / 7)
- led.plotBrightness(3, 2, 255 * 1 / 7)
- led.plotBrightness(3, 1, 255 * 0 / 7)
- basic.pause(100)
- led.plotBrightness(2, 1, 255 * 6 / 7)
- led.plotBrightness(1, 1, 255 * 5 / 7)
- led.plotBrightness(1, 2, 255 * 4 / 7)
- led.plotBrightness(1, 3, 255 * 3 / 7)
- led.plotBrightness(2, 3, 255 * 2 / 7)
- led.plotBrightness(3, 3, 255 * 1 / 7)
- led.plotBrightness(3, 2, 255 * 0 / 7)
- led.plotBrightness(3, 1, 255 * 7 / 7)
- basic.pause(100)
- led.plotBrightness(2, 1, 255 * 5 / 7)
- led.plotBrightness(1, 1, 255 * 4 / 7)
- led.plotBrightness(1, 2, 255 * 3 / 7)
- led.plotBrightness(1, 3, 255 * 2 / 7)
- led.plotBrightness(2, 3, 255 * 1 / 7)
- led.plotBrightness(3, 3, 255 * 0 / 7)
- led.plotBrightness(3, 2, 255 * 7 / 7)
- led.plotBrightness(3, 1, 255 * 6 / 7)
- basic.pause(100)
- led.plotBrightness(2, 1, 255 * 4 / 7)
- led.plotBrightness(1, 1, 255 * 3 / 7)
- led.plotBrightness(1, 2, 255 * 2 / 7)
- led.plotBrightness(1, 3, 255 * 1 / 7)
- led.plotBrightness(2, 3, 255 * 0 / 7)
- led.plotBrightness(3, 3, 255 * 7 / 7)
- led.plotBrightness(3, 2, 255 * 6 / 7)
- led.plotBrightness(3, 1, 255 * 5 / 7)
- basic.pause(100)
- led.plotBrightness(2, 1, 255 * 3 / 7)
- led.plotBrightness(1, 1, 255 * 2 / 7)
- led.plotBrightness(1, 2, 255 * 1 / 7)
- led.plotBrightness(1, 3, 255 * 0 / 7)
- led.plotBrightness(2, 3, 255 * 7 / 7)
- led.plotBrightness(3, 3, 255 * 6 / 7)
- led.plotBrightness(3, 2, 255 * 5 / 7)
- led.plotBrightness(3, 1, 255 * 4 / 7)
- basic.pause(100)
- led.plotBrightness(2, 1, 255 * 2 / 7)
- led.plotBrightness(1, 1, 255 * 1 / 7)
- led.plotBrightness(1, 2, 255 * 0 / 7)
- led.plotBrightness(1, 3, 255 * 7 / 7)
- led.plotBrightness(2, 3, 255 * 6 / 7)
- led.plotBrightness(3, 3, 255 * 5 / 7)
- led.plotBrightness(3, 2, 255 * 4 / 7)
- led.plotBrightness(3, 1, 255 * 3 / 7)
- basic.pause(100)
- led.plotBrightness(2, 1, 255 * 1 / 7)
- led.plotBrightness(1, 1, 255 * 0 / 7)
- led.plotBrightness(1, 2, 255 * 7 / 7)
- led.plotBrightness(1, 3, 255 * 6 / 7)
- led.plotBrightness(2, 3, 255 * 5 / 7)
- led.plotBrightness(3, 3, 255 * 4 / 7)
- led.plotBrightness(3, 2, 255 * 3 / 7)
- led.plotBrightness(3, 1, 255 * 2 / 7)
- basic.pause(100)
- led.plotBrightness(2, 1, 255 * 0 / 7)
- led.plotBrightness(1, 1, 255 * 7 / 7)
- led.plotBrightness(1, 2, 255 * 6 / 7)
- led.plotBrightness(1, 3, 255 * 5 / 7)
- led.plotBrightness(2, 3, 255 * 4 / 7)
- led.plotBrightness(3, 3, 255 * 3 / 7)
- led.plotBrightness(3, 2, 255 * 2 / 7)
- led.plotBrightness(3, 1, 255 * 1 / 7)
- basic.pause(100)
- }
- control.reset()
- }
- while (true) {
- bright = 100
- for (let i = 0; i < 155; i++) { // flashing of the local player's sprite.
- if (bright_on == true) {
- bright = bright + 1
- led.plotBrightness(posx, posy, bright)
- }
- basic.pause(3)
- }
- for (let i = 0; i < 155; i++) {
- if (bright_on == true) {
- bright = bright - 1
- led.plotBrightness(posx, posy, bright)
- }
- basic.pause(3)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement