class TextBox(GCompound): def __init__(self, msg, font="Helvetica", font_size=12, font_color="#000000"): super().__init__() self.label = GLabel(msg) self.label.setFont("{}px '{}'".format(font_size, font)) self.label.setColor(font_color) self.add(self.label, -self.get_width() // 2, (self.label.getAscent() - self.label.getDescent()) // 2) def get_height(self): return self.label.getAscent() + self.label.getDescent() def get_width(self): return self.label.getBounds().getWidth()
def createCenteredLabel(text, x, y, font=None): """ Creates a new GLabel centered at the point (x, y) in both the horizontal and vertical directions. If font is specified, it is used to set the font of the label. """ label = GLabel(text) if font is not None: label.setFont(font) label.setLocation(x - label.getWidth() / 2, y + label.getAscent() / 2) return label
def createBeehive(puzzle): global beehive cell = None puzzle = puzzle.upper() """ Creates a GCompound that contains the objects necessary to display the beehive structure for the Spelling Bee. The puzzle parameter is a seven-letter string that defines the labels on the hexagons. The first letter goes in the center hexagon; each subsequent letter goes in a hexagon whose center is HEX_SEP pixels from the center at angles of 30, 90, 150, 210, 270, and 330 degrees as the letters move counterclockwise around the hexagon. """ beehive = GCompound() # Create GCompund cell = createHexagon(HEX_SIDE) cell.setFilled(True) cell.setColor(CENTER_HEX_COLOR) beehive.add(cell) # Add center cell letter = GLabel(puzzle[0]) letter.setFont(LABEL_FONT) beehive.add(letter, cell.getX() - letter.getWidth() / 2, cell.getY() + HEX_LABEL_DY) # Add center letter for i in range(6): # Add other cells and letters cell = createHexagon(HEX_SIDE) cell.setFilled(True) cell.setColor(OUTER_HEX_COLOR) cell.movePolar(HEX_SEP, 30 + 60 * i) beehive.add(cell) letter = GLabel(puzzle[i + 1]) letter.setFont(LABEL_FONT) beehive.add(letter, cell.getX() - letter.getWidth() / 2, cell.getY() + HEX_LABEL_DY) shuffle = createHexagon(HEX_SIDE) # Create shuffle button shuffle.setFilled(True) shuffle.setColor(CENTER_HEX_COLOR) shuffle.movePolar(HEX_SEP * 1.75, -120) beehive.add(shuffle) letter = GLabel("SHUFFLE") letter.setFont("12px bold 'Helvetica Neue','Sans-Serif'") beehive.add(letter, shuffle.getX() - letter.getWidth() / 2, shuffle.getY() + letter.getAscent() / 2) return beehive # Return completed beehive
class SudokuDigitSelectorSubcell(GCompound): def __init__(self, digit): GCompound.__init__(self) self.digit = str(digit) cell = GRect(0, 0, SUBCELL_WIDTH, SUBCELL_WIDTH) cell.setColor(CELL_BORDER_COLOR) cell.setFillColor(SUBCELL_FILL_COLOR) cell.setFilled(True) self.add(cell, 0, 0) self.label = GLabel(digit) self.label.setFont(SUBCELL_FONT) self.label.setColor(SUBCELL_TEXT_COLOR) self.add(self.label, SUBCELL_WIDTH//2 - self.label.getWidth()//2, SUBCELL_WIDTH//2 + self.label.getAscent()//2 - 3)
def displayWordsList(puzzle): puzzle = puzzle.lower() legalWords = [] # Create blank list of words points = 0 x = WORDLIST_X i = 0 for word in ENGLISH_WORDS: if isLegalEntry(word, puzzle): legalWords.append(word) # Add leagal words to legalWords def isPangram(word): # Check to see if Pangram letters = 0 for ch in puzzle: if ch in word: letters += 1 if letters == len(puzzle): return True else: return False for word in legalWords: points += len(word) - 3 # Number of points per word if isPangram(word): # Make changes if pangram points += 7 line = GLabel(word) line.setColor(PANGRAM_COLOR) line.setFont(WORDLIST_FONT) else: line = GLabel(word) line.setFont(WORDLIST_FONT) y = WORDLIST_Y + WORDLIST_DY * i gw.add(line, x, y) # Add words to window i += 1 if y >= GWINDOW_HEIGHT - (SCORE_BASELINE + SCORE_WORDLIST_SEP + line.getAscent()): x += WORDLIST_DX i = 0 score = GLabel( str(len(legalWords)) + " words; " + str(points) + " points") # Add score to window score.setFont(WORDLIST_FONT) gw.add(score, WORDLIST_X, GWINDOW_HEIGHT - SCORE_BASELINE)
def __init__(self, node): GCompound.__init__(self) self._node = node frame = GRect(ArpanetMonitor.WIDTH, ArpanetMonitor.MAX_NODES * ArpanetMonitor.VSPACE) self.add(frame, 0, ArpanetMonitor.VSPACE) label = GLabel(node.getName()) label.setFont(ArpanetMonitor.FONT) x = ArpanetMonitor.MARGIN y = label.getAscent() self.add(label, x, y) self._label = label self._lines = [] for i in range(ArpanetMonitor.MAX_NODES): y += ArpanetMonitor.VSPACE label = GLabel("") label.setFont(ArpanetMonitor.FONT) self.add(label, x, y) self._lines.append(label) self.update()
class MenuBar(GCompound): def __init__(self): GCompound.__init__(self) bar = GRect(GWINDOW_WIDTH, MENU_BAR_HEIGHT) bar.setFilled(True) bar.setColor(CELL_BORDER_COLOR) bar.setFillColor(MENU_BAR_BGCOLOR) self.add(bar, 0, 0) self.label = GLabel("Y O U D O K U") self.label.setFont(MENU_TITLE_FONT) self.label.setColor(MENU_TITLE_COLOR) self.add(self.label, GWINDOW_WIDTH//2 - self.label.getWidth()//2, MENU_BAR_HEIGHT//2 + self.label.getAscent()//2 - 5) def mousedown(self, x, y): pass def mouseup(self, x, y): pass
def createCenteredLabel(text, x, y, font=None): label = GLabel(text) if font is not None: label.setFont(font) label.setLocation(x - label.getWidth() / 2, y + label.getAscent() / 2) return label
class SudokuCell(GCompound): def __init__(self, digit): GCompound.__init__(self) if digit != 0: self.digit = str(digit) else: self.digit = None self.cell = GRect(0, 0, CELL_WIDTH, CELL_WIDTH) self.cell.setColor(CELL_BORDER_COLOR) self.cell.setFillColor(CELL_GOOD_COLOR) self.cell.setFilled(True) self.add(self.cell, 0, 0) self.label = None self.only_a_suggestion = True self.render_label() self.selector = None def get_digit(self): if self.digit == None or self.digit == '' or self.only_a_suggestion: return 0 else: return int(self.digit) def set_background_color(self, color): self.cell.setFillColor(color) def suggest_solution(self, digit): if self.only_a_suggestion: self.digit = str(digit) self.render_label() def render_label(self): if self.label is not None: self.remove(self.label) if self.digit is not None and self.digit != "0": self.label = GLabel(self.digit) else: self.label = GLabel("") self.label.setFont(CELL_FONT) if self.only_a_suggestion: self.label.setColor(SUGGESTION_TEXT_COLOR) else: self.label.setColor(CELL_TEXT_COLOR) self.add(self.label, CELL_WIDTH//2 - self.label.getWidth()//2, CELL_WIDTH//2 + self.label.getAscent()//2 - 7) def mousedown(self): self.selector = SudokuDigitSelector() self.add(self.selector, 0, 0) def mouseup(self, x, y): if self.selector is not None: digit = self.selector.mouseup(x, y) if digit is not None: self.digit = digit if str(self.digit) == "0" or self.digit == "": self.digit = "" self.only_a_suggestion = True elif self.digit != "": self.only_a_suggestion = False self.render_label() self.remove(self.selector) self.selector = None