Ich versuche mich gerade an dem Snake Spiel, jedoch stoße ich gerade auf ein Problem. Wenn ich nach oben oder unten gehe, taucht mein zweites Segment auf einmal irgendwo anders auf. Woran könnte das liegen? Außerdem reagiert das "Futter2 nicht, wenn es gefressen wird

Code: Alles auswählen
import turtle, time, random
win=turtle.Screen()
win.title("Mein Snake-Spiel")
win.bgcolor("blue")
win.setup(width=600,height=600)
win.tracer(0)
head=turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0,100)
head.direction = "stop"
def move():
if head.direction == "up":
y = head.ycor()
head.sety(y + 20)
if head.direction == "down":
y = head.ycor()
head.sety(y - 20)
if head.direction == "right":
x = head.xcor()
head.setx(x + 20)
if head.direction == "left":
x= head.xcor()
head.setx(x - 20)
def go_up():
if head.direction !="down":
head.direction = "up"
def go_down():
if head.direction != "up":
head.direction = "down"
def go_right():
if head.direction != "left":
head.direction = "right"
def go_left():
if head.direction != "right":
head.direction = "left"
win.listen()
win.onkey(go_up, "w")
win.onkey(go_down, "s")
win.onkey(go_right, "d")
win.onkey(go_left, "a")
#food
food=turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.shapesize(0.50, 0.50)
food.goto (0, 0)
if head.distance(food) <15:
x = random.randint (-290, 290)
y = random.randint (-290, 290)
food.goto(x, y)
#neues Segment
segments = []
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.color("grey")
new_segment.shape("square")
new_segment.penup()
segments.append(new_segment)
# move the end segment in reverse order
if head.distance(food)<15:
segments=segments+1
def Segmente():
if len(segments)>0:
x = head.xcor()
y = head.ycor()
if head.direction == 'up':
new_segment.goto(y-20,x)
if head.direction == 'down':
new_segment.goto(y+20,x)
if head.direction == 'right':
new_segment.goto(x-20,y)
if head.direction == 'left':
new_segment.goto(x+20,y)
# score
score = 0
high_score = 0
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Score: 0 High Score: {}".format(high_score), align="center", font=("Courier", 24, "normal"))
# Increase the score
score = score+10
if score > high_score:
high_score = score
# reset score
score = 0
# update score
pen.clear()
pen.write("score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))
while True:
win.update()
move()
Segmente()
time.sleep(0.1)
time.sleep(5)
Mfg
Christian