Difference between revisions of "Even Odd game - Code"

From RoboWiki
Jump to: navigation, search
 
Line 1: Line 1:
 
Return back to project page: [[Spike Prime - Even Odd game - Fedor Agarshev|Even Odd game - Fedor Agarshev]]
 
Return back to project page: [[Spike Prime - Even Odd game - Fedor Agarshev|Even Odd game - Fedor Agarshev]]
  
Python code for the Spike Pong project:
+
Python code for the Even Odd game project:
  
 
<syntaxhighlight lang=python>  
 
<syntaxhighlight lang=python>  

Latest revision as of 22:02, 6 June 2023

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

Python code for the Even Odd game project:

 
from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair
from spike.control import wait_for_seconds, wait_until, Timer
from math import *

hub = PrimeHub()
pointMotor1 = Motor("B")
pointMotor2 = Motor("D")
motorForBet = Motor("A")
buttonForceSensor = ForceSensor("E")
colorSensor = ColorSensor("F")

pointMotor1.run_to_position(0)
pointMotor2.run_to_position(0)

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):
        self.motor.run_for_degrees((new_score - self.scorePlayer) * 30 * self.invert_move)
        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
    motorPosition = motorForBet.get_position()
    hub.light_matrix.write(bet)
    while not buttonForceSensor.is_pressed():
            if motorPosition + 20 < motorForBet.get_position() :
                if  bet < Player1.scorePlayer and  bet < Player2.scorePlayer:
                    bet += 1
                motorPosition = motorForBet.get_position()
                hub.light_matrix.write(bet)
            if motorPosition - 20 > motorForBet.get_position() :
                if bet != 0:
                    bet -= 1
                motorPosition = motorForBet.get_position()
                hub.light_matrix.write(bet)
            
    return bet

def choice_even_or_odd(bet, whos_betting, who_chooses):
    while colorSensor.get_color() != "red" and colorSensor.get_color() != "green":
        pass
    color = colorSensor.get_color()
    if (color == "red" and bet % 2 == 0) or (color == "green" and bet % 2 != 0):
        hub.speaker.beep(60, 0.5)
        hub.speaker.beep(64, 0.5)
        hub.speaker.beep(60, 0.5)
        new_score1 = whos_betting.scorePlayer - bet
        new_score2 = who_chooses.scorePlayer + bet
    else :
        hub.speaker.beep(80, 0.5)
        hub.speaker.beep(84, 0.5)
        hub.speaker.beep(80, 0.5)
        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:
        hub.light_matrix.write(whos_betting.name + " makes a bet")
        bet = choice_of_bet()
        hub.speaker.beep(70, 0.5)
        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:
        hub.light_matrix.write(Player1.name + " win!")
    else :
        hub.light_matrix.write(Player2.name + " win!")

hub.speaker.set_volume(50)       
game()