【问题标题】:How to detect the edge of a window in python如何在python中检测窗口的边缘
【发布时间】:2022-06-10 23:59:19
【问题描述】:

我正在尝试使用 python turtle 和 制作一个四处移动的蠕虫 AI,但我不知道如何检测海龟窗口的边缘,因为它确实会避开它。请帮忙

【问题讨论】:

  • 欢迎来到 SO!请分享更多关于你迄今为止尝试过的信息。
  • 左边框:warm_x < -width,右边框warm_x > +width等?你只需要知道width - width = turtle.window_width()/2

标签: python python-turtle


【解决方案1】:

你可以得到窗口的宽度

WIDTH = screen.window_width()

然后海龟可能在-WIDTH/2 ... WIDTH/2范围内有位置x,你可以这样做

    if x > WIDTH/2:
        t.setx(-WIDTH/2)

import turtle

screen = turtle.Screen()
WIDTH = screen.window_width()

t = turtle.Turtle()
t.penup()
t.speed(0)

while True:
    t.forward(5)
    x, y = t.pos()
    if x > WIDTH/2:
        t.setx(-WIDTH/2)
    screen.update()

【讨论】:

    猜你喜欢
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 2017-11-07
    相关资源
    最近更新 更多