如何随机放置几个非碰撞的rects?

Joh*_*ohn 9 python pygame collision-detection rect

我正在和Pygame一起做2D游戏.我需要在不相交的情况下随机放置几个物体.我尝试了一些明显的方法,但它们没有用.

明显的方法如下(伪):

create list of objects
for object in list:
    for other object in list:
        if object collides with other object:
            create new list of objects
Run Code Online (Sandbox Code Playgroud)

那种方法永远都是.

我试过的其他方法:

create list of objects
for object in list:
    for other object in list:
        if object collides with other object:
             remove object from list
Run Code Online (Sandbox Code Playgroud)

该方法在空列表附近返回.

我正在处理一个大小为2到20个对象的列表.有什么建议?

编辑:矩形都是随机不同的大小.

mar*_*eau 11

我已经改变了我的答案,以解决你的后续问题,即是否可以将其修改为生成随机非碰撞方块而不是任意矩形.我用尽可能最简单的方法做到了这一点,即后处理原始答案的矩形输出并将其内容转换为方形子区域.我还更新了可选的可视化代码以显示两种输出.显然,这种过滤可以扩展到做其他事情,例如稍微插入每个矩形或正方形以防止它们彼此接触.

我的答案避免做了许多已经发布的答案 - 即随机生成矩形,同时拒绝与已经创建的任何冲突 - 因为它本身有点慢,计算上浪费.相反,它只专注于生成那些首先不重叠的.

通过将其转换为可以非常快速地完成的区域细分问题,这使得需要做的事情变得相对简单.以下是如何实现这一目标的一种实现方式.它以一个定义外边界的矩形开始,它分成四个较小的非重叠矩形.这是通过选择半随机内部点并将其与外部矩形的四个现有角点一起使用来形成四个子部分来实现的.

大部分动作都发生在quadsect()函数中.内点的选择对于确定输出的外观至关重要.您可以按照自己的方式约束它,例如只选择一个会产生至少某个最小宽度或高度或不大于某个量的子矩形.在我的答案的示例代码中,它被定义为外部矩形的宽度和高度的中心点±1/3,但基本上任何内部点都可以在某种程度上起作用.

由于该算法非常快速地生成子矩形,因此可以花费一些计算时间来确定内部分割点.

为了帮助可视化这种方法的结果,最后有一些非必要的代码使用PIL(Python Imaging Library)模块创建一个图像文件,显示在我做的一些测试运行期间生成的矩形.

无论如何,这是代码和输出样本的最新版本:

import random
from random import randint
random.seed()

NUM_RECTS = 20
REGION = Rect(0, 0, 640, 480)

class Point(object):
    def __init__(self, x, y):
        self.x, self.y = x, y

    @staticmethod
    def from_point(other):
        return Point(other.x, other.y)

class Rect(object):
    def __init__(self, x1, y1, x2, y2):
        minx, maxx = (x1,x2) if x1 < x2 else (x2,x1)
        miny, maxy = (y1,y2) if y1 < y2 else (y2,y1)
        self.min, self.max = Point(minx, miny), Point(maxx, maxy)

    @staticmethod
    def from_points(p1, p2):
        return Rect(p1.x, p1.y, p2.x, p2.y)

    width  = property(lambda self: self.max.x - self.min.x)
    height = property(lambda self: self.max.y - self.min.y)

plus_or_minus = lambda v: v * [-1, 1][(randint(0, 100) % 2)]  # equal chance +/-1

def quadsect(rect, factor):
    """ Subdivide given rectangle into four non-overlapping rectangles.
        'factor' is an integer representing the proportion of the width or
        height the deviatation from the center of the rectangle allowed.
    """
    # pick a point in the interior of given rectangle
    w, h = rect.width, rect.height  # cache properties
    center = Point(rect.min.x + (w // 2), rect.min.y + (h // 2))
    delta_x = plus_or_minus(randint(0, w // factor))
    delta_y = plus_or_minus(randint(0, h // factor))
    interior = Point(center.x + delta_x, center.y + delta_y)

    # create rectangles from the interior point and the corners of the outer one
    return [Rect(interior.x, interior.y, rect.min.x, rect.min.y),
            Rect(interior.x, interior.y, rect.max.x, rect.min.y),
            Rect(interior.x, interior.y, rect.max.x, rect.max.y),
            Rect(interior.x, interior.y, rect.min.x, rect.max.y)]

def square_subregion(rect):
    """ Return a square rectangle centered within the given rectangle """
    w, h = rect.width, rect.height  # cache properties
    if w < h:
        offset = (h - w) // 2
        return Rect(rect.min.x, rect.min.y+offset,
                    rect.max.x, rect.min.y+offset+w)
    else:
        offset = (w - h) // 2
        return Rect(rect.min.x+offset, rect.min.y,
                    rect.min.x+offset+h, rect.max.y)

# call quadsect() until at least the number of rects wanted has been generated
rects = [REGION]   # seed output list
while len(rects) <= NUM_RECTS:
    rects = [subrect for rect in rects
                        for subrect in quadsect(rect, 3)]

random.shuffle(rects)  # mix them up
sample = random.sample(rects, NUM_RECTS)  # select the desired number
print '%d out of the %d rectangles selected' % (NUM_RECTS, len(rects))

#################################################
# extra credit - create an image file showing results

from PIL import Image, ImageDraw

def gray(v): return tuple(int(v*255) for _ in range(3))

BLACK, DARK_GRAY, GRAY = gray(0), gray(.25), gray(.5)
LIGHT_GRAY, WHITE = gray(.75), gray(1)
RED, GREEN, BLUE = (255, 0, 0), (0, 255, 0), (0, 0, 255)
CYAN, MAGENTA, YELLOW = (0, 255, 255), (255, 0, 255), (255, 255, 0)
BACKGR, SQUARE_COLOR, RECT_COLOR = (245, 245, 87), (255, 73, 73), (37, 182, 249)

imgx, imgy = REGION.max.x + 1, REGION.max.y + 1
image = Image.new("RGB", (imgx, imgy), BACKGR)  # create color image
draw = ImageDraw.Draw(image)

def draw_rect(rect, fill=None, outline=WHITE):
    draw.rectangle([(rect.min.x, rect.min.y), (rect.max.x, rect.max.y)],
                   fill=fill, outline=outline)

# first draw outlines of all the non-overlapping rectanges generated
for rect in rects:
    draw_rect(rect, outline=LIGHT_GRAY)

# then draw the random sample of them selected
for rect in sample:
    draw_rect(rect, fill=RECT_COLOR, outline=WHITE)

# and lastly convert those into squares and re-draw them in another color
for rect in sample:
    draw_rect(square_subregion(rect), fill=SQUARE_COLOR, outline=WHITE)

filename = 'square_quadsections.png'
image.save(filename, "PNG")
print repr(filename), 'output image saved'
Run Code Online (Sandbox Code Playgroud)

输出样本1

第一个样本输出图像

输出样本2

第二个样本输出图像