Python Forum

Full Version: Is there a way to move text created by turtle.write()?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm working on a school project right now, so I'm limited with the modules I can use (turtle, random, time). The basic concept is a typing game, modeled after an old game called Typer Shark. I've created turtles for each individual letter and had the corresponding turtle write the letter on each shark, but I can't get the words to move. Obviously, moving the turtle doesn't work, but it does with the shark because the turtle shape is set as the shark. Is there a way to move the text without having to create different gifs for each letter? Could I possibly make the text a turtle shape? Thanks.

# Typer Shark
import turtle as trtl
import random as rand
import time

# setup:
shark_image = 'shark.gif' # store the file name of your shape
sharks = []
letters = []
words = ['cat', 'car', 'dog', 'top', 'red']
divided_words = []
grabbed = []
wn = trtl.Screen()
wn.tracer(False)
wn.setup(width=600, height=400)
wn.addshape(shark_image) # make the screen aware of the new file
num_sharks = len(words)
for i in range(0, num_sharks):
  shark = trtl.Turtle()
  sharks.append(shark)
string = ''
for word in words:
  string += word
for char in string:
  divided_words.append(char)
  writer = trtl.Turtle()
  letters.append(writer)

# functions:
def create_shark(active_shark):
  for active_shark in sharks:
    xcor = rand.randint(0, 150)
    ycor = rand.randint(-50, 150)
    active_shark.penup()
    active_shark.goto(xcor, ycor)
    active_shark.shape(shark_image)
    wn.update()

def write_words():
  i = 0
  ii = -1
  spacing = 0                     
  while 0 < len(divided_words):
    if i % 3 == 0:
      ii += 1
      spacing = 0
    letters[i].color('blueviolet')
    letters[i].hideturtle()
    letters[i].penup()
    letters[i].goto(sharks[ii].xcor() + spacing - 30, sharks[ii].ycor() - 20)
    letters[i].pendown()
    letters[i].write(divided_words[0], align='center', font=('Arial', 20, 'bold'))  
    grabbed.append(divided_words[0])
    divided_words.pop(0)
    spacing += 15
    i += 1

def swim(): # this was just to test
  letters[0].backward(10)
  letters[1].backward(10)
  letters[2].backward(10)
  sharks[0].backward(10)
  wn.update()
  
# function calls and events:
wn.bgpic('background.gif')
create_shark(shark)
write_words()
wn.onkeypress(swim, "a")
wn.listen()
wn.mainloop()
Can you use Turtle to write text?

Perhaps writing a text character to the turtle's images when you are creating them.
Not sure I understand you completely, but can't you move the pen before you write the text? This will move the text.
(Dec-15-2020, 01:34 AM)michael1789 Wrote: [ -> ]Can you use Turtle to write text?

Perhaps writing a text character to the turtle's images when you are creating them.
How would I attach the letter's to the turtle's images?
(Dec-15-2020, 02:04 AM)MK_CodingSpace Wrote: [ -> ]Not sure I understand you completely, but can't you move the pen before you write the text? This will move the text.

I understand this, but I want the text to move with the sharks in my game, so I'm not sure how to make the text move. I think I might have to move the pens and constantly clear and rewrite the text to make it function correctly.
Yes I think your choices in turtle are to use the pens as your images (maybe one turtle for each letter?), or to draw, wait, erase, redraw over and over.

If you went down to tkinter, you could draw objects directly and then you'd be able to move them around on the canvas. A bit more work than just using turtle, but you would have extra functionality.
(Dec-15-2020, 04:59 PM)bowlofred Wrote: [ -> ]Yes I think your choices in turtle are to use the pens as your images (maybe one turtle for each letter?), or to draw, wait, erase, redraw over and over.

If you went down to tkinter, you could draw objects directly and then you'd be able to move them around on the canvas. A bit more work than just using turtle, but you would have extra functionality.

I'll try the redraw method first and I'll use separate letter images if that doesn't work. Just wanted to make sure I wasn't doing a harder method if there was something easier available. I'll look into tkinter, but I can't use it anyway because of the restrictions. Thank you.
Simply move turtle to write text in different place
t.penup()
if height < 0:
t.fd(-15)
t.write(" " + str(height))
if height < 0:
t.fd(15)
t.pendown()