-4

I'm creating a function to draw a office tower:

  • windows are 20 pixels square

  • the gap between the windows is 10 pixels

  • the door is 20 pixels wide, 50 pixels tall, and orange

My code doesn't draw it properly:

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
cdlane
  • 40,441
  • 5
  • 32
  • 81
  • 2
    Please be specific. What exactly is the problem with the output? What did you expect and what did you get? – DYZ May 15 '17 at 21:54
  • What package are you importing as `graphics`? It's not from the standard library. The `GraphWin` class suggests it's John Zelle's [companion package to _Python Programming: An Introduction to Computer Science_](http://mcsp.wartburg.edu/zelle/python/graphics/graphics/graphref.html)... Is it? Since most people won't have that package installed, you should post a screenshot of whatever it's drawing (or copy and paste the console errors, if it crashes before it manages to draw anything). – Kevin J. Chase May 15 '17 at 22:09

1 Answers1

0

This seems to be the worst virtual high-rise disaster since Irwin Allen's "Towering Inferno". There appear to be at least two different incomplete implementations in the file where most of the functions are never called and the code draws nothing. Here's my salvage job on the ruins:

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()

This draws the following building:

enter image description here

Where you can click on the windows to toggle their color. (I chose an 'on' / 'off' motif.) Clicking to the upper left of the building exits.

cdlane
  • 40,441
  • 5
  • 32
  • 81