Mindstorms Pong

Awatar użytkownika
Sariel
Posty: 917
Rejestracja: 01 paź 2013 10:46
Rok urodzenia: 1982
Lokalizacja: Warszawa
Kontakt:

Mindstorms Pong

Post autor: Sariel »

Obrazek

Taka tam bzdura. Zapoznaję się z kostką Mindstorms z zestawu 51515 i chciałem poćwiczyć programowanie w Pythonie, więc napisałem sobie Ponga. Oczywiście działa to tak średnio bo ekran ma tylko 5x5 pikseli więc brakuje miejsca żeby piłka polatała, ale działa. Silniki służą do kontroli paletek, a przyciskiem prawo/lewo odpala się piłkę. Program poniżej.

Kod: Zaznacz cały

from mindstorms import MSHub, Motor, MotorPair, ColorSensor, DistanceSensor, App
from mindstorms.control import wait_for_seconds, wait_until, Timer
from mindstorms.operator import greater_than, greater_than_or_equal_to, less_than, less_than_or_equal_to, equal_to, not_equal_to
import math

# config
hub = MSHub()
ball = [2, 4]
heading = 0
ball_is_out = 0;
paddleL1 = 2
paddleL2 = 3
paddleR1 = 2
paddleR2 = 3
motorA = Motor('A')
motorB = Motor('B')

# right paddle
def right_upper():
    hub.light_matrix.set_pixel(4, 0, 100)
    hub.light_matrix.set_pixel(4, 1, 100)
    hub.light_matrix.set_pixel(4, 2, 0)
    hub.light_matrix.set_pixel(4, 3, 0)
    hub.light_matrix.set_pixel(4, 4, 0)
    global palletteR1
    global palletteR2
    paddleR1 = 0
    paddleR2 = 1

def right_up():
    hub.light_matrix.set_pixel(4, 0, 0)
    hub.light_matrix.set_pixel(4, 1, 100)
    hub.light_matrix.set_pixel(4, 2, 100)
    hub.light_matrix.set_pixel(4, 3, 0)
    hub.light_matrix.set_pixel(4, 4, 0)
    global paddleR1
    global paddleR2
    paddleR1 = 1
    paddleR2 = 2

def right_mid():
    hub.light_matrix.set_pixel(4, 0, 0)
    hub.light_matrix.set_pixel(4, 1, 0)
    hub.light_matrix.set_pixel(4, 2, 100)
    hub.light_matrix.set_pixel(4, 3, 100)
    hub.light_matrix.set_pixel(4, 4, 0)
    global paddleR1
    global paddleR2
    paddleR1 = 2
    paddleR2 = 3

def right_lower():
    hub.light_matrix.set_pixel(4, 0, 0)
    hub.light_matrix.set_pixel(4, 1, 0)
    hub.light_matrix.set_pixel(4, 2, 0)
    hub.light_matrix.set_pixel(4, 3, 100)
    hub.light_matrix.set_pixel(4, 4, 100)
    global paddleR1
    global paddleR2
    paddleR1 = 3
    paddleR2 = 4

# left paddle
def left_upper():
    hub.light_matrix.set_pixel(0, 0, 100)
    hub.light_matrix.set_pixel(0, 1, 100)
    hub.light_matrix.set_pixel(0, 2, 0)
    hub.light_matrix.set_pixel(0, 3, 0)
    hub.light_matrix.set_pixel(0, 4, 0)
    global paddleL1
    global paddleL2
    paddleL1 = 0
    paddleL2 = 1

def left_up():
    hub.light_matrix.set_pixel(0, 0, 0)
    hub.light_matrix.set_pixel(0, 1, 100)
    hub.light_matrix.set_pixel(0, 2, 100)
    hub.light_matrix.set_pixel(0, 3, 0)
    hub.light_matrix.set_pixel(0, 4, 0)
    global paddleL1
    global paddleL2
    paddleL1 = 1
    paddleL2 = 2

def left_mid():
    hub.light_matrix.set_pixel(0, 0, 0)
    hub.light_matrix.set_pixel(0, 1, 0)
    hub.light_matrix.set_pixel(0, 2, 100)
    hub.light_matrix.set_pixel(0, 3, 100)
    hub.light_matrix.set_pixel(0, 4, 0)
    global paddleL1
    global paddleL2
    paddleL1 = 2
    paddleL2 = 3

def left_lower():
    hub.light_matrix.set_pixel(0, 0, 0)
    hub.light_matrix.set_pixel(0, 1, 0)
    hub.light_matrix.set_pixel(0, 2, 0)
    hub.light_matrix.set_pixel(0, 3, 100)
    hub.light_matrix.set_pixel(0, 4, 100)
    global paddleL1
    global paddleL2
    paddleL1 = 3
    paddleL2 = 4

# start-up
def startup():
    global paddleL1
    global paddleL2
    global paddleR1
    global paddleR2
    motorA.run_to_position(90)
    motorB.run_to_position(90)
    paddleL1 = 2
    paddleL2 = 3
    paddleR1 = 2
    paddleR2 = 3

    hub.light_matrix.set_pixel(0, paddleL1)
    hub.light_matrix.set_pixel(0, paddleL2)

    hub.light_matrix.set_pixel(4, paddleR1)
    hub.light_matrix.set_pixel(4, paddleR2)
    reset()

