Difference between revisions of "Spike pong - Code"

From RoboWiki
Jump to: navigation, search
 
Line 5: Line 5:
 
<syntaxhighlight lang=python>  
 
<syntaxhighlight lang=python>  
 
from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair
 
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 spike.control import wait_for_seconds, wait_until, Timer
from math import *
+
from math import *
import random
+
import random
+
 
hub = PrimeHub()
+
hub = PrimeHub()
motor_with_bowl = Motor('F')
+
motor_with_bowl = Motor('F')
motor_left_contoller = Motor('A')
+
motor_left_contoller = Motor('A')
motor_right_contoller = Motor('D')
+
motor_right_contoller = Motor('D')
+
 
+
 
class game:
+
class game:
    def __init__(self):
+
    def __init__(self):
        self.brush = [0,0]
+
        self.brush = [0,0]
        self.start()
+
        self.start()
+
 
    def some_button_was_pressed(self):
+
    def some_button_was_pressed(self):
        return hub.left_button.was_pressed() or hub.right_button.was_pressed()
+
        return hub.left_button.was_pressed() or hub.right_button.was_pressed()
+
 
    def start(self):
+
    def start(self):
        while True:
+
        while True:
            motor_with_bowl.set_default_speed(5)
+
            motor_with_bowl.set_default_speed(5)
            motor_with_bowl.run_to_position(0)
+
            motor_with_bowl.run_to_position(0)
            hub.light_matrix.write(str(self.brush[0]) + ":" + str(self.brush[1]))
+
            hub.light_matrix.write(str(self.brush[0]) + ":" + str(self.brush[1]))
            hub.light_matrix.show_image("ASLEEP")
+
            hub.light_matrix.show_image("ASLEEP")
            wait_until(self.some_button_was_pressed)
+
            wait_until(self.some_button_was_pressed)
            self.player_who_catches = random.choice([1,-1])
+
            self.player_who_catches = random.choice([1,-1])
            self.right_contoller_position = motor_right_contoller.get_position()
+
            self.right_contoller_position = motor_right_contoller.get_position()
            self.left_contoller_position = motor_left_contoller.get_position()
+
            self.left_contoller_position = motor_left_contoller.get_position()
            self.who_win = 0
+
            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):
+
    def play(self):
        motor_left_contoller.set_degrees_counted(0)
+
        while True:
        def motor_is_moved_to_the_correct_side(start_position, motor, side):
+
            hub.status_light.on('blue')
            if side == 'north' and 0 > motor.get_degrees_counted() + 20:
+
            self.generate_arrow()
                return True
+
            self.show_arrow()
            elif side == 'south' and 0 < motor.get_degrees_counted() - 20:
+
            self.right_contoller_position = motor_right_contoller.get_position()
                return True
+
            self.left_contoller_position = motor_left_contoller.get_position()
            return False
+
            motor_with_bowl.start(speed=motor_with_bowl.get_default_speed()*self.player_who_catches)
        while True:
+
            if self.player_who_catches == 1:
            if 30 == motor_with_bowl.get_position() :
+
                self.left_player_catche()
                hub.status_light.on('green')
+
            elif self.player_who_catches == -1:
            if 90 < motor_with_bowl.get_position() < 100:
+
                self.right_player_catche()
                hub.status_light.on('red')
+
            if self.who_win != 0:
                motor_with_bowl.stop()
+
                if self.who_win == 1:
                hub.speaker.beep(70, 0.5)
+
                    self.brush[0] += 1
                hub.speaker.beep(77, 0.5)
+
                elif self.who_win == -1:
                hub.speaker.beep(70, 0.5)
+
                    self.brush[1] += 1
                self.who_win = -1
+
                break
                break
+
            if motor_with_bowl.get_default_speed() < 100:
            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.set_default_speed(motor_with_bowl.get_default_speed() + 1)
                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:
+
    def left_player_catche(self):
                motor_with_bowl.stop()
+
        motor_left_contoller.set_degrees_counted(0)
                self.who_win = -1
+
        def motor_is_moved_to_the_correct_side(start_position, motor, side):
                hub.status_light.on('violet')
