自撞后让蛇(海龟列表)重新启动?

时间:2019-11-02 22:20:05

标签: python collision-detection turtle-graphics

一旦乌龟与自己的身体发生碰撞,我如何让它在屏幕中间重新启动?

from turtle import Screen, Turtle
import random
import time

DELAY = 100  

def move():
    if direction == 'up':
        y = turtle.ycor()
        turtle.sety(y + 20)
    elif direction == 'down':
        y = turtle.ycor()
        turtle.sety(y - 20)
    elif direction == 'left':
        x = turtle.xcor()
        turtle.setx(x - 20)
    elif direction == 'right':
        x = turtle.xcor()
        turtle.setx(x + 20)

    screen.update()
    screen.ontimer(move, DELAY)

    if turtle.xcor()>490 or turtle.xcor()<-490 or turtle.ycor()>490 or turtle.ycor()<-490:
        time.sleep(1)
        turtle.goto(0,0)
        turtle.direction = "stop"


        for section in sections
            section.goto(1000, 1000)


        sections.clear()

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


        new_section = Turtle()
        new_section.speed(0)
        new_section.shape("square")
        new_section.color("orange", "grey")
        new_section.penup()
        sections.append(new_section)

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

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

def up():
    global direction
    direction = 'up'

def down():
    global direction
    direction = 'down'

def left():
    global direction
    direction = 'left'

def right():
    global direction
    direction = 'right'


screen = Screen()
screen.title(" Snakebite mini Game")
screen.bgcolor('brown')
screen.setup(width=800, height=700)
screen.tracer(0)


turtle = Turtle()
turtle.shape('turtle')
turtle.speed(0)
turtle.penup()


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

sections = []

direction = 'stop'

screen.onkey(up, 'Up')
screen.onkey(down, 'Down')
screen.onkey(left, 'Left')
screen.onkey(right, 'Right')
screen.listen()

# main game loop
move()






screen.mainloop()

1 个答案:

答案 0 :(得分:0)

基本答案如下:

if any(turtle.distance(section) < 20 for section in sections):
    turtle.goto(0, 0)
    direction = 'stop'
    ...

但是要警告,这意味着180度现在是致命的动作!

通过优化,样式调整和更改来完善代码,使其更具可玩性:

from turtle import Screen, Turtle
from random import randint

DELAY = 100

def up():
    global direction
    direction = 'up'
    turtle.setheading(90)

def down():
    global direction
    direction = 'down'
    turtle.setheading(270)

def left():
    global direction
    direction = 'left'
    turtle.setheading(180)

def right():
    global direction
    direction = 'right'
    turtle.setheading(0)

def move():
    global direction

    x, y = old_position = turtle.position()

    if direction == 'up':
        turtle.sety(y + 20)
    elif direction == 'down':
        turtle.sety(y - 20)
    elif direction == 'left':
        turtle.setx(x - 20)
    elif direction == 'right':
        turtle.setx(x + 20)

    if direction != 'stop':

        if sections:
            if any(turtle.distance(section) < 20 for section in sections):
                turtle.goto(0, 0)
                direction = 'stop'

                for section in sections:
                    section.hideturtle()

                sections.clear()
            else:
                last_position = sections[-1].position()

                if len(sections) > 1:
                    for index in range(len(sections) - 1, 0, -1):
                        sections[index].goto(sections[index - 1].position())

                sections[0].goto(old_position)

                old_position = last_position

        if not (-390 < turtle.xcor() < 390 and -390 < turtle.ycor() < 390):
            turtle.goto(0, 0)
            direction = 'stop'

            for section in sections:
                section.hideturtle()

            sections.clear()

        if turtle.distance(food) < 20:
            food.goto(randint(-380, 380), randint(-380, 380))

            new_section = Turtle()
            new_section.shape('square')
            new_section.speed('fastest')
            new_section.color('orange', 'grey')
            new_section.penup()
            new_section.goto(old_position)
            sections.append(new_section)

    screen.update()
    screen.ontimer(move, DELAY)

screen = Screen()
screen.title("Snakebite mini Game")
screen.bgcolor('brown')
screen.setup(width=800, height=800)
screen.tracer(False)

turtle = Turtle()
turtle.shape('triangle')
turtle.speed('fastest')
turtle.color('orange', 'grey')
turtle.penup()

food = Turtle()
food.shape('circle')
food.speed('fastest')
food.color('red', 'yellow')
food.penup()
food.goto(randint(-380, 380), randint(-380, 380))

sections = []

direction = 'stop'

screen.onkey(up, 'Up')
screen.onkey(down, 'Down')
screen.onkey(left, 'Left')
screen.onkey(right, 'Right')
screen.listen()

# main game loop
move()

screen.mainloop()