首页 > 解决方案 > 如何在pygame的屏幕上随机位置绘制一定数量的圆圈?

问题描述

我正在尝试在屏幕上的随机位置绘制 x 个圆圈,这些圆圈没有交叉点。 我的预期结果: x 数量的圆圈被绘制到屏幕上的随机位置。就像我用油漆画的这张漂亮的图表一样 喜欢这个美丽的油漆图片

实际结果:每次程序循环时,我都会在一个新的随机位置绘制一个圆圈。

到目前为止我尝试过的代码:

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 的初学者。

标签: pythonpygame

解决方案


它只画一个圆圈,因为这正是您的代码所说的。要在随机位置有多个圆圈,您可以在循环之外生成和存储随机位置值,以便它每帧都发生变化。像这样的东西。

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:对于圆圈中的圆圈: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()

编辑:因此,要检查交叉点,您可以查看是否有任何圆圈发生碰撞,以及它们是否只是生成一组新的随机数。这确保他们永远不会相交。我用这个实现更新了示例代码。


推荐阅读