def reset():
    global ball_is_out
    ball_is_out = 0

    global ball
    ball = [2, 4]
    hub.light_matrix.set_pixel(ball[0], ball[1])
    hub.status_light.on('green')

def move_paddles():
    a = motorA.get_position()

    if a <= 70:
        right_lower()
    elif a > 70 and a <= 110:
        right_mid()
    elif a > 110 and a <= 130:
        right_up()
    else:
        right_upper()

    b = motorB.get_position()

    if b >= 110:
        left_lower()
    elif b < 110 and b >= 70:
        left_mid()
    elif b < 70 and b >= 50:
        left_up()
    else:
        left_upper()

def ball_out():
    hub.status_light.on('red')
    hub.speaker.play_sound('Hammer')
    reset()


def move_ball():
    global ball
    global heading
    hub.light_matrix.set_pixel(ball[0], ball[1], 0)

    # 12
    #X
    # 34

    if heading == 1: # NW
        ball = [ball[0] - 1, ball[1] - 1]
        if (ball[0] < 1 and ball[1] < 0):
            if (ball[1] != paddleL1 and ball[1] != paddleL2):
                hub.light_matrix.set_pixel(ball[0], ball[1], 100)
                wait_for_seconds(tempo)
                ball_out()
            else:
                ball = [2, 1]
                heading = 3
        elif (ball[0] < 1):
            if (ball[1] != paddleL1 and ball[1] != paddleL2):
                hub.light_matrix.set_pixel(ball[0], ball[1], 100)
                wait_for_seconds(tempo)
                ball_out()
            else:
                ball = [2, ball[1]]
                heading = 2
        elif (ball[1] < 0):
            ball = [ball[0], 1]
            heading = 4
    elif heading == 2: # NE
        ball = [ball[0] + 1, ball[1] - 1]
        if (ball[0] > 3 and ball[1] < 0):
            if (ball[1] != paddleR1 and ball[1] != paddleR2):
                hub.light_matrix.set_pixel(ball[0], ball[1], 100)
                wait_for_seconds(tempo)
                ball_out()
            else:
                ball = [2, 1]
                heading = 4
        elif (ball[0] > 3):
            if (ball[1] != paddleR1 and ball[1] != paddleR2):
                hub.light_matrix.set_pixel(ball[0], ball[1], 100)
                wait_for_seconds(tempo)
                ball_out()
            else:
                ball = [2, ball[1]]
                heading = 1
        elif (ball[1] < 0):
            ball = [ball[0], 1]
            heading = 3
    elif heading == 3: # SW
        ball = [ball[0] - 1, ball[1] + 1]
        if (ball[0] < 1 and ball[1] > 4):
            if (ball[1] != paddleL1 and ball[1] != paddleL2):
                hub.light_matrix.set_pixel(ball[0], ball[1], 100)
                wait_for_seconds(tempo)
                ball_out()
            else:
                ball = [2, 3]
                heading = 1
        elif (ball[0]< 1):
            if (ball[1] != paddleL1 and ball[1] != paddleL2):
                hub.light_matrix.set_pixel(ball[0], ball[1], 100)
                wait_for_seconds(tempo)
                ball_out()
            else:
                ball = [2, ball[1]]
                heading = 4
        elif (ball[1] > 4):
            ball = [ball[0], 3]
            heading = 2
    elif heading == 4: # SE
        ball = [ball[0] + 1, ball[1] + 1]
        if (ball[0] > 3 and ball[1] > 4):
            if (ball[1] != paddleR1 and ball[1] != paddleR2):
                hub.light_matrix.set_pixel(ball[0], ball[1], 100)
                wait_for_seconds(tempo)
                ball_out()
            else:
                ball = [1, 3]
                heading = 2
        elif (ball[0] > 3):
            if (ball[1] != paddleR1 and ball[1] != paddleR2):
                hub.light_matrix.set_pixel(ball[0], ball[1], 100)
                wait_for_seconds(tempo)
                ball_out()
            else:
                ball = [1, ball[1]]
                heading = 3
        elif (ball[1] > 4):
            ball = [ball[0], 3]
            heading = 1

    hub.light_matrix.set_pixel(ball[0], ball[1], 100)

def startGame():
    while True:
        move_paddles()

        if hub.right_button.is_pressed():
            hub.speaker.play_sound('Ping')
            global heading
            heading = 2
            global ball_is_out
            ball_is_out = 0

            while (ball_is_out == 0):
                move_ball()
                move_paddles()
                wait_for_seconds(tempo)

        if hub.left_button.is_pressed():
            hub.speaker.play_sound('Ping')
            global heading
            heading = 1
            global ball_is_out
            ball_is_out = 0

            while (ball_is_out == 0):
                move_ball()
                move_paddles()
                wait_for_seconds(tempo)

# --- start program ---
tempo = 0.25
startup()
startGame()

Fotki:
Obrazek Obrazek Obrazek

Film:
ODPOWIEDZ