Pin ball - Code

From RoboWiki
Revision as of 16:56, 26 April 2023 by Robot (talk | contribs) (Created page with "Return back to project page: Pin ball - Fedor Agarshev Python code for the Spike Pong project: <syntaxhighlight lang=python> fro...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Return back to project page: Pin ball - 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 spike.operator import equal_to
from math import *

hub = PrimeHub()
colorSensor = ColorSensor("D")
motorWithColorSensor = Motor("F")
timer = Timer()
two_seconds_timer = Timer()
distance = DistanceSensor("A")
motorPointer = Motor("B")
motorPlunger = Motor("E")
count = 0



def show_timer(pixels_list):
    if two_seconds_timer.now() > 2:
        pixel = pixels_list.pop()
        hub.light_matrix.set_pixel(pixel[0], pixel[1],0)
        for pixel in pixels_list:
            hub.light_matrix.set_pixel(pixel[0], pixel[1],100)
        two_seconds_timer.reset()

    return pixels_list
    

def start(withTimer = False):
    global count
    count = 0
    motorWithColorSensor.run_to_position(240)
    motorWithColorSensor.run_to_position(0)
    motorPlunger.run_to_position(290)
    motorPlunger.run_to_position(20)
    hub.light_matrix.show_image('SQUARE')
    pixels_list = [(2,0),(3,0),(4,0),(4,1),(4,2),(4,3),(4,4),(3,4),(2,4),(1,4),(0,4),(0,3),(0,2),(0,1),(0,0),(1,0)]
    timer.reset()
    two_seconds_timer.reset()
    pos = motorPointer.get_position()
    
    while True:    
        if withTimer == True:
            pixels_list = show_timer(pixels_list)
        if hub.left_button.was_pressed():
            motorPlunger.run_to_position(290)
            motorPlunger.run_to_position(20)
            hub.light_matrix.write(count)
            
        if colorSensor.get_color() == "green":
            print("color")
            count += 20
            motorWithColorSensor.run_for_degrees(-120)
            motorWithColorSensor.run_for_degrees(120)
            hub.speaker.beep(60, 0.5)
        if distance.get_distance_cm(True) is None:
            print("distance")
            count += 2
            hub.speaker.beep(70, 0.5)
        p = motorPointer.get_position()
        if  p < pos -2 or p > pos + 2:
            print("motor")
            count += 1
            pos = p
            hub.speaker.beep(65, 0.5)
        if withTimer == True:
            if timer.now() > 40:
                break




while True:
    hub.left_button.was_pressed()
    while not hub.left_button.was_pressed():
        hub.light_matrix.write(count)
        wait_for_seconds(3)
    start(True)