[Python] Tutles and stuff: how do i make a border for turtles?

Discussão em 'Python' iniciado por Stack, Abril 27, 2025.

  1. Stack

    Stack Membro Participativo

    I have a turtle named t and a turtle named to, turtle named t is already resolved as if turtle t touches the border (which was drawn by turtle named d, it will print game over and stuff, but i also need for turtle to, which is a turtle that moves randomly, to always move inside the box drawn by turtle d

    i have this code:

    from time import sleep
    import turtle
    import random

    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=[]
    if peepee==5:
    print("ur now on the final level")
    else:
    print(f"u r playing level {peepee}.")

    t.goto(40,-40)


    to.goto(40,40)
    t.setheading(180)
    to.setheading(180)
    to.speed(peepee)

    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())

    def skadaddle():
    if to.distance(t) < 120:
    to.setheading(to.towards(t) + 180)
    to.forward(30)


    def drunkt():
    turn=random.choice([90,-90])
    to.left(turn)
    go=random.choice([-20, 20])
    def yoyo():
    to.forward(go)
    xrandomlist.append(to.xcor())
    yrandomlist.append(to.ycor())
    yoyo()
    skadaddle()

    s.onkeypress(left, "Left")
    s.onkeypress(right, "Right")
    s.onkeypress(goup, "Up")
    s.onkeypress(godown, "Down")
    s.listen()

    while True:
    drunkt()
    skadaddle()
    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)<50:
    print(' ')
    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()

    Continue reading...

Compartilhe esta Página