Introduction: Micro:bit Gambling Game

In this instructable I will teach you how to make a simple gamballing game. For this simple step by step guide you will be using 9 categories using a block coding method. Each category does different things to your micro bit. To make the gambling game work you only need to use 6 categories but it won't be as good. the other 3 categories I will tell you how to implement but it would be after the necessary steps are done.

In the end it should look like something like the picture above.

Step 1: Opening the Website :

To start the project open up your web browser and search ( www.Makecode.microbit.org ). When you search the website you should see a webpage that looks something like this. To get started you want to click on "New Project".

Step 2: Getting Ready:

When you open the website, you are presented with two blocks ("on start" and "forever"). Since we do not need the "forever" block, you can either choose to left click it and drag it over to the section area (to delete it) or leave it there in a corner since it shouldn't change anything as long as you don't put anything in it.

Step 3: Creating Variables:

Once you have done that we can get started on the on start section. First before you do anything you have to create two variables. This is because we need something to be the score and something to act as the randomiser. to do this click on "variables" (you will find "variables" in the block section area as shown by the picture above). Once you have clicked on "variable" you should be presented with a block that says "Make a Variable". Once you see the "make new variable" left click it and you should be presented with a pop up that says "New variable name:". Under this there is a section to write a name, you can write any name but it is easiest to write "score". This is because this variable is going to keep track of the score. Next you are going to want to create another variable and name it "hand". This hand variable is going to keep track of the randomisation part of this gambling game (you don't need to use the hand variable on the next step but it will be necessary on the following steps after that).

Step 4: On Start:

To make the "on start" work we need three blocks. Block number one and two are found in the "basic" section. First we are going to drag those blocks out. once you have clicked on the basic section you are going to drag out the "show starting hello!" and also the "clear screen" block. The "clear screen" block won't be found in the basic but instead right under it where now it should say more. After you have dragged out both blocks then drag out the third block which is in the "variable" section. After you have clicked on the variable you want to drag out the "set ... to 0". It's either going to have the hand variable or the score variable in the "...."section. Once you have dragged all 3 blocks your workplace should look like something in the picture above. next check to see if the red block hand "hand" or "score" in the mini inside block. if it has "hand" then click the mini block and change it to score. After you have done that but the three blocks into on start in the order shown above in the second picture.

Step 5: Random Block:

All of the three buttons that are used all have the same coding behind them. the only difference is the numbers that you put in on the "pick random" block. This is because each button has its own odds.

First we are going to do the "on button A pressed". To find this go to "input" and it should be the first option. Next we are going back to the "variable" section to get the " set ... to 0". put this inside the "on button A pressed" instead of score this time we are going to have the "hand" variable. another difference is that we are going to change the 0 to "pick random". To find pick random you click on the Math section and it is one of the bottom options. drag out "random" and put it where the 0 is on the "set score" block and it should just get put into place. There should be two numbers for now put 1 in the first number section and 1000 in the second number section.

Step 6: Logic:

For the next part you have to go to the logic section and drag across the"if true then"but make sure it is the one with "else" in it to. We need the logic because since we are doing odds it is going to say if the number is equal to or greater than 500 you lose but if your number is under 500 you win. To get this go back to the "logic" section and scroll down until you find "0=0". Once you have dragged it over to your workplace put it in between the "if then". for the first "0" put the "hand" variable there and for the second "0" change it to 500. The final thing we have to do is change the equal sign to the "greater than or equal to" sign. Now it should look the same as the picture above.

Inside the block You want to add in the set "score to 0" block (same block as used in the "on start"). right below that you want to put in the "show leds". You find this block in the basic section and when you put in the block draw an X. This is to show that on this particular time they lost. Next put "show number" which is found also in the basic section but instead of writing a number drag in the variable "score". Finally before we move to the else section put in the "pause" block. This block slows the game down and you find it by clicking the advanced section, then click the "game" section and finally click "more". After you have done this it should look like the second picture

To finish off this part we are going to do "else" part of the logic block. this is for if they won. All you need is to put in the "change score by 1" and the "show number score" as used above.

Step 7: On Button B Pressed and on Shake:

Repeat the last two steps but instead of "on button A pressed" change it to "on button B pressed" and "On Shake". Also change the odds for each one. To do that you can change the 500 number to anything. If you change it to 600 they would have a 40% chance of winning. Once you have done all three you should have something that looks like the picture above.

Step 8: (Optional) Background Music:

To do this you have to click advanced and scroll right to the bottom where you find control. After you have clicked control take the "run in the background" block. This makes this tune play in the background. Next go to loops and drag out the "repeat" block and put it in the "run in the background". You have to use repeat because you can't put forever inside the "run in the background" block, so where you can change the number change it to 10 000. This insures it will run for what seems like forever. Next go to music and have fun. I chose to only use the "play tone" block but have fun and experiment. After that you should be fully done and you should have a fully working gambling game.