# Model: gemini-pro
# Prompt: In any generated code, please put the model name and version, and the prompt used to generate the code at the top of the comment. In any code that you create, only put the name and version of the model and the prompt used to generate the code at the top as a comment. In the code, import any Python file I attach instead of putting its content in the code. If multiple prompts are needed, please put subsequent prompts after the original prompt.
# Prompt: Generate a class called SpinGame in MicroPython that uses the classes Button, LCDDisplay, PassiveBuzzer, PrizeBox and Wheel in the files attached here. The passivebuzzer is connected to pin 16 and the button is connected to pin 15. Add buttonPresseed and buttonReleased methods in the SpinGame class. When the button is pressed, use the Wheel class to show the animation and use the returned slot to report to the player what prize they won. Use a run method to start the game.
import time
from Button import Button
from Displays import LCDDisplay
from Buzzer import PassiveBuzzer, tones
from PrizeBox import PrizeBox
from Wheel import Wheel
class SpinGame:
"""
A prize wheel game using a Button, LCD Display, Buzzer, and a Neopixel Wheel.
"""
def __init__(self, num_slots=12):
"""
Initializes the game components.
Args:
num_slots (int): The number of slots on the prize wheel.
"""
# Initialize the game hardware and components
print("Initializing SpinGame...")
self.wheel = Wheel(num_slots)
self.prize_box = PrizeBox(num_slots)
self.buzzer = PassiveBuzzer(16)
self.buzzer.setVolume(0.8) #set volume higher
# Assuming an I2C display on the default pins sda=0, scl=1
try:
self.display = LCDDisplay(sda=0, scl=1)
except (ValueError, OSError) as e:
print(f"Error initializing display: {e}")
print("Please ensure the LCD is connected correctly.")
self.display = None
# The Button class needs a handler with buttonPressed and buttonReleased
self.button = Button(15, "SpinButton", handler=self)
self.spinning = False
print("Game Initialized. Ready to run.")
def buttonPressed(self, name):
"""
Handles the event when the spin button is pressed.
"""
if self.spinning or not self.display:
return # Do nothing if already spinning or display is not working
self.spinning = True
self.display.clear()
self.display.showText("Spinning...", 0, 2)
self.buzzer.beep(tones['C4'], 100)
self.buzzer.beep(tones['G4'], 100)
# Run the wheel animation and get the result
winning_slot = self.wheel.spin()
prize = self.prize_box.getPrize(winning_slot)
# Clear the display and show the prize
self.display.clear()
self.display.showText("You won:", 0, 4)
if prize.type == "dollar":
self.display.showText(f" $ {prize.value} ", 1)
# Play a winning sound
victory_chime = [
(tones['C5'], 250),
(tones['E5'], 250),
(tones['G5'], 250),
(tones['C6'], 500),
(tones['G5'], 250),
(tones['C6'], 250),
(tones['E6'], 500),
(tones['C6'], 750)
]
for note, duration in victory_chime:
self.buzzer.beep(note, duration)
# End of New Victory Chime
elif prize.type == "retry":
self.display.showText("A retry", 1)
# Play a neutral sound
self.buzzer.beep(tones['G4'], 150)
self.buzzer.beep(tones['E4'], 150)
self.buzzer.beep(tones['C4'], 200)
else: # no prize
self.display.showText("Zip :(!", 1)
# Play a losing sound
self.buzzer.beep(tones['C4'], 200)
self.buzzer.beep(tones['G3'], 300)
# Wait for the player to see the result
time.sleep(4)
# Reset for the next game
self.display.clear()
self.display.showText("Press to Spin!", 0, 1)
self.spinning = False
def buttonReleased(self, name):
"""
Handles the event when the spin button is released.
This game does not require any action on button release.
"""
pass
def run(self):
"""
Starts the game and enters the main loop.
"""
if not self.display:
print("Cannot run the game without a display.")
return
self.display.clear()
self.display.showText("Press to Spin!", 0, 1)
self.wheel.light_strip.clear()
# The game is event-driven by the button IRQ,
# so we just need to keep the program running.
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Exiting game...")
if self.display:
self.display.clear()
self.wheel.light_strip.clear()
self.button.setHandler(None) # Clean up the button IRQ
# Example of how to run the game
if __name__ == "__main__":
game = SpinGame(num_slots=16) # Create a game with a 16-LED wheel
game.run()