Python Turtle - `write()` to top of screen, not center

Viewed 23
from turtle import Turtle

class Score(Turtle):
    def __init__(self):
        super().__init__()
        self.hideturtle()
        self.penup()
        self.write(arg="score: ", move=True, align="center", font=("arial", 20, "normal"))
1 Answers

The turtle object default position is 0,0 which is the center of the screen.

If you would like to write text in the top middle of the screen, move the object to that position before you write. (lower the object a little bit so the text will be visible)

import turtle

turtle.setup(1600, 1200)
wn = turtle.Screen()

txt = turtle.Turtle()
txt.color("blue")
txt.penup()

top_height = wn.window_height()/2  # positive height/2 is the top of the screen
y = top_height - top_height/10  # decreasing a little bit so text will be visible
txt.setposition(0, y)
txt.write(arg='score:', move=True, align='center', font=("arial", 20, "normal"))

enter image description here

Related