|
| 1 | +# This example is based on content from the 2020 openHPI course |
| 2 | +# "Programmieren lernen mit Python" ("Learning to Program with Python") |
| 3 | +# Authors: Kira Grammel, Nina Ihde, Selina Reinhard, and Sebastian Serth |
| 4 | +# Published by: openHPI, Hasso-Plattner-Institut für Digital Engineering gGmbH |
| 5 | +# License: CC BY-NC-SA 4.0, https://creativecommons.org/licenses/by-nc-sa/4.0 |
| 6 | +# |
| 7 | +# The course is freely available at: https://open.hpi.de/courses/pythonjunior2020 |
| 8 | + |
| 9 | +from turtle import * |
| 10 | + |
| 11 | +def erstelle_turtle(x, y, rotationsWinkel = 0, shape = "triangle", color = "green"): |
| 12 | + steuerung = Turtle() |
| 13 | + steuerung.speed(0) # schnellste Animationsgeschwindigkeit, um sichtbare Bewegung zu vermeiden |
| 14 | + steuerung.shape(shape) |
| 15 | + steuerung.color(color) |
| 16 | + steuerung.right(rotationsWinkel) |
| 17 | + steuerung.penup() |
| 18 | + steuerung.goto(x, y) |
| 19 | + steuerung.direction = "stop" # nur für Kopf relevant |
| 20 | + return steuerung |
| 21 | + |
| 22 | +rechts = erstelle_turtle(180, -160) |
| 23 | +unten = erstelle_turtle(160, -180, 90) |
| 24 | +links = erstelle_turtle(140, -160, 180) |
| 25 | +oben = erstelle_turtle(160, -140, 270) |
| 26 | +kopf = erstelle_turtle(0, 0, 0, "square", "black") |
| 27 | + |
| 28 | +def setze_richtung(dahin, dahinNicht): |
| 29 | + # Vermeiden, dass Schlangenkopf zurück in sich selbst laufen kann |
| 30 | + if kopf.direction != dahinNicht: |
| 31 | + kopf.direction = dahin |
| 32 | + kopf_bewegen() |
| 33 | + |
| 34 | +def kopf_bewegen(): |
| 35 | + if kopf.direction == "down": |
| 36 | + y = kopf.ycor() |
| 37 | + kopf.sety(y - 20) |
| 38 | + |
| 39 | + elif kopf.direction == "right": |
| 40 | + x = kopf.xcor() |
| 41 | + kopf.setx(x + 20) |
| 42 | + |
| 43 | + elif kopf.direction == "up": |
| 44 | + y = kopf.ycor() |
| 45 | + kopf.sety(y + 20) |
| 46 | + |
| 47 | + elif kopf.direction == "left": |
| 48 | + x = kopf.xcor() |
| 49 | + kopf.setx(x - 20) |
| 50 | + |
| 51 | + |
| 52 | +def checke_kollision_mit_fensterrand(): |
| 53 | + if kopf.xcor() > 190 or kopf.xcor() < -190 or kopf.ycor() > 190 or kopf.ycor() < -190: |
| 54 | + spiel_neustarten() |
| 55 | + |
| 56 | + |
| 57 | +def interpretiere_eingabe(x, y): |
| 58 | + if (x >= 170 and x <= 190 and y >= -170 and y <= -150): |
| 59 | + setze_richtung("right", "left") |
| 60 | + elif (x >= 150 and x <= 170 and y >= -190 and y <= -170): |
| 61 | + setze_richtung("down", "up") |
| 62 | + elif (x >= 130 and x <= 150 and y >= -170 and y <= -150): |
| 63 | + setze_richtung("left", "right") |
| 64 | + elif (x >= 150 and x <= 170 and y >= -150 and y <= -130): |
| 65 | + setze_richtung("up", "down") |
| 66 | + else: |
| 67 | + return None |
| 68 | + checke_kollision_mit_fensterrand() |
| 69 | + |
| 70 | +def spiel_neustarten(): |
| 71 | + kopf.goto(0, 0) |
| 72 | + kopf.direction = "stop" |
| 73 | + |
| 74 | +onclick(interpretiere_eingabe) |
| 75 | +mainloop() |
0 commit comments