In this session we'll be creating a menu so that you can select the game you want to play
Start by opening Thonny.
Now take your console of of its case (if it's in one) ensuring the console is switched while holding the 'bootsel' button on the back of the device while plugging it into the computer.
Follow the prompts in Thonny to install Micropython on the console.
Create a new file called "main.py" and copy the code below into it:
# Flappy Bird game for the Raspberry Pi Pico Game Boy
from PicoGameBoy import PicoGameBoy
from time import sleep
from random import randint
pgb = PicoGameBoy() #This sets up the computer
selected = 0 #This variable stores which game is selected
def paintMenu(): # Create a paint function to draw the menu
BACKGROUND_COLOR = PicoGameBoy.color(112,197,206)
pgb.fill(BACKGROUND_COLOR)
#Insert Code Here...
pgb.show()
paintMenu() #This calls the paintMenu function
Below where the text says 'Insert Code Here...' start designing/coding the menu the functions below should help
A function to draw a selection box (you'll want the the top text selected)
pgb.fill_rect(x_coordinate, y_coordinate, width, height, colour)
e.g. pgb.fill_rect(45, 25, 120, 18, pgb.black)
A function to draw text
pgb.text('Text you want to write', x_coordinate, y_coordinate, colour)
e.g. pgb.text('Flappy Bird',50, 30, pgb.white)
You'll need three menu items:
Flappy Bird
Tetris
Game of Life
The screen is 240 pixels wide and 135 pixels tall - coordinate start (0,0) in the top left corner of the screen.
Each text charact is 8 pixels wide and tall; so the text "Rob" which is 3 characters will be 24 pixels wide.
Run the program often to see what it looks like.
Next you'll need a loop to check for button presses.
Copy the code below and add it to the bottom of main.py:
while True:
if pgb.button_A() or pgb.button_B(): #If button A or B is pressed
#the selected game launches
if selected == 0:
import PicoFlappyBird
elif selected == 1:
import tetris
elif selected == 2:
import gol
#Check for the up or down button being pressed
#Insert code here...
The code below will change the selection and then repaint the screen.
elif pgb.button_down():
selected = (selected + 1) % 3 #Add 1 but make sure it
#wraps to 0 on 3
paintMenu()
sleep(0.3)
can you copy and change it so it works for a button_up press too?
can you change the paint code so that the selection box moves when the button is pressed?
(hint you'll need to update the y position based on the selection e.g. 25 + selected * 20)