生成不重叠的“分散”项目

问题描述 投票:1回答:1

这是我必须解决的问题,但过去5天来我一直在撞墙。我必须想起一些真正简单的事情,因为逻辑似乎在我的脑海中是100%正确的,但那根本行不通。

我需要画一些不分散的“分散”房屋,其中有100座。我必须用乌龟画它们,所以我有X和Y坐标。X和Y列出以进行验证,以查看房子是否已在该位置。

[我正在做的是在随机坐标上绘制一个“房子”(正方形的正方形,顶部有一个三角形,当房屋数量少于100时,我循环播放。在每个循环中,我将x和y坐标随机化,从那里开始用乌龟绘制每个房屋。我检查该值是否已经在X和Y验证列表中,以及我的新X和Y是否为房屋大小的+/-(将其作为正方形处理)

import turtle
import time
import random
t = turtle.Turtle()
turtle.screensize(1920,1000)
x_verif = []
y_verif = []
t.speed(0)
collision = None


def square():
    for s in range(0, 4):
        t.forward(90)
        t.left(90)

def triangle():
    for s in range(0, 2):
        t.left(-60)
        t.forward(52)

def house():
    square()
    t.left(90)
    t.forward(90)
    triangle()

def scatter():
    print("a")
    house()
    x1 = random.randint(-850, 850)
    y1 = random.randint(-380, 380)
    count = 0
    while count < 100:
        print("a2")
        i = 0
        u = 0
        collision = False
        for  tries in range (0, 2):
            print("a3")
            x_verif.append(x1)
            y_verif.append(y1)
        while u < 100:
            print("a4")

            print(x_verif, y_verif, x1, y1)
            while i < len(x_verif):
                x1 = random.randint(-850, 850)
                y1 = random.randint(-380, 380)
                print(x1, y1)
                if x1 not in x_verif and (x1 > x_verif[i]+91 or x1 < x_verif[i]-91):
                    if y1 not in y_verif and (y1 > y_verif[i]+142 or y1 < y_verif[i]-142):
                        collision = False
                else:
                    collision = True
            if collision == False:
                            t.penup()
                            t.hideturtle()
                            t.setx(x1)
                            t.sety(y1)
                            t.pendown()
                            house()
                            x_verif.append(x1)
                            y_verif.append(y1)
                            count += 1
            count += 1
        u += 1


scatter()

很抱歉难看的代码及其背后的简单性。我很乐意使用列表推导,但是我不知道目前我的逻辑在哪里失败。就像我的第100次尝试一样,从此版本开始,它仅绘制初始房屋,我认为它在某个地方无限循环。...

我的问题在于遍历整个列表以获取每个新值。我是否需要每次都遍历它们,还是可以使用某些中频条件?编辑:它不断循环遍历随机值,但是我使用的两个IF语句都不接受它们。

附带说明:使用我当前的代码,它们每次也会更改绘图的方向...不知道为什么会发生这种情况。...

python list random collision-detection turtle-graphics
1个回答
0
投票

Defo没有最好的方法,但是

import turtle
import time
import random
t = turtle.Turtle()
turtle.screensize(1920,1000)
x_verif = []
y_verif = []
t.speed(0)
collision = None


def square():
    for s in range(0, 4):
        t.forward(90)
        t.left(90)

def triangle():
    for s in range(0, 2):
        t.left(-60)
        t.forward(52)

def house():
    square()
    t.left(90)
    t.forward(90)
    triangle()
    t.left(30)#returning to 90 degrres
def scatter():
    beenAt = [] #this will hold every place that there is a house
    for i in range(100): 
        t.penup()
        loop = True
        while loop == True:
            area = random.randint(-850, 850) 
            for i in range(91): #looping for the size of the house
                if area+i in beenAt: #if the number chosen plus i is in beenAt stop because we cant use that place
                    break
                if i == 90:#if at the finial part of our loop then draw house
                    t.goto(area, 0)
                    t.pendown()
                    for i in range(area, area + 91): 
                        beenAt.append(i) #add to been at list every place we have drawn
                    house()
                    loop = False
scatter()
© www.soinside.com 2019 - 2024. All rights reserved.