Spike pong - Code for version 3
From RoboWiki
Revision as of 14:03, 2 June 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 hub import light_matrix, port, button, light, speaker
import motor
import random, color, time
motor_with_bowl = port.F
motor_left_contoller = port.A
motor_right_contoller = port.D
class game:
def __init__(self):
self.brush = [0,0]
self.speed = 50
def some_button_was_pressed(self):
return button.pressed(button.LEFT) or button.pressed(button.RIGHT)
def start(self):
while True:
light.color(light.POWER, color.WHITE)
motor.run_to_relative_position(motor_with_bowl, 0, 200)
light_matrix.write(str(self.brush[0]) + ":" + str(self.brush[1]))
time.sleep_ms(2000)
while not self.some_button_was_pressed():
pass
time.sleep_ms(2000)
self.speed = 50
self.player_who_catches = random.choice([1,-1])
self.who_win = 0
self.play()
def play(self):
while True:
light.color(light.POWER, color.BLUE)
self.generate_arrow()
self.show_arrow()
motor.run(motor_with_bowl, self.speed*self.player_who_catches)
if self.player_who_catches == 1:
self.left_player_catche()
elif self.player_who_catches == -1:
self.right_player_catche()
if self.who_win != 0:
if self.who_win == 1:
self.brush[0] += 1
elif self.who_win == -1:
self.brush[1] += 1
break
if self.speed < 1000:
self.speed += 10
def left_player_catche(self):
motor.reset_relative_position(motor_left_contoller, 0)
def motor_is_moved_to_the_correct_side(motor1, side):
if side == 'north' and 20 < motor.relative_position(motor1):
return True
elif side == 'south' and -20 > motor.relative_position(motor1):
return True
return False
while True:
if 30 == motor.relative_position(motor_with_bowl) :
light.color(light.POWER, color.GREEN)
if 90 < motor.relative_position(motor_with_bowl) < 100:
light.color(light.POWER, color.RED)
motor.stop(motor_with_bowl)
speaker.beep(70, 500, 70)
self.who_win = -1
break
elif 30 < motor.relative_position(motor_with_bowl) < 90 and motor_is_moved_to_the_correct_side(motor_left_contoller, self.side):
motor.stop(motor_with_bowl)
speaker.beep(67, 500, 67)
self.player_who_catches = -1
break
elif self.motor_is_moved(motor_left_contoller) and motor.relative_position(motor_with_bowl) < 30:
motor.stop(motor_with_bowl)
self.who_win = -1
light.color(light.POWER, color.PURPLE)
while not self.some_button_was_pressed():
pass
break
return
def right_player_catche(self):
motor.reset_relative_position(motor_right_contoller,0)
def motor_is_moved_to_the_correct_side(motor1, side):
if side == 'north' and 20 < motor.relative_position(motor1):
return True
elif side == 'south' and -20 > motor.relative_position(motor1):
return True
return False
while True:
if motor.relative_position(motor_with_bowl) == -30:
light.color(light.POWER, color.GREEN)
if -100 < motor.relative_position(motor_with_bowl) < -90:
light.color(light.POWER, color.RED)
motor.stop(motor_with_bowl)
speaker.beep(70, 500, 70)
self.who_win = 1
break
elif -90 < motor.relative_position(motor_with_bowl) < -30 and motor_is_moved_to_the_correct_side(motor_right_contoller,self.side):
motor.stop(motor_with_bowl)
speaker.beep(67, 500, 67)
self.player_who_catches = 1
break
elif self.motor_is_moved(motor_right_contoller) and motor.relative_position(motor_with_bowl) > -30:
motor.stop(motor_with_bowl)
light.color(light.POWER, color.PURPLE)
while not self.some_button_was_pressed():
pass
self.who_win = 1
break
return
def motor_is_moved(self, motor1):
position = motor.relative_position(motor1)
if 20 < position or -20 > position :
return True
return False
def generate_arrow(self):
self.side = random.choice(['south','north'])
def show_arrow(self):
field_arrow = {'south' : 37, 'north': 38}
light_matrix.show_image(field_arrow[self.side])
game1 = game()
game1.start()