Python Forum
Is there a way to move text created by turtle.write()?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a way to move text created by turtle.write()?
#1
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()
Reply
#2
Can you use Turtle to write text?

Perhaps writing a text character to the turtle's images when you are creating them.
Reply
#3
Not sure I understand you completely, but can't you move the pen before you write the text? This will move the text.
Reply
#4
(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?
Reply
#5
(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.
Reply
#6
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.
Reply
#7
(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.
Reply
#8
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  matplotlib x-axis text move bottom upward jacklee26 3 973 May-31-2023, 04:28 AM
Last Post: jacklee26
  Read text file, modify it then write back Pavel_47 5 1,573 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  Turtle.setpos() vs text position on screen query ElectronWrangler 0 1,583 Nov-26-2022, 02:39 AM
Last Post: ElectronWrangler
  How to write in text file - indented block Joni_Engr 4 6,432 Jul-18-2022, 09:09 AM
Last Post: Hathemand
  CSV to Text File and write a line in newline atomxkai 4 2,671 Feb-15-2022, 08:06 PM
Last Post: atomxkai
  List Won't Write in Text File IILawrenceII 4 2,122 Jul-17-2020, 11:16 PM
Last Post: j.crater
  Write text expander in Python constantin01 5 3,860 Jan-16-2020, 08:28 AM
Last Post: buran
  read text file and write into html with correct link jacklee26 4 2,888 Aug-02-2019, 05:48 AM
Last Post: jacklee26
  write image into string format into text file venkat18 2 4,386 Jun-01-2019, 06:46 AM
Last Post: venkat18
  write each line of a text file into separate text files and save with different names Shameendra 3 2,768 Feb-20-2019, 09:07 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020