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 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 endGame(): global N_BALLS, count, timer, endmsg, againmsg, clicks N_BALLS -= 1 if count == (N_ROWS * N_COLS): # If all bricks have been removed timer.stop() gw.remove(ball) endmsg = GLabel("You Won!") # Create end message, if won endmsg.setFont("36pt 'Arial'") x = (GWINDOW_WIDTH - endmsg.getWidth()) / 2 gw.add(endmsg, x, GWINDOW_HEIGHT / 2) elif N_BALLS > 0: #gw.remove(paddle) clicks = 0 endmsg = GLabel("You Lost :(") # Create end message, if lost endmsg.setFont("30pt 'Arial'") x = (GWINDOW_WIDTH - endmsg.getWidth()) / 2 gw.add(endmsg, x, GWINDOW_HEIGHT * .6) againmsg = GLabel("Click to Play Again") # Create end message, if lost againmsg.setFont("30pt 'Arial'") x = (GWINDOW_WIDTH - againmsg.getWidth()) / 2 gw.add(againmsg, x, GWINDOW_HEIGHT * .6 + spacing) count = 0 Breakout() elif N_BALLS == 0: timer.stop() endmsg = GLabel("You Lost :(") # Create end message, if lost endmsg.setFont("30pt 'Arial'") x = (GWINDOW_WIDTH - endmsg.getWidth()) / 2 gw.add(endmsg, x, GWINDOW_HEIGHT / 2)
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 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 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
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 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 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)
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): #This function takes the parameter as string and outputs the beehive angle = 30 figure = GCompound( ) #This will store the whole beehive and is the output of the function in the end figureinside = GCompound( ) #This will store the intermediate values to be put into figure 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.5 * labelinside.getWidth(), HEX_LABEL_DY ) #Using -0.5 times label width tends to center the label more 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) labelinside.setLocation(-0.5 * labelinside.getWidth(), HEX_LABEL_DY) figureinside.add(labelinside) figureinside.movePolar(HEX_SEP, angle) angle += 60 figure.add(figureinside) return figure
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)
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 __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 __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 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 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 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 __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)
class EnigmaRotor(GCompound): 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 clickAction(self, enigma): # advance on click action of rotors self.advance() def advance(self): # define advance self.offset += 1 if self.offset == 26: self.offset = 0 r = True # tells that the rotor should carry else: r = False # tells that the rotor should not carry self.ch.setLabel(ALPHABET[self.offset]) return r
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
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 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 = []
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
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