如何让随机的乌龟从受控的乌龟身边跑开?

问题描述 投票:0回答:0

我有一只名为“t”的乌龟,这是一只由用户控制的乌龟,还有一只名为“to”的随机移动的乌龟,我需要这只名为“to”的乌龟在以下情况下试图逃离乌龟“t” peepee(顺便说一下,它只是水平)等于 1 到 4,它试图逃跑,但仍然可以捕捉,但是当它达到 peepee 等于 5 时,乌龟只是随机移动或随机转到某个地方,这几乎不可能赶上

from time import sleep
import turtle
import random

print("this is a turtle game, u r the police turtle (blue), and u need to beat up the criminal turtle (black)")

s = turtle.Screen()
s.bgcolor('white')
s.setup(500,380)

d=turtle.Turtle()
d.penup()
d.goto(-150,-150)
d.pensize(5)
d.pendown()
d.hideturtle()
for x in range(4):
  d.forward(300) 
  d.left(90) 

t = turtle.Turtle()
t.shape('turtle')
t.color('blue')
t.penup()
to = turtle.Turtle()
to.shape('turtle')
to.color('black')
to.penup()


for peepee in range(1, 6):
    xcor=[]
    ycor=[]
    xrandomlist=[]
    yrandomlist=[]

    jack=random.randint(-130,130)
    to.speed(peepee)
    t.goto(40,-40)
    to.goto(40,40)
    t.setheading(180)
    to.setheading(180)
    def left():
      t.left(90)
  
    def right():
      t.left(-90)
  
    def goup():
      t.forward(15)
      xcor.append(t.xcor())
      ycor.append(t.ycor())
    def godown():
      t.forward(-15)
      xcor.append(t.xcor())
      ycor.append(t.ycor()) 
    jacker=random.randint(-130,130)
  
    def drunkt():
      if to.xcor()<140:
        if to.xcor()> -140:
          if to.ycor()<140:
            if to.ycor()>-140:
              turn=random.choice([90,-90])
              to.left(turn)
              go=random.choice([-20, 20])
              def jiji():
                if peepee==5:
                  if to.distance(t)<60:
                    to.goto(jack, jacker)
                to.forward(go)
                xrandomlist.append(to.xcor())
                yrandomlist.append(to.ycor())
              jiji()
  
        if to.xcor()>130 or to.xcor()< -130 or to.ycor()>130 or to.ycor()<-130:
          def yoyo():
            to.goto(jacker, jacker)
            xrandomlist.append(to.xcor())
            yrandomlist.append(to.ycor())
          yoyo()
  
    s.onkeypress(left, "Left")
    s.onkeypress(right, "Right")
    s.onkeypress(goup, "Up")
    s.onkeypress(godown, "Down")
    s.listen()
    if peepee==5:
      print("ur now on the final level")
      to.speed(7)
      to.color("yellow")
    else:
      print(f"u r playing level {peepee}.")
      sleep(1)
  
  
    while True:
      if peepee==5:
        don=random.choice([1,2])
        if don==1:
          to.goto(jack, jacker)
      drunkt()
      if t.xcor()>130 or t.xcor()<-130:
        print("game over")
        s.bye()
        sleep(9999)
      if t.ycor()>130 or t.ycor()<-130:
        print("game over")
        s.bye()
        sleep(9999)
      if t.distance(to)<30:
        print("noice")
        print(' ')
        for x in range(len(xcor)):
          t.speed(1)
          t.goto(xcor[-1],ycor[-1])
          xcor.pop(-1)
          ycor.pop(-1)
    
        for x in range(len(xrandomlist)):
          to.speed(1)
          to.goto(xrandomlist[-1],yrandomlist[-1])
          xrandomlist.pop(-1)
          yrandomlist.pop(-1)
        sleep(2)
        break
  
turtle.done()
s.bye()

and i tried putting in some stuff on def drunkt(), and it doesn't matter where the turtle "to" goes, i just need that the turtle moves away from "t", but not impossible to catch it, and on level 5 i need the turtle "to" to be hard to catch, like nearly impossible
python turtle-graphics python-turtle replit
© www.soinside.com 2019 - 2024. All rights reserved.