Slide 1

Slide 1 text

INTRODUCTION TO MICROPYTHON GETTING STARTED WITH BBC MICRO:BIT Juliana Karoline de Sousa | @julianaklulo

Slide 2

Slide 2 text

JULIANA KAROLINE DE SOUSA | @JULIANAKLULO ➔ Bachelor of Computer Science | UFSCar ➔ PyLadies São Carlos | co-founder & organizer ➔ Grupy-Sanca | co-founder & organizer ➔ Omnivector | software engineer ➔ IoT, robotics, 3D printing & kitties :3

Slide 3

Slide 3 text

Please handle the equipment with care Be careful Disclaimers No sponsors This tutorial is not sponsored Have fun! Return Don’t forget to return the devices before leaving the room 1 2 3 4

Slide 4

Slide 4 text

Challenge MicroPython Examples Experimenting with the device’s components Battleship Build the single player version of the game 01. Overview about MicroPython BBC micro:bit Overview of the device and the specifications Python editor 02. 03. Using the Python editor and built-in simulator 04. 05. 06. Make the game multiplayer Agenda

Slide 5

Slide 5 text

MicroPython 01. Python for microcontrollers

Slide 6

Slide 6 text

What is MicroPython? Reimplementation of Python 3 targeted to microcontrollers

Slide 7

Slide 7 text

But what is a microcontroller?

Slide 8

Slide 8 text

Microcontroller Can store persistently user-defined code in memory Integrated Circuit Processor, memory and peripherals all on a single chip I/O Capabilities Built-in input/output ports to interact with sensors and actuators Programmable Low power consumption Suitable for battery powered applications

Slide 9

Slide 9 text

MicroPython Key points: ● Re-implementation of Python 3, written in C ● Contains a small subset of the standard library ● Optimized for constrained environments Features: ● Interactive prompt (REPL) ● List comprehensions, generators, closures ● Exception handling ● Access to GPIO, PWM, ADC, UART and more Requirements: ● 256k code space ● 16k RAM

Slide 10

Slide 10 text

Microcontrollers with MicroPython PyBoard ESP8266 & ESP32 BBC micro:bit Raspberry Pi Pico Arduino Nano RP2040

Slide 11

Slide 11 text

BBC micro:bit 02. Pocket-sized computer for physical computing

Slide 12

Slide 12 text

What is the BBC micro:bit? Pocket-sized computer that introduces you to how software and hardware work together

Slide 13

Slide 13 text

Which components are included?

Slide 14

Slide 14 text

Which components are included?

Slide 15

Slide 15 text

Python editor 03. Easiest way to program the micro:bit

Slide 16

Slide 16 text

Online Python editor https://python.microbit.org

Slide 17

Slide 17 text

Workflow Plug micro:bit into computer Write .py file in the Python editor Click on “save to micro:bit” and select the micro:bit Automatically flash the .hex file to micro:bit Plug micro:bit into computer Write .py file in the Python editor Click on “save to micro:bit” to download .hex file to your machine Copy .hex file to micro:bit flash drive Supported browsers (Chrome) Other browsers (Firefox)

Slide 18

Slide 18 text

Examples 04. Experimenting with the device’s components

Slide 19

Slide 19 text

Code Resources https://tinyurl.com/micropython-tutorial Examples and cheat sheet are available in the repository at https://github.com/julianaklulo/pycon2024-intro-to-micropython

Slide 20

Slide 20 text

Using the Display 0-9: Brightness intensity

Slide 21

Slide 21 text

Using the Push Buttons Button A (left) and Button B (right)

Slide 22

Slide 22 text

Example 1: Buttons and Display

Slide 23

Slide 23 text

Using the Speaker

Slide 24

Slide 24 text

Example 2: Play melody

Slide 25

Slide 25 text

Using the Accelerometer

Slide 26

Slide 26 text

Example 3: Accelerometer Gestures

Slide 27

Slide 27 text

Example 4: Accelerometer values

Slide 28

Slide 28 text

Using the Radio

Slide 29

Slide 29 text

Example 5: Teleporting Duck

Slide 30

Slide 30 text

Battleship 05. Single-player version of the game

Slide 31

Slide 31 text

Battleship - Single Player

Slide 32

Slide 32 text

Battleship Breakdown Player Handles the logic to shoot on the sea and stores the shots fired Sea Stores the automatically generated ships and checks if a shot hit a ship Game Runs the game until the player hits all the ships

