![]() |
Is there a way to move text created by turtle.write()? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Is there a way to move text created by turtle.write()? (/thread-31488.html) |
Is there a way to move text created by turtle.write()? - derekered - Dec-14-2020 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() RE: Is there a way to move text created by turtle.write()? - michael1789 - Dec-15-2020 Can you use Turtle to write text? Perhaps writing a text character to the turtle's images when you are creating them. RE: Is there a way to move text created by turtle.write()? - MK_CodingSpace - Dec-15-2020 Not sure I understand you completely, but can't you move the pen before you write the text? This will move the text. RE: Is there a way to move text created by turtle.write()? - derekered - Dec-15-2020 (Dec-15-2020, 01:34 AM)michael1789 Wrote: Can you use Turtle to write text?How would I attach the letter's to the turtle's images? RE: Is there a way to move text created by turtle.write()? - derekered - Dec-15-2020 (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. RE: Is there a way to move text created by turtle.write()? - bowlofred - Dec-15-2020 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. RE: Is there a way to move text created by turtle.write()? - derekered - Dec-15-2020 (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. 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. RE: Is there a way to move text created by turtle.write()? - itexamples - Dec-16-2020 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() |