from Button import Button
from LightStrip import LightStrip, RED, GREEN
from Displays import LCDDisplay
from Buzzer import PassiveBuzzer, tones
import time
import utime
class Game:
"""Represents the Ping Pong game with settings and game state."""
def __init__(self, winning_score=10, duration=60, max_score=10):
self.winning_score = winning_score
self.duration = duration
self.max_score = max_score
self.start_time = None
self.game_over = False
def start(self):
"""Starts the game and initializes the timer."""
self.start_time = utime.time()
self.game_over = False
print("Game Started!")
def reset(self):
"""Resets the game to initial settings."""
self.winning_score = 10
self.duration = 60
self.max_score = 10
self.start_time = None
self.game_over = False
print("Game Reset!")
class Player:
"""Represents a player in the Ping Pong game."""
def __init__(self):
self.hits = 0
self.misses = 0
self.fouls = 0
self.score = 0
class Ball:
"""Represents the ball in the Ping Pong game."""
def __init__(self, speed=1, position=0):
self.speed = speed
self.position = position
self.direction = 1 # 1 for right, -1 for left
class GameController:
"""Controls the logic, hardware interaction, and game flow."""
def __init__(self, game, player1, player2, ball, light_strip, button1, button2, display, buzzer):
self.game = game
self.player1 = player1
self.player2 = player2
self.ball = ball
self.light_strip = light_strip
self.button1 = button1
self.button2 = button2
self.display = display
self.buzzer = buzzer
self.setup_buttons()
def setup_buttons(self):
"""Initializes the buttons with their respective handlers."""
self.button1.setHandler(self)
self.button2.setHandler(self)
def buttonPressed(self, button_name):
"""Handles button press events, with increased buzzer volume."""
if self.game.game_over:
return
if button_name == "Blue" and self.ball.position in range(0, 4):
self.player1.hits += 1
self.ball.direction *= -1
elif button_name == "Yellow" and self.ball.position in range(4, 8):
self.player2.hits += 1
self.ball.direction *= -1
else:
# Foul - play louder buzzer sound and update score
self.buzzer.setVolume(20) # Set to max volume for loudness
self.buzzer.beep(tones['C4'], 1000)
if button_name == "Blue":
self.player1.fouls += 1
self.player2.score += 1
else:
self.player2.fouls += 1
self.player1.score += 1
self.update_display()
def buttonReleased(self, button_name):
"""Not used in this simple game logic."""
pass
def update_display(self):
"""Updates the score on the LCD display."""
self.display.clear()
self.display.showText(f"P1:{self.player1.score}", 0, 0)
self.display.showText(f"P2:{self.player2.score}", 1, 0)
def update_ball(self):
"""Updates the ball position and checks for scoring."""
self.light_strip.setColor((0, 0, 0))
self.ball.position += self.ball.direction
if self.ball.position < 0:
self.player2.score += 1
self.ball.position = 3
self.ball.direction = 1
elif self.ball.position >= self.light_strip._numleds:
self.player1.score += 1
self.ball.position = 4
self.ball.direction = -1
self.light_strip.setPixel(self.ball.position, (255, 255, 255))
def check_game_over(self):
"""Checks if the game over conditions are met."""
if self.player1.score >= self.game.winning_score:
self.game.game_over = True
self.display.clear()
self.display.showText("Player 1", 0, 0)
self.display.showText("Wins Game!", 1, 0)
elif self.player2.score >= self.game.winning_score:
self.game.game_over = True
self.display.clear()
self.display.showText("Player 2", 0, 0)
self.display.showText("Wins Game!", 1, 0)
elif utime.time() - self.game.start_time >= self.game.duration:
self.game.game_over = True
self.display.clear()
self.display.showText("Time Up!", 0, 0)
def run(self):
"""Main game loop."""
self.game.start()
while not self.game.game_over:
self.update_ball()
self.check_game_over()
time.sleep(0.5 / self.ball.speed)
print("Game Over!")
print(f"Player 1 Score: {self.player1.score}, Player 2 Score: {self.player2.score}")
# --- Game Initialization ---
game = Game(winning_score=10) # Players need to score 10 points to win
player1 = Player()
player2 = Player()
ball = Ball()
light_strip = LightStrip(pin=2, numleds=8)
button1 = Button(pin=15, name="Blue")
button2 = Button(pin=14, name="Yellow")
display = LCDDisplay(sda=0, scl=1)
buzzer = PassiveBuzzer(17)
game_controller = GameController(game, player1, player2, ball, light_strip, button1, button2, display, buzzer)
game_controller.run()