+
            if side == 'north' and 0 > motor.get_degrees_counted() + 20:
                hub.light_matrix.show_image("SKULL")
+
                return True
                wait_until(self.some_button_was_pressed)
+
            elif side == 'south' and 0 < motor.get_degrees_counted() - 20:
                break
+
                return True
+
            return False
        return
+
        while True:
+
            if 30 == motor_with_bowl.get_position() :
    def right_player_catche(self):
+
                hub.status_light.on('green')
        motor_right_contoller.set_degrees_counted(0)
+
            if 90 < motor_with_bowl.get_position() < 100:
        def motor_is_moved_to_the_correct_side(start_position, motor, side):
+
                hub.status_light.on('red')
            if side == 'north' and 0 < motor.get_degrees_counted() - 20:
+
                motor_with_bowl.stop()
                return True
+
                hub.speaker.beep(70, 0.5)
            elif side == 'south' and 0 > motor.get_degrees_counted() + 20:
+
                hub.speaker.beep(77, 0.5)
                return True
+
                hub.speaker.beep(70, 0.5)
            return False
+
                self.who_win = -1
        while True:
+
                break
            if motor_with_bowl.get_position() == 330:
+
            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):
                hub.status_light.on('green')
+
                motor_with_bowl.stop()
            if 260 < motor_with_bowl.get_position() < 270:
+
                hub.speaker.beep(67, 0.1)
                hub.status_light.on('red')
+
                self.player_who_catches = -1
                motor_with_bowl.stop()
+
                break
                hub.speaker.beep(70, 0.5)
+
            elif self.motor_is_moved(self.left_contoller_position, motor_left_contoller) and motor_with_bowl.get_position() < 30:
                hub.speaker.beep(77, 0.5)
+
                motor_with_bowl.stop()
                hub.speaker.beep(70, 0.5)
+
                self.who_win = -1
                self.who_win = 1
+
                hub.status_light.on('violet')
                break
+
                hub.light_matrix.show_image("SKULL")
            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):
+
                wait_until(self.some_button_was_pressed)
                motor_with_bowl.stop()
+
                break
                hub.speaker.beep(67, 0.1)
+
 
                self.player_who_catches = 1
+
        return
                break
+
 
            elif self.motor_is_moved(self.right_contoller_position, motor_right_contoller) and motor_with_bowl.get_position() > 330:
+
    def right_player_catche(self):
                motor_with_bowl.stop()
+
        motor_right_contoller.set_degrees_counted(0)
                hub.status_light.on('violet')
+
        def motor_is_moved_to_the_correct_side(start_position, motor, side):
                hub.light_matrix.show_image("SKULL")
+
            if side == 'north' and 0 < motor.get_degrees_counted() - 20:
                wait_until(self.some_button_was_pressed)
+
                return True
                self.who_win = 1
+
            elif side == 'south' and 0 > motor.get_degrees_counted() + 20:
                break
+
                return True
        return
+
            return False
+
        while True:
    def motor_is_moved(self, start_position, motor):
+
            if motor_with_bowl.get_position() == 330:
        position = motor.get_position()
+
                hub.status_light.on('green')
        if start_position > position + 20 or start_position < position - 20:
+
            if 260 < motor_with_bowl.get_position() < 270:
            return True
+
                hub.status_light.on('red')
        return False
+
                motor_with_bowl.stop()
+
                hub.speaker.beep(70, 0.5)
   
+
                hub.speaker.beep(77, 0.5)
   
+
                hub.speaker.beep(70, 0.5)
    def generate_arrow(self):
+
                self.who_win = 1
        self.side = random.choice(['south','north'])
+
                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):
    def show_arrow(self):
+
                motor_with_bowl.stop()
        field_arrow = {'south' : 'ARROW_S', 'north': 'ARROW_N'}
+
                hub.speaker.beep(67, 0.1)
        hub.light_matrix.show_image(field_arrow[self.side])
+
                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')
game = game()
+
                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()
 +
game.play()
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 12:53, 23 April 2023

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 *
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
                
         

    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()
game.play()