2017-04-27 6 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関数に比べて小さいものの結果を渡していました。

corner2未満y_spots[slot] + 28た時はいつでも(absは比較後まで呼び出されなかったため)ので、条件がtrueに評価されます、低い値(画面の上部)は、それをトリガーすると場合は拒否されていることを意味彼らはまた、他の条件を満たした。最終的にused_spotsy_spotsには、少なくとも1つが常にifの両方の条件をトリガしていて、プログラムがロックアップして、有効な座標を見つけようと無限ループに詰め込まれているというエントリがあります。

関連する問題