''' Graphics functions for the Hog Heaven project No functions in this file should change. If you need to change these functions for some extra credit reason, simply create new versions of these functions in your file, and modify that one. This file should not be modified and will not be submitted with your code. See the specific documentation for each function. ''' from graphics import * import random WINDOW_SIZE = 600 # Size of the window width and height. # Note: I may change this value in my version of the code! # Your code should still work as long as it's between 300 and 1200. def random_integer(bound): '''Produces a random integer in the range 0 -- (bound-1). Returns: int: the integer. ''' return random.randrange(0, bound) def roll(mode=1): '''Generates the outcome of two Pig Dice: 'A' = Pig lands on its side (with no dot) 'B' = Pig lands on its side (with dot) 'C' = Razorback (pig lands on its back) 'D' = Trotter (pig is standing upright) 'E' = Snouter (pig is leaning on its snout) 'F' = Leaning jowler (pig is resting on its snout and ear) Returns: list: a list of dice faces ''' faces = ['A', 'B', 'C', 'D', 'E', 'F'] rolls1 = [['A', 'A'], ['A', 'B'], ['A', 'C'], ['A', 'D'], ['A', 'E'], ['A', 'F'], ['B', 'A'], ['B', 'B'], ['B', 'C'], ['B', 'D'], ['B', 'E'], ['B', 'F'], ['C', 'A'], ['C', 'B'], ['F', 'F'] ] rolls2 = [ ['C', 'C'], ['C', 'D'], ['C', 'E'], ['C', 'F'], ['D', 'A'], ['D', 'B'], ['D', 'C'], ['D', 'D'] ] rolls3 = [ ['D', 'E'], ['D', 'F'], ['E', 'A'], ['E', 'B'], ['E', 'C'], ['E', 'D'], ['E', 'E'] ] rolls4 = [ ['E', 'F'], ['F', 'A'], ['F', 'B'], ['F', 'C'], ['F', 'D'], ['F', 'E'] ] if mode == 0: i = random_integer(len(faces)) j = random_integer(len(faces)) return [faces[i], faces[j]] else: try: roll.counter += 1 except AttributeError: roll.counter = 0 if mode == 1: return rolls1[roll.counter] elif mode == 2: return rolls2[roll.counter] elif mode == 3: return rolls3[roll.counter] else: return rolls4[roll.counter] def draw_main_window(size): '''Draws and returns a window Args: size (int): the window size. Returns: GraphWin: a window ''' mainwin = GraphWin('Hog Heaven', size, size) mainwin.setBackground('LightBlue') return mainwin def draw_button(win, txtCenter, txt): '''Draw a button with text, and return the button (as a Rectangle). Args: win (GraphWin): the window to draw the button. txtCenter (Point): coordinates of button center. txt (str): text to draw in button. Returns: Rectangle: the object that defines the button ''' button = Rectangle(Point(txtCenter.getX() - len(txt) * 10, txtCenter.getY() - 15), Point(txtCenter.getX() + len(txt) * 10, txtCenter.getY() + 15)) button.setFill('DarkGray') button.draw(win) buttontxt = Text(txtCenter, txt) buttontxt.setSize(20) buttontxt.draw(win) return button def wait_for_button(win, button): '''Waits for the user to type some text and click the button Args: win (GraphWin): the graphics window button (Rectangle): the area where a click may occur Returns: None ''' # Infinite loop - will break out of it by # returning if the user clicks the button button_ul = button.getP1() # button upper left button_lr = button.getP2() # button upper right while True: mousept = win.getMouse() if button_ul.getX() < mousept.getX() < button_lr.getX() and \ button_ul.getY() < mousept.getY() < button_lr.getY(): return