用窗户和门绘制建筑物的功能

时间:2017-05-15 21:52:15

标签: python graphics zelle-graphics

我正在创建一个绘制办公大楼的功能:

  • 窗口是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

1 个答案:

答案 0 :(得分:0)

这似乎是自Irwin Allen&#34; Towering Inferno&#34;以来最严重的虚拟高层大灾难。在文件中似乎至少有两个不同的不完整实现,其中大多数函数从未被调用,代码什么都没有。这是我在废墟上的打捞工作:

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

您可以点击窗口切换颜色。 (我选择了&#39; on&#39; /&#39; off&#39; motif。)点击建筑物的左上角退出。