python和pygame中的随机非重叠圆(控制圆数)

2023-12-24

我正在编写具有不同半径的非重叠随机圆的代码。我得到了应得的,但我检查重叠或非重叠的“if”语句排除了许多圆。所以,我得到的圈子数量较少。这是代码:

import pygame
import numpy as np

pygame.init()
display_width = 800
display_height = 500

black = [0, 0, 0]
white = [255, 255, 255]
red = [255, 0, 0]

display_surface = pygame.display.set_mode((display_width, display_height))
clock = pygame.time.Clock()
pygame.display.set_caption("Random Circle")


def circle(x, y, r):
    pygame.draw.circle(display_surface, red, (int(x), int(y)), int(r), 2)


def distance(x1, y1, x2, y2):
    dsq = (x1 - x2) ** 2 + (y1 - y2) ** 2
    d = np.sqrt(dsq)
    return d


n = 100

r = np.random.randint(10, 20, size=n)
x = np.random.randint(r, display_width - r, size=n)
y = np.random.randint(r, display_height - r, size=n)

display_surface.fill(black)

for i in range(len(r)):
    valid = True
    for j in range(len(r)):
        if i != j:
            d = distance(x[i], y[i], x[j], y[j])
            if d < r[i] + r[j]:
                valid = False

    if valid:

        circle(x[i], y[i], r[i])

    pygame.display.update()
    clock.tick(100)

exit = False
while not exit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

我看到了一个问题的答案here https://stackoverflow.com/questions/46702987/python-pygame-randomly-draw-non-overlapping-circles它的作用正是这段代码的作用(除了它使用了我没有使用的一类圆圈)。 我感觉我必须使用某种 while 循环,直到我得到所需的圆数才结束。但我发现实际编写代码很困难。有人可以帮忙吗?


创建圆列表 每个圆都是一个包含 3 个分量的元组:x 和 y 坐标以及半径。只要列表的长度小于,就追加新的圆圈n:

circle_list = []
while len(circle_list) < n:
    # [...]

创建随机位置和半径:

r = random.randint(10, 20)
x = random.randint(r, display_width - r)
y = random.randint(r, display_height - r)

评估该圆是否与列表中的另一个圆相交:

collide = False
for x2, y2, r2 in circle_list:
    d = distance(x, y, x2, y2)
    if d < r + r2:
        collide = True
        break

如果不发生碰撞,则附加圆:

if not collide:
    circle_list.append((x, y, r))

最小的例子:

import random
import math
import pygame

def euclidean_distance(x1, y1, x2, y2):
    #return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
    return math.hypot((x1 - x2), (y1 - y2))

pygame.init()
window = pygame.display.set_mode((400, 400))
font50 = pygame.font.SysFont(None, 50)
clock = pygame.time.Clock()

pygame.time.delay(10000)

run = True
circle_list = []
while run:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    r = random.randint(10, 20)
    x = random.randint(10+r, window.get_width()- r-10)
    y = random.randint(10+r, window.get_height()-r-10)
    if not any((x2, y2, r2) for x2, y2, r2 in circle_list if euclidean_distance(x, y, x2, y2) < r + r2):
        circle_list.append((x, y, r))    

    window.fill((255, 255, 255))
    for x, y, r in circle_list:
        pygame.draw.circle(window, (0, 0, 0), (round(x), round(y)), int(r), 3)
    window.blit(font50.render(str(len(circle_list)), True, (255, 0, 0)), (10, 10))
    pygame.display.flip()
 
pygame.quit()
exit()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python和pygame中的随机非重叠圆(控制圆数) 的相关文章

随机推荐