from Log import *
import time
import random
class StopLightGame:
"""
Class representing the "Stop the Light" game where an LED light moves back and forth,
and the player must stop it at the correct target position.
"""
def _init_(self, num_leds=8):
"""
Initializes the game with a set number of LEDs and other variables.
"""
Log.d("StopLightGame: Initializing")
self.num_leds = num_leds
self.current_position = 0 # Light starts at position 0
self.target_position = None # Target position the player needs to stop at
self.direction = 1 # 1 means moving right, -1 means moving left
self.is_running = False # Flag to check if the game is running
self.speed = 0.5 # Speed of the light movement, decreases as the level increases
self.level = 1 # Start at level 1
def generateTargetPosition(self):
"""
Generates a random target position that the player needs to stop the light at.
"""
self.target_position = random.randint(0, self.num_leds - 1)
Log.d(f"StopLightGame: Generated target position at {self.target_position}")
def moveLight(self):
"""
Moves the light back and forth across the LED strip.
"""
if self.is_running:
Log.d(f"StopLightGame: Moving light from position {self.current_position}")
self.current_position += self.direction
# Reverse direction if it reaches the end of the strip
if self.current_position == 0 or self.current_position == self.num_leds - 1:
self.direction *= -1
Log.d(f"StopLightGame: New light position is {self.current_position}")
def flashLight(self):
"""
Simulates flashing the light at the current position on the LED strip.
"""
Log.d(f"StopLightGame: Flashing light at position {self.current_position}")
# Here you can add code to interact with your hardware LED strip
def checkPosition(self):
"""
Checks if the player's input stopped the light at the correct target position.
"""
if self.current_position == self.target_position:
Log.d("StopLightGame: Player stopped at the correct position!")
return True
else:
Log.d(f"StopLightGame: Player stopped at the wrong position: {self.current_position}")
return False
def levelUp(self):
"""
Advances the game to the next level by speeding up the light movement.
"""
self.level += 1
self.speed = max(0.1, self.speed - 0.1) # Increase speed by reducing delay
Log.d(f"StopLightGame: Leveling up to Level {self.level} with new speed {self.speed}")
def startGame(self):
"""
Starts the game by setting the running flag to True and generating the first target.
"""
self.is_running = True
self.generateTargetPosition()
Log.d("StopLightGame: Game started")
def endGame(self):
"""
Ends the game by setting the running flag to False and stopping light movement.
"""
self.is_running = False
Log.d("StopLightGame: Game over!")
self.level = 1
self.speed = 0.5 # Reset to the initial speed
self.startGame()