如何在pygame中随机位置绘制一定数量的圆形?

3
我正在尝试在屏幕上随机位置画x个不相交的圆。 我的期望结果:在屏幕上绘制x个圆到随机位置,如我在paint中绘制的美丽图表所示: Like this beautifl paint pciture

实际结果:每次程序循环时,在新的随机位置只画了一个圆。

我尝试过的代码:

import pygame
import random

def draw_food(screen,color,x,y,radius):
    '''This function draws/creates the food sprite, The screen variable tells the food what screen to be drawn to. the color variable sets the color the radius variable sets how large of a circle the food will be.'''
    pygame.draw.circle(screen,green,(x,y),radius)

pygame.init()
width = 800
height = 600
screen = pygame.display.set_mode((width,height))
white = (255, 255, 255)
green = (0, 255, 0)
running = True

# main program loop
while running:
    # Did the user click the window close button?
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Fill the background with white
    screen.fill((white))

    # Draw the food
    draw_food(screen,green,random.randint(0,width),random.randint(0,height),20)

    # Flip the display
    pygame.display.flip()

我查看了其他类似的问题,但我能找到的所有问题都是针对不同的编程语言或者与本题无关。同时我也是 Python 和 Pygame 的初学者。


1
嘿,这是你的实际代码还是类似于你的代码,因为有多个缺少缩进的情况(我认为)。 - ZERO
你希望圆形没有任何交集吗? - Cedric Zoppolo
哎呀,它没有正确格式化,糟糕。 - Nova 4026
是的,我期望它们没有交集。 - Nova 4026
@Nova4026,那你应该在你的问题中添加那个约束条件 ;) - Cedric Zoppolo
1个回答

3
它只绘制了一个圆,因为这正是您的代码要求的。要在随机位置绘制多个圆,您可以在循环之外生成并存储随机位置值,以便每帧不会改变。像这样:
sizes = []
noOfCircles = 20
for i in range(noOfCircles):
    size.append([random.randint(0, screenWidth), random.randint(0, screenHeight))

在主循环中,您可以使用for循环来绘制圆形。请注意,在您的代码中,它只会绘制一个圆形,因为您没有使用任何类型的循环。
for i in range(20):
    pygame.draw.circle(screen,green,(sizes[i][0], sizes[i][1]),radius)

这种方法虽然可行,但不是最好的方法。通常使用类来完成。像这样为圆形创建一个类。
class Circle:
    def __init__(self):
         #You can initialise the position to a random value
         self.pos = [random.randint(0, screenWidth), random.randint(0, screenHeight)]
         self.color = green
         self.radius = 10

    def draw(self):
         pygame.draw.circle(screen, self.color, (self.size[0], self.size[1]), self.radius)

你可以创建一个包含许多圆的列表。
circles = []
for i in range(20):
    circles.append(Circle())

在你的主循环中,你可以绘制这些圆形。

while True: for circle in circles: circle.draw()

下面是完整的示例代码。

import pygame, random, math

win = pygame.display
display = win.set_mode((1200, 600))

class Circle:
    def __init__(self):
         #You can initialise the size to a random value
         self.pos = [random.randint(0, 1200), random.randint(0, 600)]
         self.color = (0,255, 0)
         self.radius = 10

    def draw(self):
         pygame.draw.circle(display, self.color, (self.pos[0], self.pos[1]), self.radius)


circles = []

for i in range(20):
    circles.append(Circle())

def checkIntersection(c1, c2):
    dx = c1.pos[0] - c2.pos[0]
    dy = c1.pos[1] - c2.pos[1]
    d = math.hypot(dx, dy)
    if d < c1.radius + c2.radius:
        return True
    return False


for i in range(19):
    while checkIntersection(circles[i], circles[i + 1]):
        circles[i].pos = random.randint(0, 1200), random.randint(0, 600)

while True:
    display.fill((255, 255, 255))
    pygame.event.get()
    for circle in circles:
        circle.draw()
    win.flip()

编辑:因此,要检查交集,您只需查看任何两个圆是否相撞,如果相撞,只需生成一组新的随机数。这可以确保它们永远不会相交。我更新了示例代码并实现了此功能。


这个答案的方法是有效的,因为它有许多圆,但OP在评论中指出这些圆不应该相交。 - Cedric Zoppolo
1
更新了答案,使得圆形永远不会相交。 - user12291970
谢谢,这对我帮助很大,虽然我不完全理解它的工作原理。 - Nova 4026

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接