Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

Something wrong with python sequence

#imports
import turtle
import time
import random

delay = 0.1

#design
window = turtle.Screen()
window.title("Snake Game by LZ")
window.bgcolor("#cce0ff")
window.setup(width=700, height=750)
window.tracer(0)

head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0,0)
head.direction = "stop"

food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.goto(0,100)

segments = []

#functions
def go_up():
    head.direction = "up"
    
def go_down():
    head.direction = "down"
    
def go_left():
    head.direction = "left"
    
def go_right():
    head.direction = "right"
    
def motion():
    if head.direction == "up":
        y = head.ycor()
        head.sety(y + 13)

    if head.direction == "down":
        y = head.ycor()
        head.sety(y - 13)
        
    if head.direction == "left":
        x = head.xcor()
        head.setx(x - 13)
        
    if head.direction == "right":
        x = head.xcor()
        head.setx(x + 13)

#keyboard bindings
window.listen()
window.onkeypress(go_up, "Up")
window.onkeypress(go_down, "Down")
window.onkeypress(go_left, "Left")
window.onkeypress(go_right, "Right")

#main game loop
while True:
    window.update()

    #check for collision
    if head.xcor() > 300 or head.xcor() < -300 or head.ycor() > 300 or head.ycor() < -300:
        time.sleep(1)
        head.goto(0,0)
        head.direction = "stop"

        for segment in segments:
            segment.goto(1000, 1000)

        segments.clear()

    if head.distance(food) < 20:
        x = random.randint(-300, 300)
        y = random.randint(-300, 300)
        food.goto(x,y)

        #add segment
        new_segment = turtle.Turtle()
        new_segment.speed(0)
        new_segment.shape("circle")
        new_segment.color("grey")
        new_segment.penup()
        segments.append(new_segment)

    for index in range(len(segments)-1, 0, -1):
        x = segments[index-1].xcor()
        y = segments[index-1].ycor()
        segments[index].goto(x,y)

    if len(segments) > 0:
        x = head.xcor()
        y = head.ycor()
        segments[0].goto(x,y)

    motion()

    for segment in segments:
        if segment.distance(head) < 20:
            time.sleep(1)
            head.goto(0,0)
            head.direction = "stop"
            
            for segment in segments:
                segment.goto(1000, 1000)

            segments.clear()

    time.sleep(delay)

window.mainloop()

Here's my problem: When my 'snake' hits its body or the border, it should go back to the center and all of its segments will be gone. However, when I run it; when the snake eats the 'food', it will go back to the center; in other words, the 'snake' can't eat the 'food'.

I suspect something is wrong with this part below:

for segment in segments:
        if segment.distance(head) < 20:
            time.sleep(1)
            head.goto(0,0)
            head.direction = "stop"
            
            for segment in segments:
                segment.goto(1000, 1000)

            segments.clear()

...because when I remove this (above) part, the 'snake' is able to eat the 'food' and grow. When I add this (above) in, it can't even eat the 'food'; whenever it touches the 'food' it will restart. Can anyone help me? Thanks!

question from:https://stackoverflow.com/questions/65857571/something-wrong-with-python-sequence

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...