我对python非常陌生,似乎缺少了一些东西.
我想在pygame显示器上随机绘制圆圈,但前提是圆圈彼此不重叠.
我相信我必须找到所有圆心之间的距离,并且只有在该距离大于圆半径* 2时才绘制它.
我尝试了许多不同的方法,但是都没有成功,但我总是得到相同的结果-重叠绘制的圆圈.
#!/usr/bin/env python
import pygame, random, math
red = (255, 0, 0)
width = 800
height = 600
circle_num = 10
tick = 2
speed = 5
pygame.init()
screen = pygame.display.set_mode((width, height))
class circle():
def __init__(self):
self.x = random.randint(0,width)
self.y = random.randint(0,height)
self.r = 100
def new(self):
pygame.draw.circle(screen, red, (self.x,self.y), self.r, tick)
c = []
for i in range(circle_num):
c.append('c'+str(i))
c[i] = circle()
for j in range(len(c)):
dist = int(math.hypot(c[i].x - c[j].x, c[i].y