在我的函数 Stars() 中,它是创建“星星”的地方。我只希望在背景为黑色时放置它们。我尝试了 100 个不同的 if 语句,但无法弄清楚,我怎样才能实现这一目标。 我需要更改 if 语句吗?或者还有其他必须添加/删除的内容吗?根据记录,建筑功能是空的,因为它绘制了“摩天大楼”的轮廓并用灰色填充。所以想象一下上半部分是黑色的,下半部分是灰色的。
import turtle
import random
def main5():
turtle.bgcolor('black')
building():
for n in range(30):
stars(0,0,5,'white')
def building():
def stars(x,y,width,color):
x = random.randint(-450,450)
y = random.randint(0,400)
if turtle.fillcolor() != 'grey':
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
turtle.fillcolor(color)
turtle.begin_fill()
for sqr in range(5):
turtle.forward(width)
turtle.left(90)
turtle.end_fill()
main5()
您应该检测黑色背景的位置而不是颜色
import turtle
import random
def main5():
turtle.bgcolor('black')
building()
for n in range(30):
stars(0,0,5,'white')
def building():
pass
def stars(x,y,width,color):
x = random.randint(-450,450)
y = random.randint(0,400) # Assume the black background is between y=0 and y=400
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
turtle.fillcolor(color)
turtle.begin_fill()
for sqr in range(5):
turtle.forward(width)
turtle.left(72) # Usually 5 points are used to draw stars, and the rotation angle of each point is 72 degrees.
turtle.end_fill()
main5()