如何在画布上显示海龟坐标

新手上路,请多包涵

使用 Python 3.6 和 Turtle 模块,我想知道如何在屏幕上打印乌龟的坐标。

我的问题是: 如何在屏幕上打印文本,以及如何使该文本成为玩家的坐标?

这是我的代码。

 from turtle import Turtle, Screen

play = Screen()

play.bgcolor("black")
play.screensize(250, 250)
play.title("Turtle Keys")

def position():
    coord = follow.coor()
    coord.color("white")
    coord.setposition(130, 100)

run = Turtle("triangle")
run.speed("fastest")
run.color("white")
run.penup()
run.setposition(250, 250)

print(position)

谢谢你。

编辑 我试过 write ,但它抛出一个名称未定义的错误。

原文由 The Weird 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 700
1 个回答

以下是如何在屏幕上写入海龟位置:来自 turtle 模块的 write 方法,必须在 Turtle 上调用

import turtle

playground = turtle.Screen()       # use nouns for objects, play is a verb

playground.bgcolor("black")
playground.screensize(250, 250)
playground.title("Turtle Keys")

tom = turtle.Turtle()              # use nouns for objects, run is a verb

tom.color("white")
tom.setposition(130, 100)
tom.speed("fastest")
tom.color("white")

p = tom.pos()               # here you get the coordinates of the turtle
tom.write(str(p), True)     # and you print a string representation on screen
tom.penup()

print(p)                    # this prints to the console

playground.exitonclick()

在此处输入图像描述

原文由 Reblochon Masque 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题