From MakeCode to Circuit Python, an open text by students of Bates College and amazing people anywhere.
For my next make code exploration, I decided to dig further into the math functions and using the buttons on the CPx. I have very limited experience with these aspects of the CPx. Looking back, it would have been interesting to incorporate these complex elements into our group presentation.
1.
Sam, the dancer who used dice in his dance routine, inspired me to look at a program that involved random numbers. For my first program, I used the “on button click A” block to generate random numbers from 1-6, corresponding to the number on a die. This is included inside an “if, then, else” block. For my first program, I used the math inequality “less than”, and said if the random number generator was less than 3.5, an arbitrary number, both a sound and light animation would be displayed on the CPx. This is repeated every time you click button A. As you can see in my video, the sound and light display only works approximately fifty percent of the time, which makes sense. The probability of drawing a number less than 3.5 is 0.5 or 50%.
https://makecode.com/_H1PiLK6e1LC5
2.
My next program is very similar to the code above. In this program, I wanted to use the same random number generator, but have a different sound and animation combination play regardless of the number generated. To do this, I chose a sound and animation block which I attached to the else block. This is all included inside the larger “on button A click” block. As you can see from the screenshot below, a siren sound and purple light animation show up on the CPx if the random number selected is above 3.5, which occurs approximately 50% of the time.
https://makecode.com/_VajTL22MxP1L
3.
For my third program, I used random numbers along with the “on button A click” to come up with a die that radiates a random amount of lights every time button A is clicked. The only difference is this die has ten possibilites instead of six, which I used for the previous two programs. To do this, I used a “for” loop and used the math block “pick random _ to _” to assign numbers from 0-9. I chose 0-9 because these integers correspond to the amount of lights on the CPx (10). I then set pixel color at the given index to a certain color, which displays the number of lights the die generates. To make things more entertaining, I also added a sound to this every time you “roll the die”.
https://makecode.com/_0t1Cxi5tdici
Lastly, I decided to include my Loom video that I created for all three of my programs. Hope you enjoy watching and learned something about using buttons, math blocks, and random numbers on make code!!
https://www.useloom.com/share/cbdca1524f4e4003ae744f113e6261eb