有趣的python代码
时间: 2023-07-10 19:18:57 浏览: 94
以下是一个有趣的 Python 代码,可以生成一个画廊风格的绘画:
import turtle
import random
# set up turtle
t = turtle.Turtle()
t.speed(0)
# set up window
turtle.setup(800, 600)
turtle.bgcolor("white")
# define colors
colors = ["red", "orange", "yellow", "green", "blue", "purple"]
# draw gallery
for i in range(15):
# draw frame
t.penup()
t.goto(random.randint(-350, 350), random.randint(-250, 250))
t.pendown()
t.color("black")
t.begin_fill()
for j in range(4):
t.forward(100)
t.right(90)
t.end_fill()
# draw painting
t.penup()
t.goto(random.randint(-300, 300), random.randint(-200, 200))
t.pendown()
t.color(random.choice(colors))
t.begin_fill()
for j in range(5):
t.forward(50)
t.right(144)
t.end_fill()
# hide turtle
t.hideturtle()
# keep window open
turtle.done()
这个代码使用了 turtle 库,可以生成一个画廊风格的绘画。运行代码后,你会发现输出了一个随机的画廊。每个画廊都包括一个黑色的框架和一个随机颜色的五边形绘画。你可以修改代码中的颜色和形状来创建不同的画廊效果。
阅读全文
相关推荐














