Skip to content

Commit

Permalink
python: update dice experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed May 23, 2024
1 parent 99b1e2b commit 0b2d89d
Showing 1 changed file with 70 additions and 48 deletions.
118 changes: 70 additions & 48 deletions python/src/dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,88 +4,110 @@
from microbit import *


def roll_dice(sides: int = 6) -> int:
return random.randint(1, sides)
class Dice:
"""Simulates a single dice."""

def __init__(self) -> None:
self.faces: list = [4, 6, 8, 12, 20]
self.face_index: int = 0
self.sides: int = self.faces[self.face_index]
self.roll: int = 0

def was_shaken() -> bool:
if accelerometer.was_gesture("shake"):
print("INFO: shaken, not stirred")
print("INFO: sides:" + str(self.sides) + " roll:" + str(self.roll))

strength: int = accelerometer.get_strength()
def roll_dice(self) -> None:
"""Throw the dice and return it's value."""

print("INFO: force used to shake was " + str(strength))
self.roll = random.randint(1, self.sides)

# let's try to keep users from breaking the microbit dice
# looks like max value is < 3,500
# average shake is around 1,000
if strength > 2_000:
print("WARN: you risk angering the RNG gods!")
display.show(Image.ANGRY)
sleep(2000)
print("INFO: sides:" + str(self.sides) + " roll:" + str(self.roll))

return True
def was_shaken(self) -> bool:
if accelerometer.was_gesture("shake"):
print("INFO: shaken, not stirred")

return False
strength: int = accelerometer.get_strength()

print("INFO: force used to shake was " + str(strength))

def was_touched() -> bool:
if pin_logo.is_touched():
print("INFO: touched, not shaken")
# let's try to keep users from breaking the microbit dice
# looks like max value is < 3,500
# average shake is around 1,000
if strength > 2_000:
print("WARN: you risk angering the RNG gods!")
display.show(Image.ANGRY)
sleep(2000)

return True
return True

return False
return False

def was_touched(self) -> bool:
if pin_logo.is_touched():
print("INFO: touched, not shaken")

# at first glance, it looks like the pin_logo is a bad idea but it
# blocks the execution of the code so it's only rolling dice and you
# don't see the roll until after you release the pin
def was_roll_requested() -> bool:
return was_shaken() or was_touched()
return True

return False

faces: list = [4, 6, 8, 12, 20]
face_index: int = 0
sides: int = faces[face_index]
roll: int = 0
def was_roll_requested(self) -> bool:
"""
at first glance, it looks like the pin_logo is a bad idea but it
blocks the execution of the code so it's only rolling dice and you
don't see the roll until after you release the pin
"""

print("INFO: sides:" + str(sides) + " roll:" + str(roll))
return self.was_shaken() or self.was_touched()

def scroll_sides_left(self) -> None:
"""Scrolls left on the list of predefined sides."""

self.face_index = (self.face_index - 1) % len(self.faces)
self.sides = self.faces[self.face_index]
print("INFO: new number of sides is " + str(self.sides))

# this adds latency to the button feedback
display.show(str(self.sides))

def scroll_sides_right(self) -> None:
"""Scrolls right on the list of predefined sides."""

self.face_index = (self.face_index + 1) % len(self.faces)
self.sides = self.faces[self.face_index]
print("INFO: new number of sides is " + str(self.sides))

# this adds latency to the button feedback
display.show(str(self.sides))


print()
print("Dice Simulator")
print()

dice: Dice = Dice()

# Code in a 'while True:' loop repeats forever
while True:
while not was_roll_requested():
display.scroll(str(roll))
while not dice.was_roll_requested():
display.scroll(str(dice.roll))

# decrement sides
presses_a: int = button_a.get_presses()
if presses_a > 0:
face_index = (face_index - 1) % len(faces)
sides = faces[face_index]
print("INFO: new number of sides is " + str(sides))

# this adds latency to the button feedback
display.show(str(sides))
dice.scroll_sides_left()
sleep(500)

# increment sides
presses_b: int = button_b.get_presses()
if presses_b > 0:
face_index = (face_index + 1) % len(faces)
sides = faces[face_index]
print("INFO: new number of sides is " + str(sides))

# this adds latency to the button feedback
display.show(str(sides))
dice.scroll_sides_right()
sleep(500)

print("INFO: rolling dice...")

roll = roll_dice(sides)
dice.roll_dice()

# closest thing to a spinning wheel
# this was a good call, it adds suspense and feed back so you
# know when it was shaken or when you can let go of the pin_logo
display.show(Image.ALL_CLOCKS)

print("INFO: sides:" + str(sides) + " roll:" + str(roll))

0 comments on commit 0b2d89d

Please sign in to comment.