【问题标题】:l want to add function in python turtle graphics我想在python海龟图形中添加功能
【发布时间】:2020-04-24 01:44:56
【问题描述】:

我的代码让用户输入海龟数量。然后,它将显示随机海龟形状和颜色的输入量。我想在我的海龟图形代码中添加 3 个函数。

  1. "请输入海龟数量:"
  2. 通过接收海龟数量作为参数来初始化海龟数组的函数
  3. 最后,函数绘制海龟

请帮帮我, 这是我的代码:

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()

【问题讨论】:

  • 你有什么问题?

标签: function turtle-graphics


【解决方案1】:

这是使用global 变量的代码。您应该考虑在此函数中从全局变量切换,阅读 global 变量会很有趣且很重要。

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)

【讨论】:

    猜你喜欢
    • 2014-03-18
    • 2021-02-23
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 2020-05-11
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    相关资源
    最近更新 更多