2. Magic 8-ball
2.1. Game design
random.choice(responses)
.2.2. Specific Syntax
- import random
Import the random number module.
- random.choice(sequence)
Return a random element from a sequence such as a list.
display.scroll(random.choice(responses), delay=120)
- accelerometer.was_gesture(gesture)
Return True or False to indicate if the named gesture was active since the last call.
if accelerometer.was_gesture("shake"):
2.3. Game code without classes
"""Magic_8 simulation see responses at https://en.wikipedia.org/wiki/Magic_8-Ball"""
from microbit import *
import random
responses = [
"It is certain",
"It is decidedly so",
"Without a doubt",
"Yes, definitely",
"You may rely on it",
"As I see it, yes",
"Most likely",
"Outlook good",
"Yes",
"Signs point to yes",
"Reply hazy try again",
"Ask again later",
"Better not tell you now",
"Cannot predict now",
"Concentrate and ask again",
"Don't count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful",
]
while True:
display.show("8")
if accelerometer.was_gesture("shake"):
display.clear()
sleep(1000)
display.scroll(random.choice(responses), delay=120)
Tasks
Modify the code to only respond with positive responses.
Modify the code to require a tilt to the left or right instead of a shake.
Modify the code to require a button press instead of a shake.
Divide up the responses into positive responses and negative responses. Display a positive response when the A button is pressed and a negative response when the B button is pressed.
Divide up the responses into positive responses and negative responses. Display a negative response when the microbit is tilted to the left and a positive response when the microbit is tilted to the right.
2.4. Converting to using a class
game = Magic8()
instantiates the class by creating an object from the class, called an instance, which inherits all the class attributes and methods.__init__
method has the responses list as well as an attribute for the text, magic_text, to display between responses.run_game
method has the game code that was previously within the body of the while loop.game.run_game()
calls the run_game
method on the game object to run the game.from microbit import *
import random
class Magic8:
"""Magic_8 game using a class"""
def __init__(self, magic_text=8):
self.magic_text = magic_text
self.responses = [
"It is certain",
"It is decidedly so",
"Without a doubt",
"Yes, definitely",
"You may rely on it",
"As I see it, yes",
"Most likely",
"Outlook good",
"Yes",
"Signs point to yes",
"Reply hazy try again",
"Ask again later",
"Better not tell you now",
"Cannot predict now",
"Concentrate and ask again",
"Don't count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful",
]
def run_game(self):
display.show(self.magic_text)
sleep(1000)
if accelerometer.was_gesture("shake"):
display.clear()
sleep(100)
display.scroll(random.choice(self.responses), delay=120)
game = Magic8()
while True:
game.run_game()
Tip
In the code game = Magic8()
, the class is Magic8
and the instantiated object is game
.
The __init__
method is used to initialize (assign values) to the data variables of the class when the class object is created. It also can contain statements (i.e. instructions) that are executed at time of object creation. The __init__
method is run as soon as the class object is instantiated.
2.5. Modifying classes
responses = ["For sure", "Yes", "No", "No way"]
2.6. Pass arguments to the class
Tasks
Use ‘?’ as an argument for
Magic8()
to show ‘?’ instead of ‘8’.
game = Magic8('?')
replaces the default value of 8 with ‘?’.'?'
is passed to the __init__ method in place of the magic_text
parameter.self
is automatically passed to the instance methods.from microbit import *
import random
class Magic8:
def __init__(self, magic_text=8):
self.magic_text = magic_text
self.responses = ["For sure", "Yes", "No", "No way"]
def run_game(self):
display.show(self.magic_text)
sleep(1000)
if accelerometer.was_gesture("shake"):
display.clear()
sleep(100)
display.scroll(random.choice(self.responses), delay=120)
game = Magic8('?')
while True:
game.run_game()
2.7. Modify the __init__ method in a child class
Tasks
Modify the code to only respond with positive responses.
Magic8Pos
, can inherit from the Magic8
class by passing Magic8
as an argument when declaring Magic8Pos
, as in: class Magic8Pos(Magic8):
super().__init__(magic_text=8)
to inherit attributes from the __init__
method in the Magic8
class.self.responses
attribute in the child class, Magic8Pos
, to just use positive responses.from microbit import *
import random
class Magic8:
def __init__(self, magic_text=8):
self.magic_text = magic_text
self.responses = ["For sure", "Yes", "No", "No way"]
def run_game(self):
display.show(self.magic_text)
sleep(1000)
if accelerometer.was_gesture("shake"):
display.clear()
sleep(100)
display.scroll(random.choice(self.responses), delay=120)
class Magic8Pos(Magic8):
"""modifies responses to just positive ones"""
def __init__(self, magic_text=8):
super().__init__(magic_text=8)
self.responses = ["It is certain", "Yes"]
game = Magic8Pos(Magic8)
while True:
game.run_game()
2.8. Use tilting in the run_game method in a child class
Tasks
Modify the code to require a tilt to the left or right instead of a shake.
Magic8Tilt
, can inherit from the Magic8
class by passing it as an argument when declaring it, as in: class Magic8Tilt(Magic8):
super().__init__(magic_text=8)
to inherit attributes from the __init__
method in the Magic8
class.run_game
method in the child class, Magic8Tilt
, to use tilting.from microbit import *
import random
class Magic8:
def __init__(self, magic_text=8):
self.magic_text = magic_text
self.responses = ["For sure", "Yes", "No", "No way"]
def run_game(self):
display.show(self.magic_text)
sleep(1000)
if accelerometer.was_gesture("shake"):
display.clear()
sleep(100)
display.scroll(random.choice(self.responses), delay=120)
class Magic8Tilt(Magic8):
"""modifies run_game to use tilts"""
def __init__(self, magic_text=8):
super().__init__(magic_text=8)
def run_game(self):
display.show(self.magic_text)
sleep(1000)
if accelerometer.was_gesture("left") or accelerometer.was_gesture("right"):
display.clear()
sleep(100)
display.scroll(random.choice(self.responses), delay=120)
game = Magic8Tilt()
while True:
game.run_game()
2.10. Modify the __init__ and run_game methods in a new class
Tasks
Divide up the responses into positive responses and negative responses. Display a negative response when the A button is pressed and a positive response when the B button is pressed.
__init__
method, use responses_neg and responses_pos instead of just responses.run_game
method, use button pressing to set the responses_choice which is then picked from for display.from microbit import *
import random
class Magic8NegPos:
def __init__(self, magic_text=8):
self.magic_text = magic_text
self.responses_pos = ["For sure", "Yes"]
self.responses_neg = ["No", "No way"]
def run_game(self):
display.show(self.magic_text)
sleep(1000)
if button_a.is_pressed():
responses_choice = self.responses_neg
elif button_b.is_pressed():
responses_choice = self.responses_pos
else:
responses_choice = ""
if responses_choice != "":
display.clear()
sleep(100)
display.scroll(random.choice(responses_choice), delay=120)
game = Magic8NegPos()
while True:
game.run_game()
Tasks
Use a subclass of
Magic8NegPos
to display a negative response when the microbit is tilted to the left and a positive response when the microbit is tilted to the right.Use a subclass of
Magic8NegPos
to display a negative response when pin0 of the microbit is touched and a positive response when pin2 of the microbit is touched.