Difference between revisions of "Spike pong - Code"

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

Latest revision as of 11: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()