4. Press It game
4.1. Game design
- Set up the game object
Assign values to the LEVEL_SPEED dictionary.
Assign values to the LEVELUP tuple.
Set the score to 0.
Set the level to 1.
- Run the game:
Show ‘A’ or ‘B’.
- If the correct button is pressed within the time limit then:
Display a tick (Image.YES).
Add one to the score.
Level up if the next level has been reached.
- If the wrong button is pressed or no button is pressed within the time limit then:
Display an X (Image.NO).
Scroll the score.
PressIt()
, for the game object.run_game
method is used outside the class itself.from microbit import *
import random
game = PressIt()
game.run_game()
while True:
if button_a.was_pressed() and button_b.was_pressed():
game = PressIt()
game.run_game()
else:
sleep(2000)
4.2. The PressIt class
- class PressIt
- Set up the game object to control the game, including the LEVEL_SPEED dictionary, the LEVELUP tuple, the initial level and score.
from microbit import *
import random
game = PressIt()
4.3. The PressIt class methods
show_a()
shows ‘A’.show_b()
shows ‘B’.show_yes()
shows a tick, Image.YES.show_no()
shows a cross, Image.NO.show_start
shows ‘A or B’ and the level.show_levelup()
shows an up arrow, Image.ARROW_N, and scrolls the level.show_score
shows the score.is_correct_button()
picks A or B to show then waits according to the duration for the level, and returns True if the correct button has been pressed, otherwise False.continue_game()
calls show_yes() and updates the score and level.end_game()
calls show_no() and show_score() methodsrun_game()
calls several methods as it checks the correct button is pressed within the time limit and either continues or ends the game.
4.4. The PressIt __init__ method
- __init__()
- The __init__() method is called when the game object is created and initializes the instance varaibles.
score
is set to 0.level
is set to 1.
class TiltPixels:
def __init__(self):
self.score = 0
self.level = 1
4.5. The PressIt class constants
1: 1200
, the key is 1 and the duration is 1200.class TiltPixels:
LEVEL_SPEED = {1: 1200, 2: 1000, 3: 800, 4: 700, 5: 600, 6: 550, 7: 500, 8: 450, 9: 400}
LEVELUP = (3, 6, 9, 12, 15, 18, 21, 24)
4.6. Game code
"""PressIt_game: Press the A or B button when the text is shown"""
from microbit import *
import random
class PressIt():
LEVEL_SPEED = {1: 1200, 2: 1000, 3: 800, 4: 700, 5: 600, 6: 550, 7: 500, 8: 450, 9: 400}
LEVELUP = (3, 6, 9, 12, 15, 18, 21, 24)
def __init__(self):
self.score = 0
self.level = 1
def show_a(self):
display.show("A")
def show_b(self):
display.show("B")
def show_yes(self):
display.show(Image.YES)
sleep(500)
def show_no(self):
display.show(Image.NO)
sleep(500)
def show_levelup(self):
display.show(Image.ARROW_N)
display.scroll('level ' + str(self.level), delay=60)
sleep(500)
def is_correct_button(self):
button_number = random.randint(0, 1)
if button_number == 0:
self.show_a()
elif button_number == 1:
self.show_b()
a_pressed = False
b_pressed = False
start_time = running_time()
now = running_time()
while now - start_time < self.LEVEL_SPEED[self.level]:
if button_a.is_pressed():
a_pressed = True
if button_b.is_pressed():
b_pressed = True
now = running_time()
if button_number == 0:
if a_pressed is True and b_pressed is False:
return True
else:
return False
elif button_number == 1:
if a_pressed is False and b_pressed is True:
return True
else:
return False
def continue_game(self):
self.show_yes()
self.score += 1
if self.score in self.LEVELUP:
self.level += 1
self.show_levelup()
def end_game(self):
self.show_no()
self.show_score()
def run_game(self):
self.show_start()
game_over = False
while game_over is False:
if self.is_correct_button():
self.continue_game()
else:
game_over = True
self.end_game()
game = PressIt()
game.run_game()
while True:
if button_a.was_pressed() and button_b.was_pressed():
game = PressIt()
game.run_game()
else:
sleep(2000)
Tasks
Modify the code to display left and right arrows instead of ‘A’ and ‘B’.
Add a rapid animation of 3 to 6 built in image shapes to be shown when the level reaches level 5.
Replace the level scrolled text with an animation in which the number of images in the animation is equal to the level number.
Add code to store all the game scores and display the average score after each game.
Add code to store the best game score after each game and display the best score after exiting by pressing both buttons.