Repository navigation
Expand file tree
/
Copy pathdefinitionen.py
More file actions
163 lines (138 loc) · 3.96 KB
/
Copy pathdefinitionen.py
File metadata and controls
163 lines (138 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/python
# -*- coding: latin-1 -*-
# Definitionen für Snake-Spiel
import turtle
segmente = []
kopf = turtle.Turtle()
kopf.shape("square")
kopf.color("black")
kopf.shape("square")
kopf.penup()
kopf.speed(0)
kopf.goto(0, 0)
kopf.direction = "stop"
essen = turtle.Turtle()
essen.shape("circle")
essen.color("red")
essen.penup()
essen.speed(0)
essen.goto(0, 100)
rechts = turtle.Turtle()
rechts.shape("triangle")
rechts.color("green")
rechts.penup()
rechts.speed(0)
rechts.goto(180, -160)
unten = turtle.Turtle()
unten.shape("triangle")
unten.color("green")
unten.right(90)
unten.penup()
unten.speed(0)
unten.goto(160, -180)
links = turtle.Turtle()
links.shape("triangle")
links.color("green")
links.right(180)
links.penup()
links.speed(0)
links.goto(140, -160)
oben = turtle.Turtle()
oben.shape("triangle")
oben.color("green")
oben.right(270)
oben.penup()
oben.speed(0)
oben.goto(160, -140)
neues_segment = turtle.Turtle()
neues_segment.shape("square")
neues_segment.color("yellow")
neues_segment.penup()
segmente.append(neues_segment)
def wiederhole_spiellogik():
checke_kollision_mit_essen()
checke_kollision_mit_fensterrand()
koerper_bewegen()
kopf_bewegen()
checke_kollision_mit_segmenten()
def interpretiere_eingabe(x, y):
print(x, y)
if (x >= 150 and x <= 170 and y >= -190 and y <= -170):
nach_unten_ausrichten()
elif (x >= 170 and x <= 190 and y >= -170 and y <= -150):
nach_rechts_ausrichten()
elif (x >= 150 and x <= 170 and y >= -150 and y <= -130):
nach_oben_ausrichten()
elif (x >= 170 and x <= 190) and (y >= -150 and y <= -130):
nach_links_ausrichten()
elif (x >= 130 and x <= 150 and y >= -170 and y <= -150):
nach_links_ausrichten()
kopf_bewegen()
def nach_unten_ausrichten():
if kopf.direction != "up":
kopf.direction = "down"
def nach_rechts_ausrichten():
if kopf.direction != "left":
kopf.direction = "right"
def nach_links_ausrichten():
if kopf.direction != "right":
kopf.direction = "left"
def nach_oben_ausrichten():
if kopf.direction != "down":
kopf.direction = "up"
def kopf_bewegen():
if kopf.direction == "down":
y = kopf.ycor()
kopf.sety(y - 20)
elif kopf.direction == "right":
x = kopf.xcor()
kopf.setx(x + 20)
elif kopf.direction == "up":
y = kopf.ycor()
kopf.sety(y + 20)
elif kopf.direction == "left":
x = kopf.xcor()
kopf.setx(x - 20)
def checke_kollision_mit_essen():
if kopf.distance(essen) < 15:
# essen an neue Position kopf_bewegen
# Schlange wachsen lassen
x = random.randint(-180, 180)
y = random.randint(-180, 180)
food.goto(x, y)
def checke_kollision_mit_fensterrand():
if kopf.xcor() < -190 or kopf.xcor() > 190 or kopf.ycor() < -190 or kopf.ycor() > 190:
time.sleep(1)
head.goto(0, 0)
head.direction = "stop"
spiel_neustarten()
# Kopf in der Mitte platzieren
# Richtung auf "stop" setzen
segmente_entfernen()
# Ausgabe, dass Spielrunde vorbei ist
def checke_kollision_mit_segmenten():
for segment in segmente:
if segment.distance(kopf) < 20:
time.sleep(1)
kopf.goto(0, 0)
kopf.direction = "stop"
segmente_entfernen()
def koerper_bewegen():
for index in range(len(segmente) - 1, 0, -1):
# bewege segmente(index) an segmente(index - 1)
x = segmente[index-1].xcor()
y = segmente[index-1].ycor()
segmente[index].goto(x, y)
if len(segmente) < 0:
x = head.xcor()
y = head.ycor()
segmente[0].goto(x, y)
# überprüfe, ob die Schlange nicht nur aus Kopf besteht
# Wenn, dann bewege erstes Segment zum Kopf
def segmente_entfernen():
# Hide the segments
for segment in segmente:
segment.goto(1000, 1000)
# clear segment list
segmente = []
#def spiel_neustarten():