In my previous post, I introduced how to create a dodge game on the smart hub of LEGO MindStorms by using MicroPython.
In this post, I would like to introduce how to write a Python version of dodge game on micro:bit by using its new Python Editor (beta version). The final effect could be like this:
Now let us take a look the sample code and I will explain them line by line.
From line 2 to line 5, the program imports microbit module, and some other Python common modules. At line 7, the microbit LED screen will display an image of Heart shape.
Till this moment, I believe that you understand the code pretty well. So what are the code from line 8 to line 11 doing? They are adjusting the brightness of LED grids. The default brightness of each pixel is 9 (0 represents off, and 9 represents the brightest). Personally, I feel the light is too dazzling for eyes, so I would like to adjust it to 3. When the pixel brightness is bigger than 0, use “display.set_pixel(i, j, 3)” to adjust its brightness to 3. If the pixel is off, nothing needs to be done.
Line 12 will make the Heart pattern show 1 second.
From line 14 to line 16, the program turns off all the pixels. You could use “display.clear()” to realize the same effect here. From line 18 to line 26, the program sets some variables. “score” variable is used to record how many times the user controlled pixel dodges from the dropping pixel. “player_col” and “player_row” variables are used to record column and row number of the user controlled pixel, while “drop_col” and “drop_row” variables are used to record column and row number of the dropping pixel.
“drop_frame” variable is used to control the speed of dropping pixel. “running” variable is used to indicate if the game is running as normal or ending.
At line 19, the program uses “music.play(“c4:4″)” to play a beep sound. “c4:4” represents playing the note named C in octave number 4 and playing for a duration of
4. You could find those detailed character definition in the MicroPython Documentation.
Line 27 will make the user controlled pixel show at the position of column 2, row 4. Its brightness is set to 4. Starting from line 28 to line 60, the program enters a main loop. Line 30 to line 39 handles the user input. That is, when user presses button B on micro:bit, the program checks the column value of the user controlled pixel. If it is not at the right most column, player_col variable increases by 1. The current pixel is turned off, and the pixel at the new position is turned on with brightness of 4. The same rule applies to the pressing of button A.
There is a tricky point I need to mention here. From the MicroPython Documentation, we know that button class has two similar methods – “was_pressed()” and “is_pressed()”. What is the difference between them? The key difference is that for the method “was_pressed()”, pressing the button will return True ONLY ONCE. Therefore, no matter you press the button for 0.5 second or 2 seconds, the first time the program calls “button_a.was_pressed()”, the method returns True, and it returns False when you continue pressing the button. Using this method “was_pressed()”, the program could avoid triggering too many times of True condition.
Line 41 to line 60 handles the activity of dropping pixel. Since the code on micro:bit board runs pretty fast, without using a variable “drop_frame” to slow down the speed, the dropping pixel will move too fast to see its trace. You could tweak the value of “drop_frame” till you are satisfied with the movement of the pixel. The smaller the value of “drop_frame”, the faster the dropping pixel moves. In each iteration, the program checks the value of “drop_frame”. If it is bigger than zero, just decrease “drop_frame” by 1. All the pixel position updating and pixel collision checking happen when “drop_frame” equals to 0.
At line 46, the program checks if the dropping pixel moves to the bottom row. If it does, reset drop_row to 0 and randomize its drop_col. That means, the dropping pixel will display on the top row and restart dropping.
Line 51 to line 60 handle the interaction between dropping pixel and user controlled pixel. when the dropping pixel reaches the same row as the user controlled pixel (row 4), the program checks if the two pixels overlap at the same column. If they do, the program sets running to False, indicating game over. If they do not, score variable increases by 1.
Line 62 to 70 handles the clean up of the whole program. It first shows the score on the LED grids. Line 64 to line 67 are used to adjust the brightness of the LED grids. After waiting for 1 second, all the LED pixels are turned off and the game finishes.
That is all for the Python version of the dodge game. Hope this explanation and sample code help you better understand the MicroPython and its usage on micro_bit. Enjoy the coding and have fun!
Note: All the analysis articles are copyright products of http://www.thecodingfun.com. Anyone re-posting them should credit author and original source. Anyone using them for commercial purposes or translating them into other languages should notify TheCodingFun and get confirmation first. All Rights Reserved.