如何使用Turtle Python随机化形状和颜色?

发布于 2025-01-24 17:09:37 字数 42 浏览 0 评论 0原文

有人可以使用Turtle Python帮助我,以随机颜色绘制随机形状?

Can someone please help me with drawing random shapes in random colors using turtle python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

燃情 2025-01-31 17:09:37

首先分享您的尝试。这是一个简单的程序,从列表中选择随机颜色和形状并绘制它。您可以在列表中添加更多颜色或更多功能来绘制不同的形状。

请随时在下面的评论中要求澄清...

import random

color_list = ["red" , "green" , "blue"]
shape_list = ["square" , "circle" , "triangle"]

myTurtle = turtle.Turtle()
myTurtle.hideturtle()
myTurtle.color(random.choice(color_list))

def drawSquare():
    for i in range(4):
        myTurtle.forward(100)
        myTurtle.left(90)

def drawCircle():
    myTurtle.circle(100)

def drawTriangle():
    for i in range(3):
        myTurtle.forward(100)
        myTurtle.left(120)
        
random_shape = random.choice(shape_list)

if random_shape == "square":
    drawSquare()
elif random_shape == "circle":
    drawCircle()
elif random_shape == "triangle":
    drawTriangle()

Sharing your attempt first is appreciated. Here is a simple program that chooses a random colour and shape from a list and draws it. You can add more colours in the list or more functions to draw different shapes.

Feel free to ask for clarifications in the comments below...

import random

color_list = ["red" , "green" , "blue"]
shape_list = ["square" , "circle" , "triangle"]

myTurtle = turtle.Turtle()
myTurtle.hideturtle()
myTurtle.color(random.choice(color_list))

def drawSquare():
    for i in range(4):
        myTurtle.forward(100)
        myTurtle.left(90)

def drawCircle():
    myTurtle.circle(100)

def drawTriangle():
    for i in range(3):
        myTurtle.forward(100)
        myTurtle.left(120)
        
random_shape = random.choice(shape_list)

if random_shape == "square":
    drawSquare()
elif random_shape == "circle":
    drawCircle()
elif random_shape == "triangle":
    drawTriangle()
酒与心事 2025-01-31 17:09:37

这是一些非常简单的代码,用于随机移动和颜色:

import turtle
import random
pat = turtle.Turtle()
turtle.Screen().bgcolor('black')
colors = ["red", "yellow", "blue"]

for i in range(500):
    pat.color(random.choice(colors))
    x = random.randint(1,4)
    if x == 1:
        pat.forward(30)
    elif x == 2:
        pat.back(30)
    elif x == 3:
        pat.right(30)
    elif x == 4:
        pat.left(30)

Here is some very straight-forward code for random moves and colors:

import turtle
import random
pat = turtle.Turtle()
turtle.Screen().bgcolor('black')
colors = ["red", "yellow", "blue"]

for i in range(500):
    pat.color(random.choice(colors))
    x = random.randint(1,4)
    if x == 1:
        pat.forward(30)
    elif x == 2:
        pat.back(30)
    elif x == 3:
        pat.right(30)
    elif x == 4:
        pat.left(30)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文