Schülerarbeit: Leiterlispielrennen

Turtle Graphics

Das Leiterlispiel von Silja Brändle ist die Nachbildung eines bekannten Brettspiels. Das Programm ist zwar nur auf eine Person ausgelegt, dafür funktionieren auch alle Spezialfelder.

Der Programm-Code

#
# (c) 2014, Silja Braendle
#
from gturtle import*
from random import randint
from time import sleep

def Leiter(x, y):
    setLineWidth(4)
    setPenColor("brown")
    setFillColor("blue")
    setPos(x, y)
    fill()
    y += 60
    moveTo(x, y)
    setPos(x+3, y)
    setFillColor("skyblue")
    fill()
    repeat 2:
        y -= 20
        setPos(x, y)
        x += 20
        moveTo(x, y)
        x -= 20
    y -= 20
    x += 20
    setPos(x, y)
    y += 60
    moveTo(x, y)

def grLeiter(x, y):
    setLineWidth(4)
    setPenColor("brown")
    setFillColor("blue")
    setPos(x, y)
    fill()
    y += 300
    moveTo(x, y)
    setPos(x+3, y)
    setFillColor("skyblue")
    fill()
    repeat 14:
        y -= 20
        setPos(x, y)
        x += 20
        moveTo(x, y)
        x -= 20
    y -= 20
    x += 20
    setPos(x, y)
    y += 300
    moveTo(x, y)
    
def Rutschbahn(x, y, x2, y2):
    setPenColor("brown")
    setFillColor("blue") 
    setPos(x, y)
    fill()
    right(90)
    forward(30)       
    repeat 5:
        forward(5)
        right(10)
    moveTo(x2, y2)
    setPos(x2+3, y2)
    setFillColor("skyblue")
    fill()
    # Männlein auf Rutschbahn
    x2 = x2 - 40
    y2 = y2 + 30
    setPos(x2, y2)
    setPenColor("blue")
    forward(20)
    left(90)
    forward(5)
    setPos(x2, y2)
    left(40)
    forward(30)
    dot(20)
    right(180)
    forward(12)
    left(90)
    forward(20)
    dot(10)
    left(88)
    
def zweimalWuerfeln(x, y):
    setPos(x, y)
    setFillColor("yellow")
    fill()
    
def zurueckzumStart(x, y):
    setPos(x, y)
    setFillColor("pink")
    fill()
                   
def Spielfeld(x, y):
    setPenColor("black")
    setLineWidth(3)
    repeat 10:
        setPos(x, y)
        x *= -1
        moveTo(x, y)
        x *= -1
        y += 60
    repeat 10:
        setPos(x, y)
        y *= -1
        moveTo(x, y)
        y *= -1
        x += 80
    # Startfeld
    setPos(-390, -280)
    setFillColor("green")
    fill()
    label("Start")
    # Zielfeld
    setPos(-390, 270)
    setFillColor("red")
    fill()
    label("Ziel")
    Leiter(-230, -270)
    Leiter(170, 150)
    Leiter(-310, 30)
    grLeiter(330, -210)
    Rutschbahn(-60, -40, 120, -150)
    Rutschbahn(-300, 260, -120, 150)
    zweimalWuerfeln(-350, -70)
    zweimalWuerfeln(150, 270) 
    zurueckzumStart(170, -10) 
    setPenColor("black")
    # Felder nummerieren
    x = -360
    y = -270
    n = 1
    repeat 5:
        if x <= 0:
            repeat 10:
                setPos(x, y)
                label(str(n))
                x += 80
                n += 1
            y += 60
        if x >= 0:
            repeat 10:
                x -= 80
                setPos(x, y) 
                label(str(n))
                n += 1 
                if n == 100:
                    break 
            y += 60   
             
# Spielfigur
def Spieler(color):
    setPenColor(color)
    dot(20)

def pixels(pixel):
    if pixel == "brown":
        setPenColor("blue")
    if pixel == "blue":
        setPenColor("blue")
    if pixel == "white":
        setPenColor("white")
    if pixel == "skyblue":
        setPenColor("skyblue")
    if pixel == "yellow":
        setPenColor("yellow")
    if pixel == "pink":
        setPenColor("pink")

# Spezialfelder
def kleineLeiter(x, y):
    y += 60
    setPos(x, y)
    return x, y
    