Slide 33

Slide 33 text

Sea How to populate the sea with ships?

Slide 34

Slide 34 text

Examples of Sea boards

Slide 35

Slide 35 text

The ship can’t overflow the board Location matters Size matters Place the biggest ships first No touching Each boat must be surrounded only by water 1 2 3 Rules to place the ships Valid placement Impossible to place the third ship

Slide 36

Slide 36 text

Sea Breakdown

Slide 37

Slide 37 text

Analyzing the code Open file battleship-single-player/template_1.py in the repository

Slide 38

Slide 38 text

Sea - Generate board ship sizes brightness

Slide 39

Slide 39 text

Sea - Indexing Index: [ROW][COL] Index: [COL][ROW] VS

Slide 40

Slide 40 text

Sea - Populate board reset the board if the ships don’t fit

Slide 41

Slide 41 text

Sea - Place Ship (part 1) select among the candidates coordinates fails if none of the coordinates work Each water coordinate is a candidate

Slide 42

Slide 42 text

Sea - Possible H: hold the row and increase the col V: increase the row and hold the col check for each part of the boat desired placement

Slide 43

Slide 43 text

Sea - Near Ships Example: (2, 2) non-inclusive exclude the coordinate

Slide 44

Slide 44 text

Sea - Place Ship (part 2) H: hold the row and increase the col V: increase the row and hold the col

Slide 45

Slide 45 text

Sea - Hit will be used once the player starts shooting

Slide 46

Slide 46 text

Sea - Show Board represents the brightness

Slide 47

Slide 47 text

Sea - Show Board create one string for each row then concatenate each string using :

Slide 48

Slide 48 text

Try it in the simulator Open battleship-single-player/template_1.py, paste it in the simulator and execute the code https://python.microbit.org

Slide 49

Slide 49 text

Player How to shoot the ships?

Slide 50

Slide 50 text

Player Breakdown

Slide 51

Slide 51 text

Analyzing the code Open file battleship-single-player/template_2.py in the repository

Slide 52

Slide 52 text

Player - Generate Shots Board start player in the center of the board same structure as the sea board

Slide 53

Slide 53 text

Player - Mark similar to the hit method

Slide 54

Slide 54 text

Player - How to Shoot

Slide 55

Slide 55 text

Player - Shoot Value to filter out small movements or noise in the accelerometer readings Alternate between the coordinate in the shot board and the current coordinate Threshold Blink While loop Run until Button A is pressed Update row Use accelerometer axis Y to update the row Update col Use accelerometer axis X to update the col 1 4 5 3 6 Show the board Show the shots board so the user knows where they’ve already shot 2

Slide 56

Slide 56 text

Writing the shoot method Edit file battleship-single-player/template_2.py in the repository

Slide 57

Slide 57 text

Player - Shoot

Slide 58

Slide 58 text

Try it in the micro:bit Upload battleship-single-player/template_2.py with your implementation to the micro:bit https://python.microbit.org

Slide 59

Slide 59 text

Game Control the logic of the game

Slide 60

Slide 60 text

Analyzing the code Open file battleship-single-player/template_3.py in the repository

Slide 61

Slide 61 text

Game Breakdown

Slide 62

Slide 62 text

Game - Init Sea and Player

Slide 63

Slide 63 text

Game - Win

Slide 64

Slide 64 text

Starting the Game

Slide 65

Slide 65 text

Game - Start and End Display a countdown to indicate that the game is about to start Countdown Greet the player Play sound and display image 1 3 Wait for input Keep blinking the image until the player press both buttons 2 Choose an appropriate sound Play an ending sound End game The game ends when all the ships have been hit 4 6 Display a positive image Happy face is a good choice 5

Slide 66

Slide 66 text

Writing start and end methods Edit file battleship-single-player/template_3.py in the repository

Slide 67

Slide 67 text

Game - Start and End

Slide 68

Slide 68 text

Game - Run Read the player’s shot Shoot the board Start the game Run the start routine 1 3 Loop until end Run the loop until the game ends with the player winning 2 Let the player know if the shot has hit or missed the ships Display the result Check hit and mark Check in the Sea instance if the shot hit any ships 4 6 Check win Check if the player has already hit all the ships and run the end routine if so 5

Slide 69

Slide 69 text

Writing the Game run method Continue editing file battleship-single-player/template_3.py in the repository

