Even Odd game - Code
From RoboWiki
Revision as of 12:58, 23 April 2023 by Robot (talk | contribs) (Created page with "Return back to project page: Spike pong - Fedor Agarshev Python code for the Spike Pong project: <syntaxhighlight lang=python>...")
Return back to project page: Spike pong - Fedor Agarshev
Python code for the Spike Pong 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)
Player2 = player("Player2", pointMotor2, -1)
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 = 1
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 != 1:
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()