我的目标是使用围绕圆的边缘和恒定起点(圆圈的中间)的随机端点在pygame中使用线条形成圆形.所以我决定将pygame.draw.line函数:screen,aRandomColor,startingPosition和endingPosition作为参数.结束位置是包含随机生成的x值的元组,辅助函数将根据圆的半径计算y值.我的第一个函数计算y值,如下所示:
import math
import random
def findY(pos1,pos2,distance,bothValues=False):
p1 =pos1
p2 = pos2
x1 = float(p1[0])
y1 = float(p1[1])
x2 = float(p2[0])
d = float(distance)
y2 = y1 - math.sqrt(d**2 - (x1-x2)**2)
_y2 = y1 + math.sqrt(d**2 - (x1-x2)**2)
if bothValues==True:
return y2,_y2
else:
return y2
和线条抽屉:
width = 500
height = 500
def randLine(surface,color=rand,start=rand,end=rand,length=rand):
if start==rand:
start = randPos()
if end==rand:
end = randPos()