Spike pong - Code
From RoboWiki
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 * import random hub = PrimeHub() motor_with_bowl = Motor('F') motor_left_contoller = Motor('A') motor_right_contoller = Motor('D') class game: def __init__(self): self.brush = [0,0] self.start() def some_button_was_pressed(self): return hub.left_button.was_pressed() or hub.right_button.was_pressed() def start(self): while True: motor_with_bowl.set_default_speed(5) motor_with_bowl.run_to_position(0) hub.light_matrix.write(str(self.brush[0]) + ":" + str(self.brush[1])) hub.light_matrix.show_image("ASLEEP") wait_until(self.some_button_was_pressed) self.player_who_catches = random.choice([1,-1]) self.right_contoller_position = motor_right_contoller.get_position() self.left_contoller_position = motor_left_contoller.get_position() self.who_win = 0 self.play() def play(self): while True: hub.status_light.on('blue') self.generate_arrow() self.show_arrow() self.right_contoller_position = motor_right_contoller.get_position() self.left_contoller_position = motor_left_contoller.get_position() motor_with_bowl.start(speed=motor_with_bowl.get_default_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 motor_with_bowl.get_default_speed() < 100: motor_with_bowl.set_default_speed(motor_with_bowl.get_default_speed() + 1) def left_player_catche(self): motor_left_contoller.set_degrees_counted(0) def motor_is_moved_to_the_correct_side(start_position, motor, side): if side == 'north' and 0 > motor.get_degrees_counted() + 20: return True elif side == 'south' and 0 < motor.get_degrees_counted() - 20: return True return False while True: if 30 == motor_with_bowl.get_position() : hub.status_light.on('green') if 90 < motor_with_bowl.get_position() < 100: hub.status_light.on('red') motor_with_bowl.stop() hub.speaker.beep(70, 0.5) hub.speaker.beep(77, 0.5) hub.speaker.beep(70, 0.5) self.who_win = -1 break elif 30 < motor_with_bowl.get_position() < 90 and motor_is_moved_to_the_correct_side(self.left_contoller_position, motor_left_contoller, self.side): motor_with_bowl.stop() hub.speaker.beep(67, 0.1) self.player_who_catches = -1 break elif self.motor_is_moved(self.left_contoller_position, motor_left_contoller) and motor_with_bowl.get_position() < 30: motor_with_bowl.stop() self.who_win = -1 hub.status_light.on('violet') hub.light_matrix.show_image("SKULL") wait_until(self.some_button_was_pressed) break return def right_player_catche(self): motor_right_contoller.set_degrees_counted(0) def motor_is_moved_to_the_correct_side(start_position, motor, side): if side == 'north' and 0 < motor.get_degrees_counted() - 20: return True elif side == 'south' and 0 > motor.get_degrees_counted() + 20: return True return False while True: if motor_with_bowl.get_position() == 330: hub.status_light.on('green') if 260 < motor_with_bowl.get_position() < 270: hub.status_light.on('red') motor_with_bowl.stop() hub.speaker.beep(70, 0.5) hub.speaker.beep(77, 0.5) hub.speaker.beep(70, 0.5) self.who_win = 1 break elif 270 < motor_with_bowl.get_position() < 330 and motor_is_moved_to_the_correct_side(self.right_contoller_position, motor_right_contoller,self.side): motor_with_bowl.stop() hub.speaker.beep(67, 0.1) self.player_who_catches = 1 break elif self.motor_is_moved(self.right_contoller_position, motor_right_contoller) and motor_with_bowl.get_position() > 330: motor_with_bowl.stop() hub.status_light.on('violet') hub.light_matrix.show_image("SKULL") wait_until(self.some_button_was_pressed) self.who_win = 1 break return def motor_is_moved(self, start_position, motor): position = motor.get_position() if start_position > position + 20 or start_position < position - 20: return True return False def generate_arrow(self): self.side = random.choice(['south','north']) def show_arrow(self): field_arrow = {'south' : 'ARROW_S', 'north': 'ARROW_N'} hub.light_matrix.show_image(field_arrow[self.side]) game = game()