def redCross(): def createRedCross(w, h): cross = GCompound() rect_1 = GRect(-w / 2, -h / 2, LONG_SIDE, SHORT_SIDE) rect_1.setFilled(True) rect_1.setColor('red') cross.add(rect_1) rect_2 = GRect(-h / 2, -w / 2, SHORT_SIDE, LONG_SIDE) rect_2.setFilled(True) rect_2.setColor('red') cross.add(rect_2) return cross def step(): nonlocal cross, theta cross.movePolar(VELOCITY, theta) def clickAction(e): nonlocal theta if cross.contains(e.getX(), e.getY()): theta = random.uniform(0,360) gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) theta = random.uniform(0,360) cross = createRedCross(LONG_SIDE, SHORT_SIDE) gw.add(cross, GWINDOW_WIDTH / 2, GWINDOW_HEIGHT / 2) gw.addEventListener('click', clickAction) timer = gw.createTimer(step, TIME_STEP) timer.setRepeats(True) timer.start()
def DrawLines(): def mousedownAction(e): nonlocal line line = GLine(e.getX(), e.getY(), e.getX(), e.getY()) gw.add(line) def dragAction(e): line.setEndPoint(e.getX(), e.getY()) gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) line = None gw.addEventListener('mousedown', mousedownAction) gw.addEventListener('drag', dragAction)
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 clickAction(e): gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) x0 = (GWINDOW_WIDTH - N_COL * BOX_SIZE) / 2 y0 = (GWINDOW_HEIGHT - N_ROW * BOX_SIZE) / 2 for row in range(N_ROW): for col in range(N_COL): x = x0 + col * BOX_SIZE y = y0 + row * BOX_SIZE box = GRect(x, y, BOX_SIZE, BOX_SIZE) gw.add(box) gw.addEventListener('click', clickAction)
def sudoku_builder(): def mousedown_action(e): element = gw.getElementAt(e.getX(), e.getY()) element.mousedown(e.getX(), e.getY()) def mouseup_action(e): element = gw.getElementAt(e.getX(), e.getY()) element.mouseup(e.getX(), e.getY()) gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) gw.addEventListener("mousedown", mousedown_action) gw.addEventListener("mouseup", mouseup_action) board = YoudokuBoard() gw.add(board, 0, 0)
def DrawRectangles(): def mousedownAction(e): nonlocal rect, x0, y0 x0 = e.getX() y0 = e.getY() rect = GRect(x0, y0, 0, 0) rect.setFilled(True) gw.add(rect) def dragAction(e): x = min(e.getX(), x0) y = min(e.getY(), y0) rect.setBounds(x, y, abs(e.getX() - x0), abs(e.getY() - y0)) gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) rect = None x0 = None y0 = None gw.addEventListener('mousedown', mousedownAction) gw.addEventListener('drag', dragAction)
def Enigma(): def mousedownAction(e): gobj = gw.getElementAt(e.getX(), e.getY()) if gobj is not None: if getattr(gobj, "mousedownAction", None) is not None: gobj.mousedownAction(enigma) def mouseupAction(e): gobj = gw.getElementAt(e.getX(), e.getY()) if gobj is not None: if getattr(gobj, "mouseupAction", None) is not None: gobj.mouseupAction(enigma) def clickAction(e): gobj = gw.getElementAt(e.getX(), e.getY()) if gobj is not None: if getattr(gobj, "clickAction", None) is not None: gobj.clickAction(enigma) gw = GWindow(ENIGMA_WIDTH, ENIGMA_HEIGHT) enigma = EnigmaMachine(gw) gw.addEventListener("mousedown", mousedownAction) gw.addEventListener("mouseup", mouseupAction) gw.addEventListener("click", clickAction)
def Swirl(): gw = GWindow(GW_WIDTH,GW_HEIGHT) allFlowers=[] theta=0 steps=0 angleNumber=4 diamondAngle=THETA[3] limChanger=0 stepChanger=10 angularOffset=(.5*diamondAngle) aOchanger=0 def setUp():#sets up scene nonlocal allFlowers,theta,diamondAngle,angularOffset lim=GW_WIDTH*2 while(lim>=1): flower=drawFlowerz(lim,theta,diamondAngle) gw.add(flower,MIDX,MIDY) theta+= angularOffset lim= changeLim(lim) allFlowers.append(flower) def changeDiamondAngle():#changes the number of diamonds, or the number of colors nonlocal diamondAngle,angleNumber,angularOffset angleNumber+=1 if angleNumber>len(THETA)-1: angleNumber=0 diamondAngle=THETA[angleNumber] angularOffset=(.5*diamondAngle) def click(e): buttonPressed=gw2.getElementAt(e.getX(),e.getY()) if buttonPressed==changeAngleButton: changeDiamondAngle() if buttonPressed==changeSpaceOffsetButton: changeSpace() if buttonPressed==changeAngularOffsetButton: changeAngularOff() def changeAngularOff():#changes angular offset of each flower with respect to the previous nonlocal diamondAngle,angularOffset,aOchanger aOchanger+=1 if aOchanger>2: aOchanger=0 if aOchanger==0: angularOffset=(.5*diamondAngle) if aOchanger==1: angularOffset=(.75*diamondAngle) if aOchanger==2: angularOffset=(2*diamondAngle) def step(): nonlocal allFlowers,theta,steps,diamondAngle,stepChanger,angularOffset for i in allFlowers: gw.remove(i) allFlowers.clear() lim=GW_WIDTH*2 steps+=stepChanger theta=0+(steps) while(lim>=1): flower=drawFlowerz(lim,theta,diamondAngle) gw.add(flower,MIDX,MIDY) theta+= angularOffset lim= changeLim(lim) allFlowers.append(flower) def changeSpace(): nonlocal limChanger limChanger+=1 def changeLim(lim): #changes space offset between each flower nonlocal limChanger,stepChanger if limChanger>=4: limChanger=0 if limChanger==0: stepChanger=10 return ((.5* lim)/cos((radians(.5*diamondAngle)))) if limChanger==1: stepChanger=30 return lim-60 if limChanger==2: stepChanger=20 return lim-100 if limChanger==3: stepChanger=40 return lim-40 setUp() gw2=GWindow(GW_WIDTH,100) gw.setInterval(step,TIME_STEP) gw2.addEventListener("click",click) space=gw2.getWidth()//4 changeAngleButton=createButton("Change Number of Colors") #changes the number of diamonds changeSpaceOffsetButton=createButton("Change Space Offset")#changes the space between each flower changeAngularOffsetButton=createButton("Change Angular Offset")#changes offset of each flower with respect to the previous gw2.add(changeAngleButton,space,gw2.getHeight()//2) gw2.add(changeSpaceOffsetButton,space*2,gw2.getHeight()//2) gw2.add(changeAngularOffsetButton,space*3,gw2.getHeight()//2)
def game(): gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) game_state = GameState() apples_collected = [] objects_lost = [] background = GImage("background.png", 0, 0) gw.add(background) scoreboard = GRect(GWINDOW_WIDTH - SB_WIDTH, 550, SB_WIDTH, SB_HEIGHT) scoreboard.setFilled(True) scoreboard.setColor("White") gw.add(scoreboard) collected_label = create_centered_label("Apples collected: ", GWINDOW_WIDTH - 160, 590, SB_FONT) gw.add(collected_label) collected_num_label = create_centered_label(str(len(apples_collected)), GWINDOW_WIDTH - 30, 590, SB_FONT) gw.add(collected_num_label) lost_label = create_centered_label("Apples lost: ", GWINDOW_WIDTH - 195, 650, SB_FONT) gw.add(lost_label) lost_num_label = create_centered_label(str(len(objects_lost)), GWINDOW_WIDTH - 30, 650, SB_FONT) gw.add(lost_num_label) c = Character() isaac_newton = c.character gw.add(isaac_newton) # This function adds the apples to the game, according to the timestep provided in the list of constants. Apples # added to a list and removed when they are either collected or they hit the ground. apples = [] def add_apples(): xpos = random.randint(0 + APPLE_WIDTH, GWINDOW_WIDTH - APPLE_WIDTH) f = Falling_Object(xpos) apple = f.apple gw.add(apple) apples.append(apple) # This function adds worms to the window. Worms will appear after some duration of the game (i.e. 5 apples # have been collected). They appear according to the third timestep provided in constants.py. worms = [] def add_worms(): if len(apples_collected) > 5: xpos = random.randint(0 + WORM_WIDTH, GWINDOW_WIDTH - WORM_WIDTH) w = Worm(xpos) worm = w.worm gw.add(worm) worms.append(worm) # This function increases the apples' y velocity every time 3 apples are collected so that the game becomes harder # as the player progresses. def change_yvel(): if len(apples_collected) % 3 == 0 and len(apples_collected) != 0: game_state.apple_yvel += 0.005 return game_state.apple_yvel # This is the most important function. It makes both the apple and the worm objects move. It handles collisions # between isaac_newton and objects, and deals with them accordingly. This function is called every timestep (constants) # If you lose < 3 apples and collect 25 without collecting a worm, you win the game! def update_objects(): collected_num_label.setLabel(len(apples_collected)) for apple in apples: apple_x_now = apple.getX() apple_y_now = apple.getY() + APPLE_HEIGHT isaac_x = isaac_newton.getX() isaac_y = isaac_newton.getY() if isaac_x <= apple_x_now <= ( isaac_x + ISAAC_WIDTH) and isaac_y <= apple_y_now <= ( isaac_y + ISAAC_HEIGHT): gw.remove(apple) apples.remove(apple) apples_collected.append(apple) if apple_y_now >= GWINDOW_HEIGHT: objects_lost.append(apple) lost_num_label.setLabel(len(objects_lost)) gw.remove(apple) apples.remove(apple) if len(objects_lost) >= 3: end_game(game_state) add_loser(gw) if len(apples_collected) == 25: collected_num_label.setLabel(len(apples_collected)) end_game(game_state) add_winner(gw) game_state.apple_yvel = change_yvel() apple.move(game_state.xvel, game_state.apple_yvel) for worm in worms: worm_x_now = worm.getX() worm_y_now = worm.getY() + WORM_HEIGHT isaac_x = isaac_newton.getX() isaac_y = isaac_newton.getY() if isaac_x <= worm_x_now <= ( isaac_x + ISAAC_WIDTH) and isaac_y <= worm_y_now <= ( isaac_y + ISAAC_HEIGHT): gw.remove(worm) worms.remove(worm) end_game(game_state) add_loser(gw) if worm_y_now >= GWINDOW_HEIGHT: gw.remove(worm) worms.remove(worm) worm.move(game_state.xvel, game_state.worm_yvel) # This function handles the key movement for isaac_newton. If the player touches the left arrow, isaac will move left. # This is the same for the right arrow. def key_action(event): if event.key == "<LEFT>": isaac_newton.move(-ISAAC_XVEL, ISAAC_YVEL) elif event.key == "<RIGHT>": isaac_newton.move(ISAAC_XVEL, ISAAC_YVEL) if isaac_newton.getX() >= (GWINDOW_WIDTH - ISAAC_WIDTH): isaac_newton.setLocation(GWINDOW_WIDTH - ISAAC_WIDTH, Character().ypos) gw.addEventListener("key", key_action) if isaac_newton.getX() <= 0: isaac_newton.setLocation(0, Character().ypos) gw.addEventListener("key", key_action) # Adds key event listener for the arrows and starts the round calling the appropriate functions with the right # timesteps. gw.addEventListener("key", key_action) start_round(gw, game_state, update_objects, add_apples, add_worms)
def Breakout(): """ The main program for the Breakout game. """ def mousemoveAction(e): paddle_X = paddle.getX() dx = e.getX() - paddle_X if 0 <= dx + paddle_X <= GWINDOW_WIDTH - PADDLE_WIDTH: paddle.move(dx, 0) elif 0 > dx + paddle_X: paddle.setLocation(0, PADDLE_Y) else: paddle.setLocation(GWINDOW_WIDTH - PADDLE_WIDTH, PADDLE_Y) def AnimatedBall(): def step(): nonlocal vx, vy, ball, bricks_hit, balls_left, x_text, y_text collider = getCollidingObject() if ball.getX() < 0 or ball.getX() > GWINDOW_WIDTH - BALL_SIZE: vx *= -1 elif ball.getY() < 0: vy *= -1 elif ball.getY() > GWINDOW_HEIGHT - BALL_SIZE: timer.stop() gw.remove(ball) balls_left -= 1 if balls_left > 0: ball = GOval((GWINDOW_WIDTH - BALL_SIZE) / 2, (GWINDOW_HEIGHT - BALL_SIZE) / 2, BALL_SIZE, BALL_SIZE) ball.setFilled(True) gw.add(ball) gw.add(instruct) else: msg = GLabel('You Lose.') msg.setColor('red') msg.setFont('bold 36px sans-serif') x = (GWINDOW_WIDTH - msg.getWidth()) / 2 y = (GWINDOW_HEIGHT - msg.getHeight()) / 2 gw.add(msg, x, y) if collider == paddle: vy *= -1 elif not (collider == paddle or collider == gw.getElementAt( x_text, y_text)) and collider is not None: vy *= -1 gw.remove(collider) bricks_hit += 1 if bricks_hit == N_COLS * N_ROWS: timer.stop() msg = GLabel('You Win!') msg.setColor('green') msg.setFont('bold 36px sans-serif') x = (GWINDOW_WIDTH - msg.getWidth()) / 2 y = (GWINDOW_HEIGHT - msg.getHeight()) / 2 gw.add(msg, x, y) ball.move(vx, vy) gw.remove(gw.getElementAt(x_text, y_text)) lives = GLabel('Lives: ' + str(balls_left)) gw.add(lives, x_text, y_text) vx = random.choice([-1, 1]) * random.uniform(MIN_X_VELOCITY, MAX_X_VELOCITY) vy = INITIAL_Y_VELOCITY x_text = 20 y_text = GWINDOW_HEIGHT - 10 timer = gw.createTimer(step, TIME_STEP) timer.setRepeats(True) timer.start() def clickAction(e): gw.remove(instruct) AnimatedBall() def getCollidingObject(): loc = gw.getElementAt(ball.getX(), ball.getY()) if loc is not None: return loc else: loc = gw.getElementAt(ball.getX() + BALL_SIZE, ball.getY()) if loc is not None: return loc else: loc = gw.getElementAt(ball.getX(), ball.getY() + BALL_SIZE) if loc is not None: return loc else: loc = gw.getElementAt(ball.getX() + BALL_SIZE, ball.getY() + BALL_SIZE) return loc random.seed() gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) colors = [ 'red', 'red', 'orange', 'orange', 'green', 'green', 'cyan', 'cyan', 'blue', 'blue' ] for row in range(N_ROWS): for col in range(N_COLS): rect = GRect( ((GWINDOW_WIDTH - ((N_COLS * (BRICK_WIDTH + BRICK_SEP)) - BRICK_SEP)) / 2) + (row * (BRICK_WIDTH + BRICK_SEP)), (TOP_FRACTION * GWINDOW_HEIGHT) + (col * (BRICK_HEIGHT + BRICK_SEP)), BRICK_WIDTH, BRICK_HEIGHT) rect.setFilled(True) rect.setColor(colors[col]) gw.add(rect) paddle = GRect((GWINDOW_WIDTH - PADDLE_WIDTH) / 2, PADDLE_Y, PADDLE_WIDTH, PADDLE_HEIGHT) paddle.setFilled(True) gw.add(paddle) gw.addEventListener('mousemove', mousemoveAction) ball = GOval((GWINDOW_WIDTH - BALL_SIZE) / 2, (GWINDOW_HEIGHT - BALL_SIZE) / 2, BALL_SIZE, BALL_SIZE) ball.setFilled(True) gw.add(ball) gw.addEventListener('click', clickAction) instruct = GLabel('Click to Start!') instruct.setFont('bold 24px sans-serif') x_inst = (GWINDOW_WIDTH - instruct.getWidth()) / 2 y_inst = ((GWINDOW_HEIGHT - instruct.getHeight()) / 2) + (3 * BALL_SIZE) gw.add(instruct, x_inst, y_inst) balls_left = N_BALLS bricks_hit = 0
def SnowmanGame(): def clickAction(e): """ Checks if there is a letter where was clicked and identifies the letter. Counts incorrect guesses and implements game over and resets the screen. """ nonlocal incorrect_guesses, snowman, mystery_word, full_word, word_display, game_over, letters_chosen, timer if e.getY() > GWINDOW_HEIGHT - WORD_BASE and not game_over: pick = gw.getElementAt(e.getX(), e.getY()) if pick is not None: letter = pick.getLabel() if letter in letters_chosen: gw.remove( gw.getElementAt(GWINDOW_WIDTH / 2, GWINDOW_HEIGHT - MESSAGE_BASE)) message = GLabel('You already picked that letter!') message.setFont(MESSAGE_FONT) gw.add(message, (GWINDOW_WIDTH - message.getWidth()) / 2, GWINDOW_HEIGHT - MESSAGE_BASE) else: letters_chosen.append(letter) gw.remove( gw.getElementAt(GWINDOW_WIDTH / 2, GWINDOW_HEIGHT - MESSAGE_BASE)) if letterFound(letter): pick.setColor(CORRECT_COLOR) else: pick.setColor(INCORRECT_COLOR) incorrect_guesses += 1 addSnowmanPart(snowman, incorrect_guesses) else: gw.remove( gw.getElementAt(GWINDOW_WIDTH / 2, GWINDOW_HEIGHT - MESSAGE_BASE)) message = GLabel('Click a letter!') message.setFont(MESSAGE_FONT) gw.add(message, (GWINDOW_WIDTH - message.getWidth()) / 2, GWINDOW_HEIGHT - MESSAGE_BASE) if incorrect_guesses == 8: game_over = True gw.remove( gw.getElementAt(GWINDOW_WIDTH / 2, GWINDOW_HEIGHT - MESSAGE_BASE)) message = GLabel('YOU LOSE!') message.setFont(MESSAGE_FONT) message.setColor(INCORRECT_COLOR) gw.add(message, (GWINDOW_WIDTH - message.getWidth()) / 2, GWINDOW_HEIGHT - MESSAGE_BASE) animateSnowman() elif not game_over: gw.remove( gw.getElementAt(GWINDOW_WIDTH / 2, GWINDOW_HEIGHT - MESSAGE_BASE)) message = GLabel('Click a letter!') message.setFont(MESSAGE_FONT) gw.add(message, (GWINDOW_WIDTH - message.getWidth()) / 2, GWINDOW_HEIGHT - MESSAGE_BASE) elif game_over: timer.stop() gw.clear() full_word = random.choice(SNOWMAN_WORDS) mystery_word = '-' * len(full_word) word_display = GLabel(mystery_word) word_display.setFont(WORD_FONT) gw.add(word_display, (GWINDOW_WIDTH - word_display.getWidth()) / 2, GWINDOW_HEIGHT - WORD_BASE) alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' for i in range(len(alphabet)): letter = GLabel(alphabet[i]) letter.setFont(LETTER_FONT) x = ((GWINDOW_WIDTH - len(alphabet) * letter.getWidth()) / (len(alphabet) + 1)) * (1 + i) + i * letter.getWidth() gw.add(letter, x, GWINDOW_HEIGHT - LETTER_BASE) incorrect_guesses = 0 snowman = createEmptySnowman(gw) game_over = False letters_chosen = [] def letterFound(ch): """ Checks if the letter clicked is in the word and replaces dashes with the letter everywhere it appears """ nonlocal full_word, mystery_word, word_display, game_over s = 0 if full_word.find(ch) != -1: while s <= len(full_word): if full_word.find(ch, s) != -1: letter = full_word.find(ch, s) mystery_word = mystery_word[:letter] + ch + mystery_word[ letter + 1:] s += letter + 1 else: s = len(full_word) + 1 gw.remove(word_display) word_display = GLabel(mystery_word) word_display.setFont(WORD_FONT) gw.add(word_display, (GWINDOW_WIDTH - word_display.getWidth()) / 2, GWINDOW_HEIGHT - WORD_BASE) if mystery_word == full_word: message = gw.getElementAt(GWINDOW_WIDTH / 2, GWINDOW_HEIGHT - MESSAGE_BASE) gw.remove(message) message = GLabel('YOU WIN!') message.setFont(MESSAGE_FONT) message.setColor(CORRECT_COLOR) gw.add(message, (GWINDOW_WIDTH - message.getWidth()) / 2, GWINDOW_HEIGHT - MESSAGE_BASE) game_over = True animateMelting() return True def animateSnowman(): """ Moves the snowman menacingly if the game is lost. """ nonlocal timer 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) x_1 = .5 * EYE_SEP + (1 + .1) * EYE_SIZE y_1 = -BASE_SIZE - BODY_SIZE - HEAD_SIZE + .17 * HEAD_SIZE x_2 = .5 * EYE_SEP y_2 = -BASE_SIZE - BODY_SIZE - HEAD_SIZE + .255 * HEAD_SIZE brow_1 = GLine(-x_1, y_1, -x_2, y_2) brow_2 = GLine(x_1, y_1, x_2, y_2) snowman.add(brow_1) snowman.add(brow_2) TIME_STEP = 70 dx = (GWINDOW_WIDTH - BASE_SIZE) / TIME_STEP dy = (GWINDOW_HEIGHT - BASE_SIZE - BODY_SIZE - HEAD_SIZE) / (TIME_STEP) timer = gw.createTimer(step, TIME_STEP) timer.setRepeats(True) timer.start() def animateMelting(): """ Moves the snowman off-screen if the game is won. """ nonlocal timer def step(): nonlocal snowman, dy snowman.move(0, dy) TIME_STEP = 70 dy = (GWINDOW_HEIGHT + BASE_SIZE + BODY_SIZE + HEAD_SIZE) / TIME_STEP timer = gw.createTimer(step, TIME_STEP) timer.setRepeats(True) timer.start() random.seed() gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) full_word = random.choice(SNOWMAN_WORDS) mystery_word = '-' * len(full_word) word_display = GLabel(mystery_word) word_display.setFont(WORD_FONT) gw.add(word_display, (GWINDOW_WIDTH - word_display.getWidth()) / 2, GWINDOW_HEIGHT - WORD_BASE) alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' for i in range(len(alphabet)): letter = GLabel(alphabet[i]) letter.setFont(LETTER_FONT) x = ((GWINDOW_WIDTH - len(alphabet) * letter.getWidth()) / (len(alphabet) + 1)) * (1 + i) + i * letter.getWidth() gw.add(letter, x, GWINDOW_HEIGHT - LETTER_BASE) gw.addEventListener('click', clickAction) incorrect_guesses = 0 snowman = createEmptySnowman(gw) game_over = False letters_chosen = [] timer = None
def Enigma(): rotor = [] # create blank list of rotors n = int( input( "Enter the first rotor you'd like to use (0 - 4). (Enter 0 for default). " )) rotor.append(ROTOR_PERMUTATIONS[n]) # add first rotor n = int( input( "Enter the second rotor you'd like to use (0 - 4). (Enter 1 for default). " )) rotor.append(ROTOR_PERMUTATIONS[n]) # add second rotor n = int( input( "Enter the third rotor you'd like to use (0 - 4). (Enter 2 for default). " )) rotor.append(ROTOR_PERMUTATIONS[n]) # add thrid rotor def mousedownAction(e): gobj = gw.getElementAt(e.getX(), e.getY()) if gobj is not None: if getattr(gobj, "mousedownAction", None) is not None: gobj.mousedownAction(enigma) def mouseupAction(e): gobj = gw.getElementAt(e.getX(), e.getY()) if gobj is not None: if getattr(gobj, "mouseupAction", None) is not None: gobj.mouseupAction(enigma) def clickAction(e): gobj = gw.getElementAt(e.getX(), e.getY()) if gobj is not None: if getattr(gobj, "clickAction", None) is not None: gobj.clickAction(enigma) def addKeys(enigma): # create keys for i in range(26): newKey = EnigmaKey(ALPHABET[i]) x, y = KEY_LOCATIONS[i] enigma.keys.append(newKey) gw.add(newKey, x, y) def addLamps(enigma): # create lamps for i in range(26): newLamp = EnigmaLamp(ALPHABET[i]) x, y = LAMP_LOCATIONS[i] enigma.lamps.append(newLamp) gw.add(newLamp, x, y) def addRotors(enigma): # create rotors for i in range(3): ROTOR_INVERSE = invertKey(rotor[i]) newRotor = EnigmaRotor(ALPHABET[0], rotor[i], ROTOR_INVERSE) x, y = ROTOR_LOCATIONS[i] enigma.rotors.append(newRotor) gw.add(newRotor, x, y) gw = GWindow(ENIGMA_WIDTH, ENIGMA_HEIGHT) enigma = EnigmaMachine(gw) gw.addEventListener("mousedown", mousedownAction) gw.addEventListener("mouseup", mouseupAction) gw.addEventListener("click", clickAction) addKeys(enigma) # add keys addLamps(enigma) # add lamps addRotors(enigma) # add rotors