乌龟随风移动

2024-09-28 22:12:23 发布

您现在位置:Python中文网/ 问答频道 /正文

创建了一个程序来随机化海龟的移动,但是一旦它碰到一个边界(窗口),就不能让它打破循环。尝试了一些类似问题的解决方案,但仍然没有成功。你知道吗

from turtle import Turtle, Screen
import random

def createTurtle(color, width):
    tempName = Turtle("turtle")
    tempName.speed("fastest")
    tempName.color(color)
    tempName.width(width)
    return tempName

def inScreen(screen, turt):

    min_x, max_x = -wn.window_width() / 2 , wn.window_width() / 2
    min_y, max_y = -wn.window_height() / 2 , wn.window_height() / 2

    turtleX, turtleY = turt.pos()

    while (min_x < turtleX < max_x) and (min_y < turtleY < max_y):
        turt.left(random.randrange(360))
        turt.fd(random.randrange(100))
        turtleX, turtleY = turt.pos()
        print(turtleX, ",", turtleY)


wn = Screen()

alpha = createTurtle("red", 3)

inScreen(wn, alpha)

wn.exitonclick()

Tags: importrandomminwindowwidthscreenmaxcolor