Slide 70

Slide 70 text

Game - Run

Slide 71

Slide 71 text

Try it in the micro:bit https://python.microbit.org Upload battleship-single-player/template_3.py with your implementation to the micro:bit

Slide 72

Slide 72 text

Challenge 06. Multiplayer version of the game

Slide 73

Slide 73 text

Battleship - Multiplayer

Slide 74

Slide 74 text

Multiplayer Breakdown Player No changes needed Sea No changes needed Game Handles the publisher and subscriber logic

Slide 75

Slide 75 text

Game Control the logic of the game

Slide 76

Slide 76 text

Analyzing the code Open file battleship-multiplayer/template_1.py in the repository

Slide 77

Slide 77 text

Game Breakdown

Slide 78

Slide 78 text

Game - Init Sea and Players same Player class for both players define the group with a partner and set the constant

Slide 79

Slide 79 text

Game - Start same as the single player version

Slide 80

Slide 80 text

Game - Lost only the player receiving the shot can check if they’ve lost the game

Slide 81

Slide 81 text

Game - End different endings depending on which player won the game

Slide 82

Slide 82 text

Choosing Players

Slide 83

Slide 83 text

Game - Choose Players Send a message after Player 1 pressed Button A, wait for response from Player 2 Player 1 - Button A Display boat image Show an image while waiting for the input from the players 1 3 Loop until press Wait for both players to be ready to start the game 2 PLayer 2 - Button B After Player 2 pressed Button B, wait for message from Player 1 and reply with acknowledgment message 4 Assign the players Store the player number to define who starts the turn 5

Slide 84

Slide 84 text

Writing the Choose Players method Edit file battleship-multiplayer/template_1.py in the repository

Slide 85

Slide 85 text

Game - Choose Players define the group with a partner and set the constant

Slide 86

Slide 86 text

Game - Send Shot Wait for the opponent to reply with the result of the shot Wait for response Run Shoot method Update the coordinates of the current player 1 3 Send message Send to the opponent the coordinates of the shot 2 Mark result Mark in the current player’s board the result of the shot 4 Check hit Celebrate if the current player’s shot hit a ship 5 Return value Returns True if the current player won 6

Slide 87

Slide 87 text

Writing the Send Shot method Continue editing the file battleship-multiplayer/template_1.py in the repository

Slide 88

Slide 88 text

Game - Send Shot

Slide 89

Slide 89 text

Game - Receive Shot Display in the board the coordinate that the opponent shot Blink the coordinate Show the board Display the sea board to the current user 1 3 Wait for message Wait for the opponent to make the shot 2 Check hit and mark Check if the opponent hit a ship in the sea board and mark the shot in the opponent’s board 4 5 Check lose Check if the opponent hit the last ship in the current player’s board 6 Send response Send message with result of the shot and if the opponent won the game 7 Show shot result Display the result of the shot to the current player 8 Return value Returns True if the current player lost

Slide 90

Slide 90 text

Writing the Receive Shot method Continue editing the file battleship-multiplayer/template_1.py in the repository

Slide 91

Slide 91 text

Game - Receive Shot

Slide 92

Slide 92 text

Game - Run Shoots first then receives a shot Player 1 Start the game Run the start routine 1 3 Choose players Decide who’s starting the game 2 The current player loses when receive_shot returns True Opponent win? Player 2 Receives the first shot and then shoots 4 6 Current player win? The current player wins when send_shot returns True 5

Slide 93

Slide 93 text

Writing the Run method Continue editing the file battleship-multiplayer/template_1.py in the repository

Slide 94

Slide 94 text

Game - Run

Slide 95

Slide 95 text

Try it in the micro:bit https://python.microbit.org Find a partner, decide the group number and upload battleship-multiplayer/template_1.py with your implementation to the micro:bit

Slide 96

Slide 96 text

Improvements ● Track the score ● Let the player place the ships manually ● Mark in the player board the sunk ships What improvements would you make?

Slide 97

Slide 97 text

Thank you for attending! I hope you enjoyed it :)

Slide 98

Slide 98 text

For more games, watch my talk at PyCon US 2023 https://www.youtube.com/watch?v=teALLngESw0

Slide 99

Slide 99 text

@julianaklulo Reach out via social media

Slide 100

Slide 100 text

Reach out via social media I’ll post more details about the MicroPython open space