Difference between revisions of "Spike pong - Code"

From RoboWiki
Jump to: navigation, search
Line 1: Line 1:
    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(10)
+
            motor_with_bowl.set_default_speed(10)
                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()
+
            self.play()
                   
+
               
           
+
         
 
+
        def play(self):
+
    def play(self):
            while True:
+
        while True:
                hub.status_light.on('blue')
+
            hub.status_light.on('blue')
                self.generate_arrow()
+
            self.generate_arrow()
                self.show_arrow()
+
            self.show_arrow()
                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()
                motor_with_bowl.start(speed=motor_with_bowl.get_default_speed()*self.player_who_catches)
+
            motor_with_bowl.start(speed=motor_with_bowl.get_default_speed()*self.player_who_catches)
                if self.player_who_catches == 1:
+
            if self.player_who_catches == 1:
                    self.left_player_catche()
+
                self.left_player_catche()
                elif self.player_who_catches == -1:
+
            elif self.player_who_catches == -1:
                    self.right_player_catche()
+
                self.right_player_catche()
                if self.who_win != 0:
+
            if self.who_win != 0:
                    if self.who_win == 1:
+
                if self.who_win == 1:
                        self.brush[0] += 1
+
                    self.brush[0] += 1
                    elif self.who_win == -1:
+
                elif self.who_win == -1:
                        self.brush[1] += 1
+
                    self.brush[1] += 1
                    break
+
                break
                if motor_with_bowl.get_default_speed() < 100:
+
            if motor_with_bowl.get_default_speed() < 100:
                    motor_with_bowl.set_default_speed(motor_with_bowl.get_default_speed() + 1)
+
                motor_with_bowl.set_default_speed(motor_with_bowl.get_default_speed() + 1)
           
+
       
 
+
 
+
 
+
        def left_player_catche(self):
+
    def left_player_catche(self):
            def motor_is_moved_to_the_right_side(start_position, motor, side):
+
        def motor_is_moved_to_the_right_side(start_position, motor, side):
                position = motor.get_position()
+
            position = motor.get_position()
                if side == 'north' and start_position > position + 20:
+
            if side == 'north' and start_position > position + 20:
                    return True
+
                return True
                elif side == 'south' and start_position < position - 20:
+
            elif side == 'south' and start_position < position - 20:
                    return True
+
                return True
                return False
+
            return False
            while True:
+
        while True:
                if 30 == motor_with_bowl.get_position() :
+
            if 30 == motor_with_bowl.get_position() :
                    hub.status_light.on('green')
+
                hub.status_light.on('green')
                if 90 < motor_with_bowl.get_position() < 100:
+
            if 90 < motor_with_bowl.get_position() < 100:
                    hub.status_light.on('red')
+
                hub.status_light.on('red')
                    motor_with_bowl.stop()
+
                motor_with_bowl.stop()
                    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_right_side(self.left_contoller_position, motor_left_contoller, self.side):
                    motor_with_bowl.stop()
+
                motor_with_bowl.stop()
                    self.player_who_catches = -1
+
                self.player_who_catches = -1
                    break
+
                break
                elif self.motor_is_moved(self.left_contoller_position, motor_left_contoller) and motor_with_bowl.get_position() < 30:
+
            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.who_win = -1
+
                self.who_win = -1
                    hub.status_light.on('violet')
+
                hub.status_light.on('violet')
                    hub.light_matrix.show_image("SKULL")
+
                hub.light_matrix.show_image("SKULL")
                    wait_until(self.some_button_was_pressed)
+
                wait_until(self.some_button_was_pressed)
                    break
+
                break
 
+
            return
+
        return
 
+
        def right_player_catche(self):
+
    def right_player_catche(self):
            def motor_is_moved_to_the_right_side(start_position, motor, side):
+
        def motor_is_moved_to_the_right_side(start_position, motor, side):
                position = motor.get_position()
+
            position = motor.get_position()
                if side == 'north' and start_position < position - 20:
+
            if side == 'north' and start_position < position - 20:
                    return True
+
                return True
                elif side == 'south' and start_position > position + 20:
+
            elif side == 'south' and start_position > position + 20:
                    return True
+
                return True
                return False
+
            return False
            while True:
+
        while True:
                if motor_with_bowl.get_position() == 330:
+
            if motor_with_bowl.get_position() == 330:
                    hub.status_light.on('green')
+
                hub.status_light.on('green')
                if 260 < motor_with_bowl.get_position() < 270:
+
            if 260 < motor_with_bowl.get_position() < 270:
                    hub.status_light.on('red')
+
                hub.status_light.on('red')
                    motor_with_bowl.stop()
+
                motor_with_bowl.stop()
                    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_right_side(self.right_contoller_position, motor_right_contoller,self.side):
                    motor_with_bowl.stop()
+
                motor_with_bowl.stop()
                    self.player_who_catches = 1
+
                self.player_who_catches = 1
                    break
+
                break
                elif self.motor_is_moved(self.right_contoller_position, motor_right_contoller) and motor_with_bowl.get_position() > 330:
+
            elif self.motor_is_moved(self.right_contoller_position, motor_right_contoller) and motor_with_bowl.get_position() > 330:
                    motor_with_bowl.stop()
+
                motor_with_bowl.stop()
                    hub.status_light.on('violet')
+
                hub.status_light.on('violet')
                    hub.light_matrix.show_image("SKULL")
+
                hub.light_matrix.show_image("SKULL")
                    wait_until(self.some_button_was_pressed)
+
                wait_until(self.some_button_was_pressed)
                    self.who_win = 1
+
                self.who_win = 1
                    break
+
                break
            return
+
        return
 
+
        def motor_is_moved(self, start_position, motor):
+
    def motor_is_moved(self, start_position, motor):
            position = motor.get_position()
+
        position = motor.get_position()
            if start_position > position + 20 or start_position < position - 20:
+
        if start_position > position + 20 or start_position < position - 20:
                return True
+
            return True
            return False
+
        return False
 
+
       
+
   
       
+
   
        def generate_arrow(self):
+
    def generate_arrow(self):
            self.side = random.choice(['south','north'])
+
        self.side = random.choice(['south','north'])
 
+
        def show_arrow(self):
+
    def show_arrow(self):
            field_arrow = {'south' : 'ARROW_S', 'north': 'ARROW_N'}
+
        field_arrow = {'south' : 'ARROW_S', 'north': 'ARROW_N'}
            hub.light_matrix.show_image(field_arrow[self.side])
+
        hub.light_matrix.show_image(field_arrow[self.side])
           
+
       
 
+
 
+
 
+
    game = game()
+
game = game()

Revision as of 12:10, 22 April 2023

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(10)
            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):
        def motor_is_moved_to_the_right_side(start_position, motor, side):
            position = motor.get_position()
            if side == 'north' and start_position > position + 20:
                return True
            elif side == 'south' and start_position < position - 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()
                self.who_win = -1
                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):
                motor_with_bowl.stop()
                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):
        def motor_is_moved_to_the_right_side(start_position, motor, side):
            position = motor.get_position()
            if side == 'north' and start_position < position - 20:
                return True
            elif side == 'south' and start_position > position + 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()
                self.who_win = 1
                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):
                motor_with_bowl.stop()
                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()