2017-05-15 1 views
-5

Je crée une fonction pour établir une tour de bureau:fonction de tirer la construction de fenêtres et de portes

  • fenêtres sont de 20 pixels carrés

  • l'écart entre les fenêtres est de 10 pixels

  • la porte est de 20 pixels de large, de 50 pixels de haut, et orange

Mon code ne tire pas correctement:

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

Veuillez être précis. Quel est exactement le problème avec la sortie? Qu'attendiez-vous et qu'as-tu obtenu? – DyZ

+0

Quel paquet importez-vous en tant que 'graphics'? Ce n'est pas de la bibliothèque standard. La classe 'GraphWin' suggère que c'est le paquet compagnon de John Zelle pour _Python Programming: Introduction à l'informatique_ (http://mcsp.wartburg.edu/zelle/python/graphics/graphics/graphref.html) ... ? Puisque la plupart des gens n'auront pas ce paquet installé, vous devriez poster une capture d'écran de tout ce qu'il dessine (ou copier et coller les erreurs de la console, si elle plante avant de pouvoir dessiner quelque chose). –

Répondre

0

Cela semble être la pire catastrophe de grande hauteur virtuelle depuis « Towering Inferno » Irwin Allen. Il semble y avoir au moins deux différentes implémentations incomplètes dans le fichier où la plupart des fonctions ne sont jamais appelées et le code ne tire rien. Voici mon travail de récupération sur les ruines:

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

Dessine le bâtiment suivant:

enter image description here

Où vous pouvez cliquer sur les fenêtres pour changer leur couleur. (J'ai choisi un motif 'on'/'off'.) En cliquant en haut à gauche des sorties du bâtiment.