Difference between revisions of "Spike pong - Code"

From RoboWiki
Jump to: navigation, search
 
(6 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 *
 
import random
 
  
hub = PrimeHub()
+
Python code for the Spike Pong project:
motor_with_bowl = Motor('F')
+
 
motor_left_contoller = Motor('A')
+
<syntaxhighlight lang=python>
motor_right_contoller = Motor('D')
+
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 *
class game:
+
import random
    def __init__(self):
+
 
        self.brush = [0,0]
+
hub = PrimeHub()
        self.start()
+
motor_with_bowl = Motor('F')
+
motor_left_contoller = Motor('A')
    def some_button_was_pressed(self):
+
motor_right_contoller = Motor('D')
        return hub.left_button.was_pressed() or hub.right_button.was_pressed()
+
 
+
 
    def start(self):
+
class game:
        while True:
+
    def __init__(self):
            motor_with_bowl.set_default_speed(10)
+
        self.brush = [0,0]
            motor_with_bowl.run_to_position(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.write(str(self.brush[0]) + ":" + str(self.brush[1]))
 
             hub.light_matrix.show_image("ASLEEP")
 
             hub.light_matrix.show_image("ASLEEP")
Line 29: Line 34:
 
             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()
 
 
                  
 
                  
 
          
 
          
Line 58: Line 62:
  
 
     def left_player_catche(self):
 
     def left_player_catche(self):
         def motor_is_moved_to_the_right_side(start_position, motor, side):
+
        motor_left_contoller.set_degrees_counted(0)
            position = motor.get_position()
+
         def motor_is_moved_to_the_correct_side(start_position, motor, side):
             if side == 'north' and start_position > position + 20:
+
             if side == 'north' and 0 > motor.get_degrees_counted() + 20:
 
                 return True
 
                 return True
             elif side == 'south' and start_position < position - 20:
+
             elif side == 'south' and 0 < motor.get_degrees_counted() - 20:
 
                 return True
 
                 return True
 
             return False
 
             return False
Line 71: Line 75:
 
                 hub.status_light.on('red')
 
                 hub.status_light.on('red')
 
                 motor_with_bowl.stop()
 
                 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
 
                 self.who_win = -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):
+
             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()
 
                 motor_with_bowl.stop()
 +
                hub.speaker.beep(67, 0.1)
 
                 self.player_who_catches = -1
 
                 self.player_who_catches = -1
 
                 break
 
                 break
Line 88: Line 96:
  
 
     def right_player_catche(self):
 
     def right_player_catche(self):
         def motor_is_moved_to_the_right_side(start_position, motor, side):
+
        motor_right_contoller.set_degrees_counted(0)
            position = motor.get_position()
+
         def motor_is_moved_to_the_correct_side(start_position, motor, side):
             if side == 'north' and start_position < position - 20:
+
             if side == 'north' and 0 < motor.get_degrees_counted() - 20:
 
                 return True
 
                 return True
             elif side == 'south' and start_position > position + 20:
+
             elif side == 'south' and 0 > motor.get_degrees_counted() + 20:
 
                 return True
 
                 return True
 
             return False
 
             return False
Line 101: Line 109:
 
                 hub.status_light.on('red')
 
                 hub.status_light.on('red')
 
                 motor_with_bowl.stop()
 
                 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
 
                 self.who_win = 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 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()
 
                 motor_with_bowl.stop()
 +
                hub.speaker.beep(67, 0.1)
 
                 self.player_who_catches = 1
 
                 self.player_who_catches = 1
 
                 break
 
                 break
Line 135: Line 147:
  
 
game = game()
 
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()