[英]Python Zelle Graphics & While loop
我正在嘗試創建不重疊的矩形抽認卡並將它們放置在 Zelle Graphics 中的隨機位置。 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)
主要的()
您的大多數問題源於您對函數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()
最初,由於此錯誤import
,您的代碼根本沒有運行:
from random import randrange *
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.