Python-Zelle图形和While循环

2024-05-13 03:37:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建不重叠的矩形抽认卡,并将它们放置在Zelle图形中的随机位置。xMin和yMin是矩形左上角的坐标,xMax和yMax是矩形右下角的坐标。我尝试生成随机(xCenter,yCenter)坐标来生成一个新矩形,并检查新矩形是否与任何现有矩形重叠。如果是重叠的,则生成新的随机点,直到不再重叠为止。你知道吗

我得到了检查重叠的函数,但是之后while循环出现了问题。我是Python的初学者,非常感谢您的帮助!你知道吗

from graphics import *
from random import randrange *

def checkLocation(xMin,xMax,yMin,yMax,xMinList,xMaxList,yMinList,yMaxList):
    for x in range (xMin,xMax):
        for y in range (yMin,yMax):
            for i in range(len(xMinList)):
                if xMinList[i] < x < xMaxList[i] and yMinList[i] < y < yMaxList[i]:
                    return False 
    #if the new rectangle isn't overlapping, append its 4 corner into the list for future comparison:                
    xMinList.append(xMin)
    xMaxList.append(xMax)
    yMinList.append(yMin)
    yMaxList.append(yMax)

    return xMinList,xMaxList,yMinList,yMaxList



def main():
    win = GraphWin("Flash Card", 800,800)
    xCenter, yCenter = randrange (200,600), randrange (200,600) #display the words from the text in randomly generated locations                   
    xMin = xCenter - 50
    xMax = xCenter + 50

    yMin = yCenter - 50
    yMax = yCenter + 50

    xMinList = [300,500,200,100,600] #I hard coded these 4 lists for the purpose of testing
    xMaxList = [350,580,220,140,650]

    yMinList = [100,500,300,600,400]
    yMaxList = [160,540,325,680,450]

    #while checkLocation is False (location overlapping), check again until it's True (not overlapping)
    while not checkLocation(xMin,xMax,yMin,yMax,xMinList,xMaxList,yMinList,yMaxList):
        checkLocation(xMin,xMax,yMin,yMax,xMinList,xMaxList,yMinList,yMaxList)
    xMinList, xMaxList,yMinList,yMaxList =  checkLocation(xMin,xMax,yMin,yMax,xMinList,xMaxList,yMinList,yMaxList)


    rect = Rectangle (Point(xMin,yMin),Point(xMax,yMax))
    rect.draw(win)

主()


Tags: theforxminymax矩形appendxmaxymin
1条回答
网友
1楼 · 发布于 2024-05-13 03:37:02

大多数问题都源于函数checkLocation()的定义和使用:有时它返回一个布尔值,有时返回一个列表元组;您似乎没有意识到它更新了有问题的列表,因此它们不需要返回和重新分配;有一次您似乎无缘无故地调用它。你知道吗

我在下面修改了你的代码,画了十张不重叠的闪卡,基本上都是相同的代码,只是按更合理的顺序排列:

from graphics import *
from random import randrange

def checkLocation(xMin, xMax, yMin, yMax, xMinList, xMaxList, yMinList, yMaxList):
    for x in range(xMin, xMax):
        for y in range(yMin, yMax):
            for i in range(len(xMinList)):
                if xMinList[i] < x < xMaxList[i] and yMinList[i] < y < yMaxList[i]:
                    return False

    # if the new rectangle isn't overlapping, append its 4 corner into the list for future comparison:
    xMinList.append(xMin)
    xMaxList.append(xMax)
    yMinList.append(yMin)
    yMaxList.append(yMax)

    return True

def main():
    win = GraphWin("Flash Card", 800, 800)

    xMinList = []
    xMaxList = []

    yMinList = []
    yMaxList = []

    for _ in range(10):

        xCenter, yCenter = randrange(200, 600), randrange(200, 600)
        xMin = xCenter - 50
        xMax = xCenter + 50

        yMin = yCenter - 50
        yMax = yCenter + 50

        # while checkLocation is False (location overlapping), check again until it's True (not overlapping)
        while not checkLocation(xMin, xMax, yMin, yMax, xMinList, xMaxList, yMinList, yMaxList):
            # display the words from the text in randomly generated locations
            xCenter, yCenter = randrange(200, 600), randrange(200, 600)
            xMin = xCenter - 50
            xMax = xCenter + 50

            yMin = yCenter - 50
            yMax = yCenter + 50

        rect = Rectangle(Point(xMin, yMin), Point(xMax, yMax))
        rect.draw(win)

    win.getMouse() # Pause to view result
    win.close()    # Close window when done

main()

enter image description here

最初,由于以下错误的import,您的代码根本没有运行:

from random import randrange *

相关问题 更多 >