2017-05-15 94 views
-5

我創建一個函數來繪製一個辦公大樓建設:功能來繪製門窗

  • 窗口是20個像素的正方形

  • 窗口之間的差距是10個像素

  • 門是20個像素寬,50個像素高,和橙

我的代碼沒有正確繪製:

from graphics import * 
from random import * 

def add_window(win, nH, nV): 
    w, h = win.getWidth(), win.getHeight() 
    rects = [] 
    for x in range(nH): 
     rects.append([]) 
     for y in range(nV): 
      r = Rectangle(Point(x *w//nH, y *h//vV), 
          Point((x+1)*w//nH, (y+1)*h//nV)) 
      window = [ r, 
         True, 
         [ 'red', 'green' ] 
         ] 
      rects[x].append(window) 
      rects[x][y][0].draw(win) 
      rects[x][y][0].setOutline('blue') 
      color = window[2][randint[0,1]] 
      rects[x][y][0].setFill(color) 
    return rects 

WIN_W, WIN_H = 500, 400 

#Top left coner of the building 
BLDG_LEFT, BLDG_TOP = 50, 50 

#How many floors, how many windows perfloor, window digit and gap 
FLOORS, WIN_FLR, WIN_SZ, GAP = 10, 5, 20, 5 

win = None 

#Syntax : window[x][y] 
# [0] : Rectangle() object 
# [1] : True/False 
windows = [] 
#-------------------------------------------------------------------- 

def draw_window(x, y): 
    global windows 
    windows = [] 
    left = BLDG_LEFT + GAP + x* (WIN_SZ+GAP) 
    top = BLDG_TOP + GAP + y* (WIN_SZ+GAP) 
    r = Rectangle(Point(x *WIN_SZ+GAP, y *(WIN_SZ+GAP)), 
        Point((x+1)*WIN_SZ+GAP, (y+1)*(WIN_SZ+GAP))) 
    windows[x][y].append(r) 
    bit = randint(0,1) 
    windows[x][y].append(bool(bit)) 
    windows[x][y][0].setFill(COLORS[bit]) 
    windows[x][y][0].draw(win) 


def draw_windows(): 
    for i in range(WIN_FLR): 
     windows.append([]) 
     for j in range(FLOORS): 
      windows[i].append([]) 
      draw_window(i, j) 


def office_tower(): 
    global win 
    win = GraphWin("OFFICE TOWER", WIN_W, WIN_H) 
    draw_window(1, 1) 

    while True: 
     pt = win.getmouse() 
     if pt.x < 10 and pt.y < 10: 
      break 

     # windows coordinates 
     x = int((pt.x - BLDG_LEFT - GAP)/(WIN_SZ + GAP)) 
     y = int((pt.y - BLDG_TOP - GAP)/(WIN_SZ + GAP)) 
     print(str((pt.x, pt.y)) + ' --> ' + str((x, y))) 

     windows[x][y][1] = netwindows[x][y][1] 
     windows[x][y][0].setFill(COLORS[windows[x][y][1]]) 


def draw_building(): 
    global windows 
    win = GraphWin("OFFICE TOWER", WIN_W, WIN_H) 
    N_H, N_V = 5, 10 

    while True: 
     pt = win.getMouse() 
     m_x, m_y = pt.getX(), pt.getY() 

     # Grid coordinates: 
     g_x = m_x // (WIN_W//N_H) 
     g_y = m_y // (WIN_H//N_V) 

     # For development purposes: 
     if m_x < 10 and m_y < 10: 
      break 
+2

請具體說明。輸出的問題究竟是什麼?你期望什麼,你得到了什麼? – DyZ

+0

你用什麼軟件包導入爲'graphics'?它不是來自標準庫。 'GraphWin'類表明它是John Zelle的[與_Python程序設計:計算機科學入門的配套軟件包](http://mcsp.wartburg.edu/zelle/python/graphics/graphics/graphref.html)...是嗎? ?由於大多數人都沒有安裝該軟件包,因此您應該發佈任何正在繪製的截圖(或者複製並粘貼控制檯錯誤,如果它在設法繪製任何內容之前崩潰)。 –

回答

0

這似乎是歐文艾倫「高聳的地獄」以來最嚴重的虛擬高層災難。在文件中似乎至少有兩個不完整的實現,其中絕大多數函數都不會被調用,代碼也不會畫任何東西。這裏的廢墟上我打撈工作:

from random import randint 
from graphics import * 

GRAPHIC_WINDOW_WIDTH, GRAPHIC_WINDOW_HEIGHT = 500, 400 

# Top left coner of the building 
BUILDING_LEFT, BUILDING_TOP = 50, 50 

COLORS = ['gray25', 'gray85'] # lights off, lights on 

# How many BUILDING_FLOORS, how many windows per floor, window size and gap 
BUILDING_FLOORS, WINDOWS_PER_FLOOR, WINDOW_SIZE, WINDOW_GAP = 10, 5, 20, 5 

WINDOW_FRAME = WINDOW_SIZE + WINDOW_GAP 

# Syntax : window[x][y] 
# [0] : Rectangle() object 
# [1] : True/False 

#-------------------------------------------------------------------- 

def draw_window(row, column, left, top): 

    r = Rectangle(Point(left + column * WINDOW_FRAME, top + row * WINDOW_FRAME), \ 
     Point(left + (column + 1) * WINDOW_FRAME, top + (row + 1) * WINDOW_FRAME)) 

    bit = bool(randint(0, 1)) 

    r.setFill(COLORS[bit]) 

    r.draw(win) 

    windows[row][column] = [r, bit] 

def draw_windows(left, top): 
    for row in range(BUILDING_FLOORS): 
     windows.append([]) 
     for column in range(WINDOWS_PER_FLOOR): 
      windows[row].append(None) 
      draw_window(row, column, left, top) 

def office_tower(): 
    draw_windows(BUILDING_LEFT, BUILDING_TOP) 

    while True: 
     pt = win.getMouse() 

     if pt.x < BUILDING_LEFT and pt.y < BUILDING_TOP: 
      break # clean exit stategy 

     # windows coordinates 
     column = int((pt.x - BUILDING_LEFT - WINDOW_GAP)/WINDOW_FRAME) 
     row = int((pt.y - BUILDING_TOP - WINDOW_GAP)/WINDOW_FRAME) 

     # print((pt.x, pt.y), '-->', (row, column)) 

     windows[row][column][1] = not windows[row][column][1] 
     windows[row][column][0].setFill(COLORS[windows[row][column][1]]) 

win = GraphWin('OFFICE TOWER', GRAPHIC_WINDOW_WIDTH, GRAPHIC_WINDOW_HEIGHT) 

windows = [] 

office_tower() 

win.close() 

這得出以下建築:

enter image description here

在這裏你可以在窗戶上點擊切換它們的顏色。 (我選擇了'開'/'關'主題。)點擊建築物的左上方退出。