Python 我能';我不明白为什么我的圆被放在对角线上
我目前正在编写一段python turtle代码,该代码要求我在正方形中随机放置圆圈。由于某些原因,它们总是放在一条对角线上,不会随机绘制Python 我能';我不明白为什么我的圆被放在对角线上,python,python-turtle,Python,Python Turtle,我目前正在编写一段python turtle代码,该代码要求我在正方形中随机放置圆圈。由于某些原因,它们总是放在一条对角线上,不会随机绘制 import random import turtle for i in range(15): # draws 15 circles and then stops random_position = random.randint(-80,90) # allows the computer to pick any numbe
import random
import turtle
for i in range(15):
# draws 15 circles and then stops
random_position = random.randint(-80,90)
# allows the computer to pick any number between -80 and 90 as an x or y co-ordinate
pit_x = random_position
pit_y = random_position
pit_radius = 7
# radius of circle
turtle.penup()
turtle.setposition(pit_x, pit_y-pit_radius)
turtle.pendown()
turtle.begin_fill()
turtle.circle(pit_radius)
turtle.end_fill()
turtle.hideturtle()
# the circle is now a black hole
它是这样出现的:
如何修复此问题?
pit_x
和pit_x
是相等的。改为这样做:
pit_x=random.randint(-80,90)
pit_y=random.randint(-80,90)
您只选择一个随机数,然后将其用于两个坐标。x=y是一条对角线
random_position
是随机生成的,但它本身不是生成器,只是一个数字
我还建议将绘图代码置于其自身的功能中,以便:
随机导入
进口海龟
def黑洞(坑x、坑y、坑半径):
乌龟
海龟。设置位置(坑深x,坑深y-坑深半径)
乌龟
乌龟,开始填充
海龟圆(坑_半径)
乌龟
乌龟
对于范围(15)内的i:
黑洞(random.randint(-80,90),random.randint(-80,90),7)
可能使用
pit_x=random.randint(-80,90)
pit_y=random.randint(-80,90)
或者如果你想要一个函数(我想你想要为每个函数做随机设置)
只是
def randomPitNumber():
返回random.randint(-80,90)
然后
pit_x=randomPitNumber()
坑_y=随机坑编号()
并删除随机位置
变量
提示:要帮助更正某些人回答的答案,请按旁边的复选标记,以便为其他人将其标记为正确答案。您使用的x和y坐标值相同。怪不得这些圆圈都在对角线上。啊,真管用!非常感谢:)谢谢!现在开始工作了:)啊,好的,谢谢!