def main(): # create the application window win = GraphWin("Dice Roller") win.setCoords(0, 0, 10, 10) win.setBackground("green2") # Draw the interface widgets die1 = DieView(win, Point(3, 7), 2) die2 = DieView(win, Point(7, 7), 2) rollButton = CButton(win, Point(5, 4.5), 1.5, "Roll Dice") rollButton.activate() quitButton = CButton(win, Point(5, 1), 1, "Quit") # Event loop pt = win.getMouse() while not quitButton.clicked(pt): if rollButton.clicked(pt): value1 = randrange(1, 7) die1.setValue(value1) value2 = randrange(1, 7) die2.setValue(value2) quitButton.activate() pt = win.getMouse() # close up shop win.close()
def main(): # Defines a Window win = GraphWin("Child Height Calculator", 600, 400) win.setBackground("darkgreen") # Draw the Instructions instruction = Text(Point(285, 100), "Please enter (in inches): ") instructionA = Text(Point(160, 140), "Mother's Height") instructionB = Text(Point(400, 140), "Father's Height") # Enable Instructions instruction.draw(win) instructionA.draw(win) instructionB.draw(win) # Draw the Entry Boxes motherEntry = Entry(Point(165, 170), 8) fatherEntry = Entry(Point(405, 170), 8) # Enables the Entry Boxes motherEntry.draw(win) fatherEntry.draw(win) # Draws the Buttons maleCalculateButton = Button(win, Point(165, 200), 160, 20, "Male Calculate") maleCalculateButton.activate() femaleCalculateButton = Button(win, Point(405, 200), 160, 20, "Female Calculate") femaleCalculateButton.activate() quitButton = CButton(win, Point(300, 330), 30, "Quit") quitButton.activate() # Draws the Output Box outputBoxA = Text(Point(300, 230), "Child Height (in): ") outputBoxB = Text(Point(300, 260), "Child Height (ft): ") # Enables the Output Box outputBoxA.draw(win) outputBoxB.draw(win) while not quitButton.clicked(win.getMouse()): try: if maleCalculateButton.clicked(win.getMouse()): temp = chooseHeightCalculation("male", int(motherEntry.getText()), int(fatherEntry.getText())) outputBoxA.setText("Child Height (in): " + str(round(temp, 2))) outputBoxB.setText("Child Height (ft): " + str(round(temp / 12, 2))) elif femaleCalculateButton.clicked(win.getMouse()): temp = chooseHeightCalculation("female", int(motherEntry.getText()), int(fatherEntry.getText())) outputBoxA.setText("Child Height (in): " + str(round(temp, 2))) outputBoxB.setText("Child Height (ft): " + str(round(temp / 12, 2))) except: print("Please Enter Numbers into Data before Proceeding")
def main(): # Create the application window win = GraphWin("Estimate Adult Height of Child", 275, 200) win.setCoords(0.0, 0.0, 5.0, 5.0) # Draw the interface widgets/buttons # Use modified CButton class calculateButton = CButton(win, Point(2.5, 2.2), 0.63, "Calculate") calculateButton.activate() quitButton = CButton(win, Point(2.5, 0.5), 0.35, "Quit") # Draw window labels and entry boxes Text(Point(1.8, 4.5), "Enter child's gender:".ljust(25)).draw(win) genderEntry = Entry(Point(4.2, 4.5), 6) genderEntry.setFill("white") genderEntry.draw(win) Text(Point(1.8, 3.8), "Enter mother's height:".ljust(24)).draw(win) motherHeightEntry = Entry(Point(4.2, 3.8), 6) motherHeightEntry.setFill("white") motherHeightEntry.draw(win) Text(Point(1.8, 3.1), "Enter father's height:".ljust(26)).draw(win) fatherHeightEntry = Entry(Point(4.2, 3.1), 6) fatherHeightEntry.setFill("white") fatherHeightEntry.draw(win) # Create output text object adultHeightLabel = Text(Point(2.5, 1.4), "") adultHeightLabel.setStyle("bold") # Event loop pt = win.getMouse() while not quitButton.clicked(pt): # calculate height only when calculate btn is clicked if calculateButton.clicked(pt): # clear output text once calculate btn is clicked adultHeightLabel.undraw() # ---INPUT--- # gender = genderEntry.getText() momHeight = eval(motherHeightEntry.getText()) dadHeight = eval(fatherHeightEntry.getText()) gender = gender[0].lower() # ---PROCESS--- # # same process from hw7project2.py if gender == 'm' or gender == 'b': adultH = ((momHeight * (13 / 12)) + dadHeight) / 2 else: adultH = ((dadHeight * (12 / 13)) + momHeight) / 2 # calculate height from inches to feet and inches feet = round(adultH) / 12 inches = round(adultH) % 12 # ---OUTPUT--- # adultHeightLabel.setText( "\nAdult height: {2:.2f} inches = {0}'{1}\"".format( int(feet), int(inches), adultH)) adultHeightLabel.draw(win) # allow quit btn to be clicked quitButton.activate() # get point/location where mouse was clicked pt = win.getMouse() # close window when Quit button is clicked win.close()
def __init__(self): self.win = GraphWin("Aggravation!", 1044, 864) #coord * 3.6 self.win.setCoords(-20, -20, 270, 220) background = Rectangle(Point(-10, -10), Point(190, 190)) background.setFill("light slate gray") background.draw(self.win) # !! Game title -- Aggravation! (with image?) upper right hand corner? # Set up board with number of players and their colors # Stars for special holes # Title line can be used for instructions self.instruct = Text(Point(90, 205), "") self.instruct.setSize(20) self.instruct.setStyle("bold") self.instruct.draw(self.win) # Set up the dice needed self.dice = [] self.dice.append(ColorDieView(self.win, Point(215, 156), 25)) self.dice.append(ColorDieView(self.win, Point(245, 156), 25)) # Make the dice gray until players are set up for i in range(len(self.dice)): self.dice[i].setColor("gray") self.bMain = [] self.bMain.append( Button(self.win, Point(230, 60), 50, 15, "Start the game!")) self.bMain.append( Button(self.win, Point(230, 36), 50, 15, "Rules of the game")) self.bMain.append(Button(self.win, Point(230, 12), 50, 15, "Quit")) self.bMain.append( Button(self.win, Point(230, 126), 56, 15, "Roll Dice")) for i in range(3): # Don't activate button "Roll Dice" self.bMain[i].activate() # Set up holes on the board # Starts at upper right hole and goes clockwise around board # 56 holes around the board holeSpecs = [(120, 180), (120, 168), (120, 156), (120, 144), (120, 132), (120, 120), (132, 120), (144, 120), (156, 120), (168, 120), (180, 120), (180, 105), (180, 90), (180, 75), (180, 60), (168, 60), (156, 60), (144, 60), (132, 60), (120, 60), (120, 48), (120, 36), (120, 24), (120, 12), (120, 0), (105, 0), (90, 0), (75, 0), (60, 0), (60, 12), (60, 24), (60, 36), (60, 48), (60, 60), (48, 60), (36, 60), (24, 60), (12, 60), (0, 60), (0, 75), (0, 90), (0, 105), (0, 120), (12, 120), (24, 120), (36, 120), (48, 120), (60, 120), (60, 132), (60, 144), (60, 156), (60, 168), (60, 180), (75, 180), (90, 180), (105, 180)] self.centHole = CButton(self.win, Point(90, 90), 4, "") self.bases = [] self.homes = [] self.holes = [] # Create holes around the board for (cx, cy) in holeSpecs: self.holes.append(CButton(self.win, Point(cx, cy), 4, "")) # Any way to clean this up with for loops or something? ulBaseSpecs = [(36, 144), (24, 156), (12, 168), (0, 180)] self.ulBase = [] # Create base holes on upper left for (cx, cy) in ulBaseSpecs: self.ulBase.append(CButton(self.win, Point(cx, cy), 4, "")) self.bases.append(self.ulBase) ulHomeSpecs = [(90, 168), (90, 156), (90, 144), (90, 132)] self.ulHome = [] # Create home holes for upper left in center screen for (cx, cy) in ulHomeSpecs: self.ulHome.append(CButton(self.win, Point(cx, cy), 4, "")) self.homes.append(self.ulHome) urBaseSpecs = [(144, 144), (156, 156), (168, 168), (180, 180)] self.urBase = [] # Create base holes on upper right for (cx, cy) in urBaseSpecs: self.urBase.append(CButton(self.win, Point(cx, cy), 4, "")) self.bases.append(self.urBase) urHomeSpecs = [(168, 90), (156, 90), (144, 90), (132, 90)] self.urHome = [] # Create home holes for upper right on right side for (cx, cy) in urHomeSpecs: self.urHome.append(CButton(self.win, Point(cx, cy), 4, "")) self.homes.append(self.urHome) lrBaseSpecs = [(144, 36), (156, 24), (168, 12), (180, 0)] self.lrBase = [] for (cx, cy) in lrBaseSpecs: # Create base holes lower right self.lrBase.append(CButton(self.win, Point(cx, cy), 4, "")) self.bases.append(self.lrBase) lrHomeSpecs = [(90, 12), (90, 24), (90, 36), (90, 48)] self.lrHome = [] # Create home holes on bottom of screen for (cx, cy) in lrHomeSpecs: self.lrHome.append(CButton(self.win, Point(cx, cy), 4, "")) self.homes.append(self.lrHome) llBaseSpecs = [(36, 36), (24, 24), (12, 12), (0, 0)] self.llBase = [] # Create base on lower left for (cx, cy) in llBaseSpecs: self.llBase.append(CButton(self.win, Point(cx, cy), 4, "")) self.bases.append(self.llBase) llHomeSpecs = [(12, 90), (24, 90), (36, 90), (48, 90)] self.llHome = [] # Create home on left side of screen for (cx, cy) in llHomeSpecs: self.llHome.append(CButton(self.win, Point(cx, cy), 4, "")) self.homes.append(self.llHome)
def main(): win = GraphWin("Estimate Adult Height", WINWIDTH, WINHEIGHT) win.setBackground("blue") background = Rectangle(Point(10,10), Point(WINWIDTH-10, WINHEIGHT-10)) background.setFill("white") background.draw(win) # Gender text and entry genderEntry = drawGenderPrompt(win) # Mother height entry motherEntry = drawMotherPrompt(win) # Father height entry fatherEntry = drawFatherPrompt(win) # draw text estimateText1, estimateText2 = drawTexts(win) # buttons to compute and quit estimateButton = CButton(win, Point(WINWIDTH/3, WINHEIGHT-WINHEIGHT/4), 75, "Estimate Height") estimateButton.activate() quitButton = CButton(win, Point(WINWIDTH-WINWIDTH/3, WINHEIGHT-WINHEIGHT/4), 75, "Quit") quitButton.activate() # Event loop pt = win.getMouse() while not quitButton.clicked(pt): if estimateButton.clicked(pt): validGenderEntry = False validMotherEntry = False validFatherEntry = False # Validate gender entry gender = genderEntry.getText() if((gender == "male") or (gender == "female")): genderEntry.setTextColor("black") validGenderEntry = True else: genderEntry.setTextColor("red") # Validate mother entry try: motherHeight = float(motherEntry.getText()) motherEntry.setTextColor("black") validMotherEntry = True except: motherEntry.setTextColor("red") # Validate father entry try: fatherHeight = float(fatherEntry.getText()) fatherEntry.setTextColor("black") validFatherEntry = True except: fatherEntry.setTextColor("red") if(not (validGenderEntry and validMotherEntry and validFatherEntry)): estimateText1.setText("Invalid entries") estimateText2.setText("") else: if(gender == "male"): childHeight = ((motherHeight*13/12)+fatherHeight)/2 else: childHeight = ((fatherHeight*12/13)+motherHeight)/2 feet = int(childHeight//12) inches = int(childHeight%12) inchesStr = "The estimated height as an adult is {:.2f} inches".format(childHeight) feetInchesStr = "{} feet {} inches".format(feet, inches) estimateText1.setText(inchesStr) estimateText2.setText(feetInchesStr) pt = win.getMouse() # Click before exit win.close()
def main(): # Draw the window to simulate green field and sky. win = GraphWin("Shoot a Cannon!", 850,850) win.setCoords(0,-50,700,700) win.setBackground("lightblue") ground = Rectangle(Point(0,-100),Point(700,0)) ground.setFill("green") cloud1 = Oval(Point(153,420),Point(390,580)) cloud1.setFill("white") cloud1.setWidth(2) cloud2 = Oval(Point(23,232),Point(230,400)) cloud2.setFill("white") cloud2.setWidth(2) cloud3 = Oval(Point(434,370),Point(610,520)) cloud3.setFill("white") cloud3.setWidth(2) tangle = Text(Point(30,680), " Angle: ") tvel = Text(Point(30,650), "Velocity:") anginp = Entry(Point(70,680),3) velinp = Entry(Point(70,650),3) fbutton = CButton(win,Point(55,590),30,"FIRE!") fbutton.activate() quitbutton = CButton(win,Point(630,660),30,"Quit") quitbutton.activate() ground.draw(win) cloud1.draw(win) cloud2.draw(win) cloud3.draw(win) tangle.draw(win) tvel.draw(win) anginp.draw(win) anginp.setText("0") velinp.setText("0") velinp.draw(win) target = Target(win) pt = win.getMouse() shots = 0 while not quitbutton.clicked(pt): try: if fbutton.clicked(pt): ang = float(anginp.getText()) vel = float(velinp.getText()) shot = Projectile(ang,vel,0) ball = Tracker(win,shot) while shot.getY() >= 0 and target.Hit(shot) == False and shot.getX() < 750: sleep(.025) shot.update(.1) ball.update(win,shot) target.Hit(shot) if target.Hit(shot) == True: shots += 1 wintxt = Text(Point(325,500), "You Hit the Target in %d shot(s)!" % shots) wintxt.setSize(36) wintxt.setTextColor("red") wintxt.draw(win) exit shots += 1 except ValueError: exit pt = win.getMouse()
def makeButtons(win): wink = CButton(win, Point(16, 17), 1, "Wink") meditate = CButton(win, Point(12, 17), 1, "Meditate") smile = CButton(win, Point(8, 17), 1, "Smile") endGame = CButton(win, Point(4, 17), 1, "Quit") wink.activate() meditate.activate() smile.activate() endGame.activate() return wink, meditate, smile, endGame