Dec-14-2020, 11:07 PM
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()
