2017-04-27 41 views
1

對於這個項目,我需要在隨機斑點處繪製400個不同的方塊,並且至少相距5個像素。目前,盒子大部分都是在窗口的底部繪製的,在程序凍結前只有大約一半是繪製的。我諮詢過我的教授,他們很難過。randint()只產生較高的值並且沒有繪製足夠的對象

#Importing things I need 
import random 
from graphics import * 

def enterValue(used_spots, y_spots, corner1, corner2): 
    #enters the value where our square starts into the array, if it's a 
    #valid value. 
    used_spots.append(corner1) 
    y_spots.append(corner2) 

def checkPlace(used_spots,y_spots,corner1,corner2): 
    #checks the placement of every square to see if our new one is valid. 
    # the total size of a square is 20px edge + 5 px Border + 3 px width. 
    # So we need to check based on that number. 
    slot=0 
    check=0 
    for slot in range(0,len(used_spots)): 
     #if the distance between the squares is more than 28 pixels, return 
     # true. Else, stop checking and return false 
     if abs(corner1-used_spots[slot])<28 and abs(corner2-y_spots[slot]<28): 
      check=0 
      break 
     else: 
      check=1 
    return check 

def randomCorners(): 
    #gets us a random variable for x and y of a box's corner 
    corner1=random.randint(3,1800-28) 
    corner2=random.randint(3,900-28) 
    return corner1, corner2 

def drawBox(corner1,corner2,EDGE,WIDTH,colors,win): 
    #Draws the box 
    point1=Point(corner1,corner2) 
    point2=Point(corner1+EDGE,corner2+EDGE) 
    square=Rectangle(point1,point2) 
    square.setWidth(WIDTH) 
    square.setFill(random.choice(colors)) 
    square.draw(win) 

def main(): 
    #delcaring variables 
    corner1=0 
    corner2=0 
    used_spots=[] 
    y_spots=[] 
    WIDTH=3 
    EDGE=20 
    colors=["red","orange","yellow","blue","pink","green"] 
    win=GraphWin("MAINWINDOW",1800,900) 
    win.setBackground("white") 

    #Draws a random box at a random spot, then makes a spot for a new potential box 
    #and tests it 
    corner1,corner2=randomCorners() 
    drawBox(corner1,corner2,EDGE,WIDTH,colors,win) 
    enterValue(used_spots,y_spots,corner1,corner2) 
    corner1,corner2=randomCorners() 

    while len(used_spots) < 400: 
     #If we can draw a box there, draw it and add coords to the lists, 
     #then generates a new set of coordinates. 
     if checkPlace(used_spots,y_spots,corner1,corner2)==1: 
      drawBox(corner1,corner2,EDGE,WIDTH,colors,win) 
      enterValue(used_spots,y_spots,corner1,corner2) 

     #otherwise, make a new coordinate and try again. 
     corner1,corner2=randomCorners() 

main() 
+1

我得到'ImportError:沒有名爲'graphics''的模塊。 –

+0

'graphics'不是一個標準的Python模塊。它來自哪裏,其他人怎麼能得到它? – martineau

+0

你可以在這裏http://mcsp.wartburg.edu/zelle/python/graphics.py將它複製並粘貼到一個python文件中並保存到你的文件的任何位置。 – Muroxxas

回答

2

的問題是在你的checkPlace功能:

if abs(corner1-used_spots[slot])<28 and abs(corner2-y_spots[slot]<28): 

應該

if abs(corner1-used_spots[slot])<28 and abs(corner2-y_spots[slot])<28: 

(最後括號注位置)。

您的代碼正在將比較結果與abs函數進行比較,而不是在比較中使用abs函數的結果。

所以,每當corner2y_spots[slot] + 28少,條件將評估爲真(因爲abs不叫,直到比較後),這意味着較低的值(在屏幕的頂部)將觸發它,如果被拒絕他們也遇到了其他條件。最終在used_spotsy_spots中會有足夠的條目,至少有一個始終觸發if中的兩個條件,並且程序鎖定,陷入無限循環,試圖找到有效的座標。

相關問題