from microbit import *
from random import *

# Handles radio communication with other microbit
def on_received_value(name, value):
    global velocityX, velocityY, ballY, ballX, ballSpeed, gameState, pauseState
    if name.compare("vx") == 0:
        velocityX = value  # Update X velocity
    if name.compare("vy") == 0:
        velocityY = value  # Update Y velocity
    if name.compare("y") == 0:
        ballY = value  # Update ball Y position
        ballX = 5  # Reset ball X position
        ballSpeed += -60  # Makes the game faster
    if name.compare("loss") == 0:
        gameState = value  # Update game state
    if name.compare("pause") == 0:
        pauseState = value  # Update pause state
radio.on_received_value(on_received_value)

# Initialize game variables
gameState = 0
pauseState = 0
velocityY = 0
velocityX = 0
ballY = 0
ballX = 0
radio.set_group(1)
paddlePosition = 2  # Initial paddle position
ballSpeed = 500  # Initial ball speed
ballX = 5  # Initial ball X position
ballY = randint(1, 3)  # Randomize initial ball Y position
velocityX = -1  # Initial ball X velocity
velocityY = -1  # Initial ball Y velocity
switchDirection = 1  # One use of switch power up
gameState = 0
pins.digital_write_pin(DigitalPin.P2, 1)

# Send initial radio values to maintain consistency between microbits
radio.send_value("sD", 1)  # Initial switch direction
radio.send_value("speed", 530)  # Initial ball speed

# Manages paddle movement based on joystick input
def on_forever():
    global paddlePosition
    basic.pause(200)  # Delay between movements
    led.unplot(0, paddlePosition)  # Clear previous paddle position

    if ballX < -1 or gameState == 1:  # Game over condition
        paddlePosition = 999  # Hide paddle
    elif pins.analog_read_pin(AnalogReadWritePin.P0) < 100:  # Move paddle up
        paddlePosition += -1
    elif pins.analog_read_pin(AnalogReadWritePin.P0) > 900:  # Move paddle down
        paddlePosition += 1

    # Wrap paddle position
    if paddlePosition == 5:
        paddlePosition = 0
    if paddlePosition == -1:
        paddlePosition = 4
basic.forever(on_forever)

# Displays game state and manages paddle visuals
def on_forever2():
    global paddlePosition

    if ballX < -1:  # Ball out of bounds
        paddlePosition = 999  # Hide paddle
        basic.show_leds("""
            . # . . .
            . # . . .
            . # . . .
            . # . . .
            . # # # .
        """)
        radio.send_value("loss", 1)  # Send signal telling other microbit the game has ended
    elif gameState == 1:  # Win state
        paddlePosition = 999  # Hide paddle
        basic.show_leds("""
            # . . . #
            # . . . #
            # . # . #
            # # . # #
            # . . . #
        """)
    led.plot(0, paddlePosition)  # Display paddle
    radio.set_group(1)  # Ensure correct radio group
basic.forever(on_forever2)

# Handles ball movement and collisions
def on_forever3():
    global velocityX, ballX, velocityY, ballY, pauseState
    basic.pause(ballSpeed)  # Pause between ball updates
    if not (ballX > 0 and ballX < 4 and pauseState == 1):
        led.unplot(ballX, ballY)  # Clear previous ball position

        if ballX == 0 and paddlePosition == ballY:  # Ball hits paddle
            velocityX = velocityX * -1  # Reverse X direction

        ballX += velocityX  # Update ball X position

        #if  pins.analog_read_pin(AnalogPin.P1) < 100 and switchDirection == 1 and (ballX > 0 and ballX < 4):
            #velocityX = velocityX * -1  # Reverse the X direction
            #switchDirection = 0  # Power up is one use only

        if ballX != 5:
            if ballY == 4 or ballY == 0:  # Ball hits top or bottom wall
                velocityY = velocityY * -1  # Reverse Y direction
            ballY += velocityY  # Update ball Y position
            led.plot(ballX, ballY)  # Display ball
        else:  # Ball out of bounds
            radio.send_value("vx", velocityX)  # Send X velocity
            radio.send_value("vy", velocityY)  # Send Y velocity
            radio.send_value("y", ballY)  # Send Y position
basic.forever(on_forever3)