def langeLeiter(x, y):
    y += 300
    x += 1
    setPos(x, y)
    
def Rutschbahnausfuhrung(x, y):
    x += 160
    y -= 120
    setPos(x, y)
    
def gelbeFelder(x, y, a, b, color, Name, pixel, Zuege):
    global Spieler
    msgDlg("Sie bekommen einen Zug geschenkt")
    Wuerfelzahl = randint(1,6) 
    msgDlg(Name,"Sie haben", Wuerfelzahl, "gewürfelt")
    # Fahren  
    penDown()            
    Spieler(color)
    sleep(0.5)
    setPos(x, y)
    setPenColor("yellow")
    dot(20)
    penUp()
    repeat Wuerfelzahl:
        if x == 370:
            y += b
            x -= 1
            a *= -1
            x += 80
        if x == -351:
            y += b
            x += 1
            a *= -1
        else:
            x += a
        setPos(x, y)
        penDown()
        pixel = getPixelColorStr()
        if pixel == "red":
                Spieler(color)
                msgDlg("Sie sind im Ziel und haben", Zuege,
                       "Züge gebraucht")
                setPenColor("red")
                dot(20)
                return Name, Zuege
                
        Spieler(color)     
        sleep(0.5)
        pixels(pixel)
        dot(20)
        penUp()
        
def pinkesFeld(Name):
    msgDlg(Name, "gehe zurück zum Start")
    x = -350
    y = -280
    setPos(x, y)
    
# Fahren
def onClick(x, y):
    global pixels
    global Spieler
    global kleineLeiter
    global langeLeiter
    global Rutschbahnausfuhrung
    global gelbeFelder
    global pinkesFeld
    liste = []
    x = -350
    y = -280
    a = 80
    b = 60
    right(90)
    setPos(x, y)
    Name = input("Bitte geben Sie ihren Namen ein:")
    color = input("Wählen Sie eine Farbe von: orange, black, "+
                  "violet, gray, silver, gold")    
    Spieler(str(color))
    Zuege = 0
    pixel = 0
    repeat: 
        Wuerfelzahl = randint(1,6)
        Zuege += 1 
        msgDlg(Name,"Sie haben", Wuerfelzahl, "gewürfelt")
        # Fahren
        pixels(pixel) 
        penDown()            
        dot(20)
        setPos(x, y)
        penUp()
        repeat Wuerfelzahl:
            if x == 370:
                y += b
                x -= 1
                a *= -1
                x += 80
            if x == -351:
                y += b
                x += 1
                a *= -1
            else:
                x += a
            setPos(x, y)
            penDown()
            pixel = getPixelColorStr()
            if pixel == "red":
                Spieler(color)
                msgDlg("Sie sind im Ziel und haben", Zuege,
                       "Züge gebraucht")
                setPenColor("red")
                dot(20)
                return Name, Zuege  
            Spieler(color)    
            sleep(0.5)
            pixels(pixel)
            dot(20)
            penUp()
        if x >= -400 and x <= -320 and y == -100:
            gelbeFelder(x, y, a, b, color, Name, pixel, Zuege)
            a *= -1
        elif x >= 80 and x <= 160 and y == 260:
            gelbeFelder(x, y, a, b, color, Name, pixel, Zuege)
        elif x >= 160 and x <= 240 and y == -40:
            pinkesFeld(Name)   
        elif x >= -240 and x <= -160 and y == -280:
            x -= 1
            kleineLeiter(x, y)  
            a *= -1   
        elif x >= 320 and x <= 400 and y == -220:
            langeLeiter(x, y) 
            a *= -1     
        elif x >= -80 and x <= 0 and y == -40:
            Rutschbahnausfuhrung(x, y)
        elif x >= -320 and x <= -240 and y == 20:
            x+= 1
            kleineLeiter(x, y)
            a *= -1
        elif x >= 160 and x <= 240 and y == 140: 
            x += 1
            kleineLeiter(x, y)
            a *= -1
        elif x >= -320 and x <= -240 and y == 260:
            Rutschbahnausfuhrung(x, y) 
            x += 1 
        x = getX()
        y = getY()
        setPos(x, y)    
        penDown()
        pixel = getPixelColorStr()
        Spieler(color)
                                                                                                  
makeTurtle(mouseHit = onClick)
hideTurtle()
Spielfeld(-400, -300)
msgDlg("Drücken Sie auf das Feld um zu starten")