Pixel Fighter Xtreme

by 0427421 in Circuits > Microcontrollers

249 Views, 1 Favorites, 0 Comments

Pixel Fighter Xtreme

FZ3081SKVNTJT2N.png

In this Instructable we’ll walk you through step by step in making your own 1-bit street fighter-esque game that runs on a microbit. All you will need is two microbits (ideally with battery packs if you don’t have a way to power it otherwise), A MicroUSB cable, and a PC.

Supplies

-2 Microbits

-2 Battery packs (optional, but recommended so you don't have to keep the micro:bit plugged into your PC)

-1 MicroUSB cable (2 if you don't have battery packs)

-A PC

-Access to Microsoft MakeCode

-A friend to play with

How to Construct It Using Makecode

Screenshot (808).png

There are many programs online that allow you to export your code as ‘.hex’ files (what microbits use as their software) but by far the best one for casual use is Microsoft’s MakeCode. Navigate to https://makecode.microbit.org/ and open a new project. With MakeCode you can code in either blocks or Javascript, this tutorial will use blocks for ease of access. Once you’ve completed this Instructable and have a completed codebase, simply download your code as a .hex file and transfer it to both microbits via a MicroUSB cable. Simply drag and drop the file into the microbit as you would a storage USB.

Configuring Radio

Microbits communicate via radio, which means you will need to set up some radio specific code unless you want your game to be a lot more one-sided than you initially envisaged. Sending commands to other microbits is thankfully quite easy, it only consists of setting up a radio group and using the send number blocks. It doesn't matter which group you pick as both micro bits will have the same code and thus join the same signalling group. The only reason why you’d pick a specific one is other microbits in the same area using the same radio group and thus confusing each other. You will also need to add a function that stores the number received through the radio and checks against the possible actions the other microbit may want it to take. In our case we added a simple startup flash of the LEDs to confirm that both microbits are on, running the code correctly, and sending and receiving radio signals.

Game Setup

Screenshot 2021-11-07 at 21-40-59 Microsoft MakeCode for micro bit.png
Screenshot (810).png
Screenshot 2021-11-07 at 14-55-57 Microsoft MakeCode for micro bit.png

To create the player characters, use the “create sprite” block to create 2 sprites either side of the center of the display (that is, at (1,2) and (3,2)). Two LEDs on the display should light up as shown. Put the player being controlled by that micro:bit on one particular side and the opponent on the other (this should be the case for both players, which will be explained more in Step 5). You don’t need to create a variable for lives as micro:bit comes with one in the “Game” section. In the example we set it to 5, but you're free to chose the game length that feels right to you.

Movement

Screenshot 2021-11-07 at 21-39-28 Microsoft MakeCode for micro bit.png
Screenshot 2021-11-07 at 22-01-36 Microsoft MakeCode for micro bit.png

Movement comes down to using button inputs to add or subtract from each player’s x value; it is important to note that computers count from 0 and not from 1, meaning x=1 is the second position. Recall that both microbits are running the exact same code, so without some absolute way of telling which microbit the code is being run on there is no way to make the players view themselves as on different sides. It is much simpler and more effective to place the player on one side and create a ‘ghost’ on the other side. Whenever an input is pressed simply move your sprite and send a designated number over the radio, if you also code the ‘ghost’ moving whenever you receive that number then it will look like both players are playing on the same board. Don’t forget to mirror the change in the x coordinate, if you want to move left you must subtract 1 from your local player’s x value. However if you receive the number that means the opponent has moved left on their screen you actually have to add one to the ‘ghost’ sprite’s value to keep player positioning in sync. In short, on any given movement input, move your local sprite accordingly and send a signal that if received on your microbit would make the opponent move the opposite.

Attacking

Screenshot 2021-11-07 at 21-44-32 Microsoft MakeCode for micro bit.png
Screenshot 2021-11-07 at 21-43-36 Microsoft MakeCode for micro bit.png
Screenshot 2021-11-07 at 21-52-43 Microsoft MakeCode for micro bit.png

Now for the fighting part of your fighting game! As with movement, everything is run locally on the receiving microbit; so attacking only consists of sending a signal to the other microbit that you’ve tried to attack and showing some LEDs to confirm the attack has been sent. We decided to attack on shake as it fit the intense gameplay environment we wished to curate, but this is your game and it's your choice of how you want to hit your friends. 


When your microbit receives the attack number, subtract the local player’s x value from the opponent’s x value; if it is equal to 1 then the players must be next to each other and you can allow the attack to go through. Make sure to have separate radio numbers for a successful and missed attack and to have an appropriate response in each case. 


For example if microbit 1 wants to attack, it will send the appropriate number over the radio. Microbit 2 hears this and decides whether or not it should hit with the player positions on its end. It then sends one of two numbers and shows a picture to communicate the result of the attack and sends a number to microbit 1 to prompt it to do the same. Remember to decrease the hit player’s lives by 1 and reset player position if it hits.

Dodging

Screenshot (812).png

In its current state it isn’t much more than a simple contest of who can get in the other player’s face faster. So here is where we have some room for creativity, in this Instructable we’ll show you how to implement a couple of example mechanics but feel free to let your imagination flow!


First, try raising the skill cap by adding a dodge mechanic. Store a local boolean and whenever you move backwards or jump (which you can code by temporarily lowering the y value by 1 to lift the player) setting it to true for a period of time. We went with 1 second and 200 milliseconds respectively. Whenever the microbit receives an attack signal when this dodge boolean is true you can reject it even if the other player is close enough to hit you.


Another idea is implementing a push mechanic, when the x values of each player is the same you can move the victim a small amount. This adds another viable option when you are close enough to attack as you could instead push them and bait them into attacking the air. There is endless room for experimentation, find a direction that shines for you and follow its path till the end!

Game States and Finishing the Game

The game operates in three different states; startup, combat, and victory/defeat. On startup the variables are set and a confirmation signal is sent. Combat is the main gameplay loop, running off various inputs and the signals sent to keep the gameplay in sync on both sides. It resets every time an attack is attempted and continues until the fifth successful attack is received. Upon which the game will recognise this and prevent all normal inputs. As attacks and lives are handled locally the microbit will know that if it thinks it is time for the game to end it must be the losing side. It will send the ‘you’ve won!’ radio number and print the game over screen.