Even Odd game - Code V3

From RoboWiki
Revision as of 09:00, 8 June 2023 by Robot (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Return back to project page: Even Odd game - Fedor Agarshev

Python code for the Even Odd game project:

 
from hub import port, light_matrix, speaker
import motor, force_sensor, color_sensor
import color


pointMotor1 = port.B
pointMotor2 = port.D
motorForBet = port.A
buttonForceSensor = port.E
colorSensor = port.F


motor.run_to_relative_position(pointMotor1,0,1000)
motor.run_to_relative_position(pointMotor2,0,1000)

class player:
    def __init__(self, name, motor, invert_move = 1):
        self.name = name
        self.scorePlayer = 10
        self.motor = motor
        self.invert_move = invert_move

    def update_score(self, new_score):
        motor.run_for_degrees(self.motor,(new_score - self.scorePlayer) * 30 * self.invert_move, 500)
        self.scorePlayer = new_score

Player1 = player("Player1", pointMotor1) #You can change the name if you want.
Player2 = player("Player2", pointMotor2, -1) #You can change the name if you want.

def update_score(new_score1, new_score2, whos_betting, who_chooses):
    whos_betting.update_score(new_score1)
    who_chooses.update_score(new_score2)

def choice_of_bet():
    global Player1, Player2
    bet = 0
    motor.reset_relative_position(motorForBet, 0)
    light_matrix.write(str(bet))
    while not force_sensor.is_pressed(buttonForceSensor):
            if 20 < motor.relative_position(motorForBet) :
                if bet < Player1.scorePlayer and bet < Player2.scorePlayer:
                    bet += 1
                motor.reset_relative_position(motorForBet, 0)
                light_matrix.write(str(bet))
            if -20 > motor.relative_position(motorForBet) :
                if bet != 0:
                    bet -= 1
                motor.reset_relative_position(motorForBet, 0)
                light_matrix.write(str(bet))

    return bet

def choice_even_or_odd(bet, whos_betting, who_chooses):
    while color_sensor.color(colorSensor) != color.RED and color_sensor.color(colorSensor) != color.GREEN:
        pass
    color1 = color_sensor.color(colorSensor)
    if (color1 == color.RED and bet % 2 == 0) or (color1 == color.GREEN and bet % 2 != 0):
        speaker.beep(600, 500, 50)
        speaker.beep(640, 500, 50)
        speaker.beep(600, 500, 50)
        new_score1 = whos_betting.scorePlayer - bet
        new_score2 = who_chooses.scorePlayer + bet
    else :
        speaker.beep(800, 500, 50)
        speaker.beep(840, 500, 50)
        speaker.beep(800, 500, 50)
        new_score1 = whos_betting.scorePlayer + bet
        new_score2 = who_chooses.scorePlayer - bet
    return new_score1, new_score2

def game():
    global Player1, Player2
    whos_betting = Player1
    who_chooses = Player2
    while whos_betting.scorePlayer != 0 and who_chooses.scorePlayer != 0:
        light_matrix.write(whos_betting.name + " makes a bet")
        bet = choice_of_bet()
        speaker.beep(700, 500, 50)
        new_score1, new_score2 = choice_even_or_odd(bet, whos_betting, who_chooses)
        update_score(new_score1, new_score2, whos_betting, who_chooses)
        whos_betting, who_chooses = who_chooses, whos_betting
    if Player1.scorePlayer > Player2.scorePlayer:
        light_matrix.write(Player1.name + " win!")
    else :
        light_matrix.write(Player2.name + " win!")


game()