0

My code lets the user input the turtle count. Then, it will show the amount of input with random turtle shapes and colors. I want to add 3 functions to my turtle graphics code.

  1. "Please enter the number of turtles: "
  2. Function to initialize the turtle array by receiving the number of turtles as a parameter
  3. Finally, the function draws the turtle

Please help me, here is my code:

import turtle 
import random


myturtle,tx,ty,tcolor,tsize,tshape=[None]*6
shapelist=[]
playerturtles=[]
swidth,sheight=500,500


if __name__=='__main__': 
    turtle.title('Turtle list utilization')
    turtle.setup(width=swidth+50,height=sheight+50)
    turtle.screensize(swidth,sheight)
    shapelist=turtle.getshapes()
    a=int(input('Turtle Count:'))

    for i in range(0,a):
        random.shuffle(shapelist)
        myturtle=turtle.Turtle(shapelist[0])
        tx=random.randrange(-swidth/2,swidth/2)
        ty=random.randrange(-sheight/2,sheight/2)
        r=random.random();g=random.random();b=random.random()
        tsize=random.randrange(1,3)
        playerturtles.append([myturtle,tx,ty,tsize,r,g,b])

    for i in range(0,a):
        myturtle=playerturtles[i][0]
        myturtle.color((playerturtles[i][4],playerturtles[i][5],playerturtles[i][6]))
        myturtle.pencolor((playerturtles[i][4],playerturtles[i][5],playerturtles[i][6]))
        myturtle.turtlesize(playerturtles[i][3])
        myturtle.goto(playerturtles[i][1],playerturtles[i][2])
    turtle.done()
Red
  • 26,798
  • 7
  • 36
  • 58

1 Answers1

0

Here is the code using global variables. You should consider switching from global variables in this function and it would be interesting and important to read about global variables.

import turtle
import random

myturtle, tx, ty, tcolor, tsize, tshape = [None] * 6
shapelist = []
playerturtles = []
swidth, sheight = 500, 500


def start_turtles():
    global shapelist
    turtle.title('Turtle list utilization')
    turtle.setup(width=swidth + 50, height=sheight + 50)
    turtle.screensize(swidth, sheight)
    shapelist = turtle.getshapes()
    a = int(input('Turtle Count:'))
    return a


def turtle_start(a):
    global myturtle
    global tsize
    global tx
    global ty

    for i in range(0, a):
        random.shuffle(shapelist)
        myturtle = turtle.Turtle(shapelist[0])
        tx = random.randrange(-swidth / 2, swidth / 2)
        ty = random.randrange(-sheight / 2, sheight / 2)
        r = random.random()
        g = random.random()
        b = random.random()
        tsize = random.randrange(1, 3)
        playerturtles.append([myturtle, tx, ty, tsize, r, g, b])


def turtle_draw(a):
    global myturtle
    for i in range(0, a):
        myturtle = playerturtles[i][0]
        myturtle.color((playerturtles[i][4], playerturtles[i][5], playerturtles[i][6]))
        myturtle.pencolor((playerturtles[i][4], playerturtles[i][5], playerturtles[i][6]))
        myturtle.turtlesize(playerturtles[i][3])
        myturtle.goto(playerturtles[i][1], playerturtles[i][2])
    turtle.done()


if __name__ == '__main__':
    a = start_turtles()
    turtle_start(a)
    turtle_draw(a)
snatchysquid
  • 1,283
  • 9
  • 24