Seite 1 von 1

Snake spiel programmieren

Verfasst: Montag 5. Oktober 2020, 16:47
von rennmaus
Hallo,
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)
(Tut mir leid, wenn es das falsche unterforum ist)

Mfg
Christian

Re: Snake spiel programmieren

Verfasst: Montag 5. Oktober 2020, 18:57
von ElektroBerry
Dein Funktionsaufruf new_segment.goto() möchte immer, zuerst die X Position und dann die Y Position.
call: goto(x, y) # two coordinates --or: goto((x, y)) # a pair (tuple) of coordinates --or: goto(vec) # e.g. as returned by pos()
Move turtle to an absolute position. If the pen is down, a line will be drawn. The turtle's orientation does not change.
Von deinem Loop:

Code: Alles auswählen

while True:
    win.update()
    move()
    Segmente(segments)
    time.sleep(0.1)
Wird die Kollisionsabfrage nicht abgerufen.

Code: Alles auswählen

if head.distance(food)<15:
Wird nur zwei mal abgefragt am Anfang.
Hat man dies korrigiert stolpert man über einen anderen Fehler.

Code: Alles auswählen

if head.distance(food)<15:
    segments=segments+1
Hier kannst du dir eine andere Lösung überlegen. Jetzt wird versucht der Liste "segments" die Zahl 1 hinzuzufügen.

Re: Snake spiel programmieren

Verfasst: Montag 5. Oktober 2020, 19:21
von rennmaus
OK danke für deine Unterstützung!

Re: Snake spiel programmieren

Verfasst: Montag 5. Oktober 2020, 19:43
von rennmaus

Code: Alles auswählen

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

new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.color("grey")
new_segment.shape("square")
new_segment.penup()


# move the end segment in reverse order

def Segmente():
    segments= [0]
    if head.distance(food)<15:
    if len(segments)>0:
        x = head.xcor()
        y = head.ycor()
        if head.direction == 'up':
            new_segment.goto(x,y-20)
        if head.direction == 'down':
            new_segment.goto(x,y+20)
        if head.direction == 'right':
            new_segment.goto(x-20,y)
        if head.direction == 'left':
            new_segment.goto(x+20,y)
Ich schaffe es nicht :( Wenn ich eine Liste mache, weiß ich nicht, wie ich es machen kann, dass die Zahl darin vergrößert wird... Wenn ich eine Variable mache, kann ich kein len() entnehmen... Nur ein kleiner Tipp... Bitte...

Re: Snake spiel programmieren

Verfasst: Montag 5. Oktober 2020, 20:25
von ElektroBerry
Jetzt so rein von der Logik:
Du hast einen Schlangenkopf den du steuerst. Die Postion vom Schlangenkopf ist klar. Aber die Position der Segmente/Körperteile, von der Schlange müssen doch gespeichert werden. Eine einfache Längenangabe der Schlange ist nicht ausreichend. Ansonsten kannst du dir ja die Musterlösung genauer ansehen.
Now we need a functionality that increases the snake body every time it touches food. We use arrays for this purpose. We create an array called segments, which is initialized to empty.
We need to add a segment to the snake’s body every time it touches the food.
Edit: In der Musterlösung wird nur die Anzeige der Segmente in der Liste gespeichert. Die Position der Segments ist immer vom letzten Segment und vom Kopf abhängig.

Re: Snake spiel programmieren

Verfasst: Mittwoch 7. Oktober 2020, 14:14
von rennmaus
Die "Musterlösung" funktioniert bei mir nicht, wegen dem "&amp;gt;0:"

Re: Snake spiel programmieren

Verfasst: Mittwoch 7. Oktober 2020, 15:23
von __blackjack__
@rennmaus: Das ist aber ein einfach zu behebender Fehler, selbst wenn man kein HTML kann.

Re: Snake spiel programmieren

Verfasst: Mittwoch 7. Oktober 2020, 16:45
von rennmaus
Oh... Naja, ich weiß nunmal nicht, wozu "&amp;gt;0" man das braucht, also weiß ich leider auch nicht wie man das behebt :(

Re: Snake spiel programmieren

Verfasst: Mittwoch 7. Oktober 2020, 16:56
von rennmaus
Habe gerade nachgeschaut : Was macht das "and" wenn darauf kein weiteres Argument folgt?

Re: Snake spiel programmieren

Verfasst: Mittwoch 7. Oktober 2020, 17:06
von rennmaus
Passt