class EnigmaLamp(GCompound): def __init__(self, letter): GCompound.__init__(self) lamp = GOval(LAMP_RADIUS * 2, LAMP_RADIUS * 2) lamp.setColor(LAMP_BORDER_COLOR) lamp.setFillColor(LAMP_BGCOLOR) self.add(lamp, -LAMP_RADIUS, -LAMP_RADIUS) # create design for lamps self.ch = GLabel(letter) self.ch.setColor(LAMP_OFF_COLOR) self.ch.setFont(LAMP_FONT) self.add(self.ch, -self.ch.getWidth() / 2, LAMP_LABEL_DY) def setState(self, state): # set state of lamp to be on or off if state: self.ch.setColor(LAMP_ON_COLOR) else: self.ch.setColor(LAMP_OFF_COLOR) def getState(self): # get state of lamp )(n or off) if self.ch.getColor() == LAMP_ON_COLOR: return True else: return False
def DrawHexagon(puzzle): angle = 30 figure = GCompound() figureinside = GCompound() hexagon = createHexagon(HEX_SIDE) hexagon.setFilled(True) hexagon.setColor(CENTER_HEX_COLOR) figureinside.add(hexagon) labelinside = GLabel(puzzle[0]) labelinside.setFont(LABEL_FONT) figureinside.add(labelinside, -0.38 * labelinside.getWidth(), HEX_LABEL_DY) figure.add(figureinside) for i in range(1, 7): figureinside = GCompound() hexagon = createHexagon(HEX_SIDE) hexagon.setFilled(True) hexagon.setColor(OUTER_HEX_COLOR) figureinside.add(hexagon) labelinside = GLabel(puzzle[i]) labelinside.setFont(LABEL_FONT) figureinside.add(labelinside, -0.38 * labelinside.getWidth(), HEX_LABEL_DY) figureinside.movePolar(HEX_SEP, angle) angle += 60 figure.add(figureinside) gw.add(figure, GWINDOW_WIDTH / 2, GWINDOW_HEIGHT / 2)
def ballsMessage(): global ballsmsg ballsmsg = GLabel("Balls Remaining: " + str(N_BALLS)) # Tell player number of balls remaing ballsmsg.setFont("30pt 'Arial'") x = (GWINDOW_WIDTH - ballsmsg.getWidth()) / 2 gw.add(ballsmsg, x, 2 * GWINDOW_HEIGHT / 3 + spacing)
def runSimulation(): decisionIndicators = createWindow() progressLabel = GLabel("GAMES PLAYED: 0") progressLabel.setFont(MESSAGE_FONT) gw.add(progressLabel, 20, 50) def adjustIndicators(): progressLabel.setLabel('GAMES PLAYED: {}'.format(agent.epochsSoFar)) for state in decisionIndicators: action = agent.getPolicy(state) indicator = decisionIndicators[state] if action == 'hit': indicator.setFillColor("green") indicator.setFilled(True) else: indicator.setFillColor("red") indicator.setFilled(True) def adjustIndicatorsAlt(): progressLabel.setLabel('GAMES PLAYED: {}'.format(agent.epochsSoFar)) for state in decisionIndicators: hitValue = agent.getQValue(state, 'hit') standValue = agent.getQValue(state, 'stand') hitDifferential = hitValue - standValue if hitDifferential > 1: color = "#00FF00" elif hitDifferential > 0.5: color = "#00CC00" elif hitDifferential > 0.2: color = "#008800" elif hitDifferential > 0.1: color = "#006600" elif hitDifferential > 0.05: color = "#004400" elif hitDifferential > -0.05: color = "#000000" elif hitDifferential > 0.1: color = "#440000" elif hitDifferential > -0.2: color = "#660000" elif hitDifferential > -0.5: color = "#880000" elif hitDifferential > -1: color = "#CC0000" else: color = "#FF0000" indicator = decisionIndicators[state] indicator.setFillColor(color) indicator.setFilled(True) timer1 = gw.createTimer(agent.go, 1000) timer1.setRepeats(True) timer1.start() timer2 = gw.createTimer(adjustIndicatorsAlt, 1500) timer2.setRepeats(True) timer2.start()
def create_centered_label(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) center_label(label, x, y) return label
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 __init__(self, letter): GCompound.__init__(self) lamp = GOval(LAMP_RADIUS * 2, LAMP_RADIUS * 2) lamp.setColor(LAMP_BORDER_COLOR) lamp.setFillColor(LAMP_BGCOLOR) self.add(lamp, -LAMP_RADIUS, -LAMP_RADIUS) # create design for lamps self.ch = GLabel(letter) self.ch.setColor(LAMP_OFF_COLOR) self.ch.setFont(LAMP_FONT) self.add(self.ch, -self.ch.getWidth() / 2, LAMP_LABEL_DY)
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 ArpanetSimulation(): def clickAction(e): nodeList = [] obj = gw.getElementAt(e.getX(), e.getY()) if isinstance(obj, ArpanetNode): nodeList = [obj] elif isinstance(obj, GLine): active = obj.getColor() == "Black" start = obj.getStartPoint() end = obj.getEndPoint() n1 = gw.getElementAt(start.getX(), start.getY()) n2 = gw.getElementAt(end.getX(), end.getY()) if active: obj.setColor("LightGray") n1.removeNeighbor(n2) n2.removeNeighbor(n1) else: obj.setColor("Black") n1.addNeighbor(n2) n2.addNeighbor(n1) elif obj == allButton: nodeList = arpanet.getNodes() elif isinstance(obj, GLabel): node = arpanet.findNode(obj.getLabel()) name = node.getName() if node.isActive(): node.setActive(False) obj.setColor("LightGray") node.setRoutingTable(RoutingTable(name)) monitors[name].update() else: node.setActive(True) obj.setColor("Black") monitors[name].update() for node in nodeList: name = node.getName() myTable = node.getRoutingTable() if node.isActive(): for neighbor in node.getNeighbors(): if neighbor.isActive(): neighbor.getRoutingTable().update(name, myTable) for name in monitors: monitors[name].update() gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) arpanet = createArpanetGraph(gw) monitors = createArpanetMonitors(gw, arpanet) allButton = GLabel("Update All") allButton.setFont(ALL_BUTTON_FONT) gw.add(allButton, ALL_BUTTON_X, ALL_BUTTON_Y) gw.addEventListener("click", clickAction)
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 __init__(self, letter): GCompound.__init__(self) key = GOval(KEY_RADIUS * 2, KEY_RADIUS * 2) key.setLineWidth(KEY_BORDER) key.setColor(KEY_BORDER_COLOR) key.setFillColor(KEY_BGCOLOR) self.add(key, -KEY_RADIUS, -KEY_RADIUS) # create design for keys self.ch = GLabel(letter) self.ch.setColor(KEY_UP_COLOR) self.ch.setFont(KEY_FONT) self.add(self.ch, -self.ch.getWidth() / 2, KEY_LABEL_DY)
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 setUp(): # Create display global ballsmsg, N_BALLS, greet for i in range(5): # Create rows of bricks of specified colors color = colors[i] y = (BRICK_HEIGHT + BRICK_SEP) * 2 * i for i in range(2): drawRow(100 + (BRICK_HEIGHT + BRICK_SEP) * i + y, color) makePaddle() # Create paddle (in setup) greet = GLabel("Click to Begin") greet.setFont("36pt 'Arial'") x = (GWINDOW_WIDTH - greet.getWidth()) / 2 gw.add(greet, x, 2 * GWINDOW_HEIGHT / 3)
def createButton(s):# makes a button with the string s button=GCompound() buttonSize=75 label=GLabel(s) label.setColor("white") label.setFont("8px 'Sans-Serif'") c=GOval(-(label.getWidth()+20)//2,-buttonSize//2,label.getWidth()+20,buttonSize) c.setFillColor("black") c.setFilled(True) button.add(c) button.add(label,-label.getWidth()//2,0) return button
def __init__(self, text, fn=None): GCompound.__init__(self) label = GLabel(text) label.set_font(self.BUTTON_FONT) width = max(self.BUTTON_MIN_WIDTH, 2 * self.BUTTON_MARGIN + label.get_width()) frame = GRect(width, self.BUTTON_DEFAULT_HEIGHT) frame.set_filled(True) frame.set_fill_color("White") self.add(frame) self.add(label) self.text = text self.label = label self.frame = frame self.fn = fn self._recenter()
def createCardTotalLabels(): alphabet = ['02','03','04','05','06','07','08','09','10', '11','12','13','14','15','16','17','18','19', '20','21'] alphabetLabels = [GLabel(letter) for letter in alphabet] for label in alphabetLabels: label.setFont(LETTER_FONT) return alphabetLabels
class EnigmaKey(GCompound): def __init__(self, letter): GCompound.__init__(self) key = GOval(KEY_RADIUS * 2, KEY_RADIUS * 2) key.setLineWidth(KEY_BORDER) key.setColor(KEY_BORDER_COLOR) key.setFillColor(KEY_BGCOLOR) self.add(key, -KEY_RADIUS, -KEY_RADIUS) # create design for keys self.ch = GLabel(letter) self.ch.setColor(KEY_UP_COLOR) self.ch.setFont(KEY_FONT) self.add(self.ch, -self.ch.getWidth() / 2, KEY_LABEL_DY) def setLetterColor(self, color): # change letter color if needed self.ch.setColor(color) def mousedownAction(self, enigma): # define mousedownAction self.setLetterColor(KEY_DOWN_COLOR) enigma.keyPressed(self) def mouseupAction(self, enigma): # define mouseupAction self.setLetterColor(KEY_UP_COLOR) enigma.keyReleased(self)
def __init__(self, letter, perm, inverse): GCompound.__init__(self) rotor = GRect(ROTOR_WIDTH, ROTOR_HEIGHT) rotor.setColor(ROTOR_BGCOLOR) rotor.setFilled(True) self.add(rotor, -ROTOR_WIDTH / 2, -ROTOR_HEIGHT / 2) # create design for rotors self.ch = GLabel(letter) self.ch.setColor(ROTOR_COLOR) self.ch.setFont(ROTOR_FONT) self.add(self.ch, -self.ch.getWidth() / 2, ROTOR_LABEL_DY) self.perm = perm self.inverse = inverse self.offset = 0 self.rotor = rotor
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 shuffle(e): # Function to shuffle hexs global shuffleword, beehive obj = gw.getElementAt(e.getX(), e.getY()) # if obj is not None: letters = shuffleword[1:7] # Only shuffle after first letter letters = [char for char in letters] # Break shuffleword into list random.shuffle(letters) letters = "".join(letters) # Create new shuffled string for i in range(6): # Add new 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(letters[i]) letter.setFont(LABEL_FONT) beehive.add(letter, cell.getX() - letter.getWidth() / 2, cell.getY() + HEX_LABEL_DY)
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 __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()
def createArpanetGraph(gw): graph = ArpanetGraph(gw) for name, x, y, dx, dy in NODE_TABLE: node = ArpanetNode(name) node.setRoutingTable(RoutingTable(name)) graph.addNode(node) gw.add(node, x, y) label = GLabel(name) label.setFont(LABEL_FONT) gw.add(label, x + dx, y + dy) graph.connect("BBN", "CMU") graph.connect("BBN", "MIT") graph.connect("BBN", "UTAH") graph.connect("CMU", "NRL") graph.connect("CMU", "UTAH") graph.connect("HARV", "MIT") graph.connect("HARV", "NRL") graph.connect("MIT", "HARV") graph.connect("NRL", "RAND") graph.connect("RAND", "UCLA") graph.connect("SRI", "STAN") graph.connect("SRI", "UTAH") graph.connect("STAN", "UCLA") return graph
def estimate_pi(tries): """ Uses random dart throws at a circular target to approximate the value of pi. As the number of throws gets large, pi should be approximately 4 times the fraction of throws that hit the circle. """ def take_shot(): """ Simulates a random "throw" toward the target, and draws a circle of the correct color depending on if the throw struck the target or not. """ x = random.random() * size y = random.random() * size is_hit = (x - radius)**2 + (y - radius)**2 < radius if is_hit: color = "red" n_hits += 1 else: color = "black" gw.add(create_filled_circle(x, y, 1, color)) size = 500 radius = size / 2 # Creates the window and adds the circular target gw = GWindow(size, size) gw.add(create_filled_circle(radius, radius, radius, "blue")) # Simulate tries number of throws num_hits = 0 for i in range(tries): take_shot() # Compute pi pi = num_hits / tries * 4 # Display the rounded value of pi centered pretty in window lab = GLabel(str(round(pi, 2))) lab.set_font("bold 100px 'serif'") lab.set_color("white") x = size / 2 - lab.get_width() / 2 y = size / 2 - lab.get_ascent() / 2 gw.add(lab, x, y)
def __init__(self, letter): ''' The constructor initalizes the key for a given letter. ''' GCompound.__init__(self) button = createFilledCircle(0, 0, KEY_RADIUS, fill=KEY_BGCOLOR, border=KEY_BORDER_COLOR) button.setLineWidth(KEY_BORDER) key = GLabel(letter) key.setFont(KEY_FONT) self.key = key key.setColor(KEY_UP_COLOR) self.add(button) self.add(key, -key.getWidth() / 2, KEY_LABEL_DY)
def __init__(self): ''' The constructor creates a return button on the enigma. ''' GCompound.__init__(self) button = createFilledRect(-RETURN_WIDTH / 2, -RETURN_HEIGHT / 2, RETURN_WIDTH, RETURN_HEIGHT, fill=RETURN_BGCOLOR, border=RETURN_BORDER_COLOR) button.setLineWidth(RETURN_BORDER) label = GLabel('RETURN') label.setFont(RETURN_FONT) label.setColor(RETURN_COLOR) self.add(button) self.add(label, -label.getWidth() / 2, RETURN_LABEL_DY)
def step(): nonlocal snowman, dx, dy, full_word if snowman.getX() > (GWINDOW_WIDTH - BASE_SIZE) or snowman.getX() < BASE_SIZE: dx *= -1 gw.remove( gw.getElementAt(GWINDOW_WIDTH / 2, GWINDOW_HEIGHT - WORD_BASE)) word_display = GLabel(full_word) word_display.setFont(WORD_FONT) word_display.setColor(INCORRECT_COLOR) gw.add(word_display, (GWINDOW_WIDTH - word_display.getWidth()) / 2, GWINDOW_HEIGHT - WORD_BASE) elif snowman.getY() < (GWINDOW_HEIGHT - BASE_SIZE - BODY_SIZE - HEAD_SIZE) or snowman.getY() > SNOWMAN_BASE: dy *= -1 snowman.move(dx, dy)
def __init__(self, letter): ''' The constructor creates a lamp for a given letter. ''' GCompound.__init__(self) self.letter = letter lamp = createFilledCircle(0, 0, LAMP_RADIUS, fill=LAMP_BGCOLOR, border=LAMP_BORDER_COLOR) lamp = GLabel(letter) lamp.setFont(LAMP_FONT) self.lamp = lamp self.state = False lamp.setColor(LAMP_OFF_COLOR) self.add(lamp) self.add(lamp, -lamp.getWidth() / 2, LAMP_LABEL_DY)
def __init__(self, permuation): ''' The constructor initalizes the rotor in the base setting. ''' GCompound.__init__(self) self.permuation = permuation self.inversion = invertKey(permuation) self.offset = 0 rotor = createFilledRect(-ROTOR_WIDTH / 2, -ROTOR_HEIGHT / 2, ROTOR_WIDTH, ROTOR_HEIGHT, fill=ROTOR_BGCOLOR) setting = GLabel(ALPHABET[self.offset]) setting.setColor(ROTOR_COLOR) setting.setFont(ROTOR_FONT) self.setting = setting self.add(rotor) self.add(setting, -setting.getWidth() / 2, ROTOR_LABEL_DY)
def __init__(self, rotor): ''' The constructor initalizes the selector in the standard setting in the three rotor model. ''' GCompound.__init__(self) self.offset = rotor self.rotor = rotor button = createFilledRect(-SELECTOR_WIDTH / 2, -SELECTOR_HEIGHT / 2, SELECTOR_WIDTH, SELECTOR_HEIGHT, fill=SELECTOR_BGCOLOR, border=SELECTOR_COLOR) button.setLineWidth(3) setting = GLabel(str(self.offset)) setting.setFont(SELECTOR_FONT) setting.setColor(SELECTOR_COLOR) self.setting = setting self.add(button) self.add(setting, -setting.getWidth() / 2, SELECTOR_LABEL_DY)