def main(): #draw main window win = GraphWin('Dice Roller', 500, 500) win.setCoords(0, 0, 10, 10) win.setBackground('green2') #draw die and buttons die1 = DieView(win, Point(3, 6.5), 2) die2 = DieView(win, Point(7, 6.5), 2) rollButton = Button(win, Point(3, 2.5), 4, 1, 'Roll Dice') rollButton.activate() quitButton = Button(win, Point(8, 2.5), 2, 1, 'Quit') #event loop pt = win.getMouse() while not quitButton.clicked(pt): if rollButton.clicked(pt): a, b = randrange(1, 7), randrange(1, 7) die1.setValue(a) die2.setValue(b) quitButton.activate() pt = win.getMouse() #clean up win.close()
def main(): # create the application window win = GraphWin("Dice Roller") win.setCoords(0,0,10,10) win.setBackground("gray") # draw the interface widgets die1 = DieView(win, Point(3,7), 2) die2 = DieView(win, Point(7,7), 2) rollButton = Button(win, Point(5,4.5), 6, 1, "Roll Dice") rollButton.activate() quitButton = Button(win, Point(5,1), 2, 1, "Quit") # event loop pt = win.getMouse() while not quitButton.clicked(pt): if rollButton.clicked(pt): # re-rolling value and pip color of die1 value1 = randrange(1, 7) color = color_rgb(randrange(0,256), randrange(0,256), randrange(0,256)) die1.setValue(value1) die1.setColor(color) # re-rolling value and pip color of die2 value2 = randrange(1, 7) color = color_rgb(randrange(0,256), randrange(0,256), randrange(0,256)) die2.setValue(value2) die2.setColor(color) quitButton.activate() pt = win.getMouse() # close up shop win.close()
def main(): win = GraphWin("House", 600, 600) win.setCoords(0,0,600,600) Text(Point(300,10),"5 Click House").draw(win) # Draw the main house p1 = win.getMouse() p2 = win.getMouse() Rectangle(p1, p2).draw(win) # Draw the door con = (abs(p1.x) - (p2.x)) / 5 p3 = win.getMouse() d1 = Point(p3.x + con / 2, p3.y) d2 = Point(p3.x - con / 2, p1.y) Rectangle(d1, d2).draw(win) # Draw the window p4 = win.getMouse() w1 = Point(p4.x - con / 4, p4.y + con / 4) w2 = Point(p4.x + con / 4, p4.y - con / 4) Rectangle(w1, w2).draw(win) p5 = win.getMouse() Polygon(p2, Point(p1.x, p2.y), p5).draw(win) Text(Point(300,590),"I hoped you liked my house!!").draw(win) time.sleep(10) # sleep the thread for 10 seconds
def draw_archery_target(): """ 3. Write a function, draw_archery_target(), that draws a coloured target consisting of concentric circles of yellow (innermost), red and blue. The sizes of the circles should be in correct proportion i.e. the red circle should have a radius twice that of the yellow circle, and the blue circle should have a radius three times that of the yellow circle. Hint: objects drawn later will appear on top of objects drawn earlier. """ win = GraphWin("Target") win.setCoords(0, 0, 1, 1) centre = Point(0.5, 0.5) yellow_circle = Circle(centre, 0.1) yellow_circle.setFill("yellow") red_circle = Circle(centre, yellow_circle.getRadius() * 2) red_circle.setFill("red") blue_circle = Circle(centre, yellow_circle.getRadius() * 3) blue_circle.setFill("blue") blue_circle.draw(win) red_circle.draw(win) yellow_circle.draw(win) await_user_input(win)
def main(): # create the application window win = GraphWin("Dice Roller", ) win.setCoords(0, 0, 10, 10) win.setBackground("green2") # draw the interface widgets b1 = Button(win, Point(4, 2), 3, 2, "Roll") b2 = Button(win, Point(8, 2), 3, 2, "Quit") d1 = DieView(win, Point(3, 6), 3) d2 = DieView(win, Point(7, 6), 3) # event loop while True: if b1.clicked(win.getMouse()): d1.setValue(MSdie().setValue()) d2.setValue(MSdie().setValue()) elif b2.clicked(win.getMouse()): return False # close the window win.close()
def main(): #create app window win = GraphWin("Dice Roller") win.setCoords(0, 0, 10, 10) win.setBackground('green2') #draw interface widgets die1 = DieView(win, Point(3, 7), 2) die2 = DieView(win, Point(7, 7), 2) rollButton = Button(win, Point(5, 4.5), 6, 1, 'Roll Dice') rollButton.activate() quitButton = Button(win, Point(5, 1), 2, 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 shope win.close()
def main() : # Introduction print ( " This program plots the growth of a 10-year investment . " ) # Get principal and interest rate principal = float(input ("Enter the initial principal : ")) apr = float(input("Enter the annualized interest rate : ")) # Create a graphics window with labels on left edge win = GraphWin ("Investment Growth Chart ", 320 , 240) win.setBackground ("white") win.setCoords (-1.75, -200 , 11.5, 10400) Text(Point (- 1, 0) , ' 0.0K').draw (win) Text(Point (- 1, 2500) , '2.5K').draw (win) Text(Point (- 1, 5000) , ' 5.0K').draw (win) Text(Point (- 1, 7500) , ' 7.5k').draw (win) Text(Point (- 1, 10000) , '10.0K').draw (win) # Draw bar f or init ial princ ipal bar = Rectangle (Point (0, 0) , Point (1, principal)) bar.setFill ("green") bar.setWidth (2) bar.draw (win) # Draw a bar for each subsequent year for year in range (1, 11) : principal = principal * ( 1 + apr) bar = Rectangle (Point (year, 0), Point (year+ 1 , principal )) bar.setFill ("green") bar.setWidth (2) bar.draw(win) input(" Press <Enter> to quit.") win.close ()
def main(): # Create animation window win = GraphWin("Projectile Animation", 640, 480, autoflush=False) win.setCoords(-10, -10, 210, 155) # Draw baseline Line(Point(-10, 0), Point(210, 0)).draw(win) # Draw labeled ticks every 50 meters for x in range(0, 210, 50): Text(Point(x, -5), str(x)).draw(win) Line(Point(x, 0), Point(x, 2)).draw(win) # Event loop, each time through fires a single shot angle, vel, height = 45.0, 40.0, 2.0 while True: # Interact with the user inputwin = InputDialog(angle, vel, height) choice = inputwin.interact() inputwin.close() if choice == "Quit": # Loop exit break # Create a shot and track until it hits ground or leaves window angle, vel, height = inputwin.getValues() shot = ShotTracker(win, angle, vel, height) while 0 <= shot.getY() and -10 < shot.getX() <= 210: shot.update(1/50) update(50) win.close()
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 draw_grid(size): squares = size - 1 win = GraphWin("Graphical traced walk", 50 * size, 50 * size) win.setCoords(0, size, size, 0) border_rectangle = Rectangle(Point(0.5, 0.5), Point(size - 0.5, size - 0.5)).draw(win) border_rectangle.setFill("gray") border_rectangle.setWidth(2) centre_square = Rectangle( Point(size / 2 - 0.5, size / 2 - 0.5), Point(size / 2 + 0.5, size / 2 + 0.5), ).draw(win) centre_square.setFill("cyan") centre_square.setOutline("") person = Circle(Point(size / 2, size / 2), 0.25).draw(win) person.setFill("red") square_texts = [[""] * squares for _ in range(squares)] for i in range(squares): for j in range(squares): # grid lines Line(Point(1.5 + j, 0.5), Point(1.5 + j, size - 0.5)).draw(win) Line(Point(0.5, 1.5 + j), Point(size - 0.5, 1.5 + j)).draw(win) # text within each square square_text = Text(Point(1 + j, 1 + i), "").draw(win) square_texts[i][j] = square_text return win, person, square_texts
def main(): win = GraphWin('Dice Roller') win.setCoords(0, 0, 10, 10) win.setBackground('green2') die1 = MultiSidedDie(6) die2 = MultiSidedDie(6) dieView1 = DieView(win, Point(3, 7), 2) dieView2 = DieView(win, Point(7, 7), 2) rollButton = Button(win, Point(5, 4.5), 6, 1, 'Roll Dice') rollButton.activate() quitButton = Button(win, Point(5, 1), 2, 1, 'Quit') point = win.getMouse() while not quitButton.clicked(point): if rollButton.clicked(point): die1.roll() dieView1.drawValue(die1.getValue()) die2.roll() dieView2.drawValue(die2.getValue()) quitButton.activate() point = win.getMouse() win.close()
def main(): print("This program plots the growth of a 10-year investment!") principal = eval(input("Enter the initial principal: ")) apr = eval(input("Enter the annualized interest rate: ")) win = GraphWin("Investment Growth Chart", 320, 240) win.setBackground("white") win.setCoords(-1.75, -200, 11.5, 10400) Text(Point(-1, 0), "0.0K").draw(win) Text(Point(-1, 2500), "2.5K").draw(win) Text(Point(-1, 5000), "5.0K").draw(win) Text(Point(-1, 7500), "7.5K").draw(win) Text(Point(-1, 10000), "10.0K").draw(win) bar = Rectangle(Point(0, 0), Point(1, principal)) bar.setFill("green") bar.setWidth(2) bar.draw(win) for year in range(1, 11): principal = principal * (1 + apr) bar = Rectangle(Point(year, 0), Point(year + 1, principal)) bar.setFill("green") bar.setWidth(2) bar.draw(win) input("Press <Enter> to quit.") win.close()
def draw_house(door_colour, lights_on, n, size): win = GraphWin("House", 100 * size, 100 * size) win.setCoords(0, 0, 1, 1) roof = Polygon(Point(0.01, 0.7), Point(0.22, 0.992), Point(0.78, 0.992), Point(0.992, 0.7)) roof.setFill("pink") roof.draw(win) # draw wall and door draw_rectangle(win, Point(0.008, 0.008), Point(0.992, 0.7), "brown") draw_rectangle(win, Point(0.15, 0.008), Point(0.4, 0.45), door_colour) # draw door number door_n = Text(Point(0.275, 0.35), n) door_n.setSize(4 + 2 * size) if door_colour == "black": door_n.setFill("white") door_n.draw(win) # draw window if lights_on: window_colour = "yellow" else: window_colour = "black" draw_rectangle(win, Point(0.55, 0.15), Point(0.85, 0.45), window_colour)
def main(): # create application window win = GraphWin("Three Door Monte", 600, 400) win.setCoords(0, 0, 15, 10) win.setBackground("darkgrey") # create game doorMonte = Game(win) # create replay and quit button quitbutton = Button(win, Point(11.25, 1), 3, 1.5, "Quit") quitbutton.activate() replay = Button(win, Point(3.75, 1), 3, 1.5, "Replay") # event loop click = win.getMouse() while not quitbutton.clicked(click): if doorMonte.doorPicked(click): replay.activate() elif replay.clicked(click): doorMonte.reset() click = win.getMouse() win.close()
def main(): print("This program plots the growth of a 10-year investment.") print() principal = eval(input("Enter the initial principal: ")) apr = eval(input("Enter the annualized interest rate: ")) win = GraphWin("Investment Growth Chart", 320, 240) win.setBackground("white") win.setCoords(-1.75, -200, 11.5, 10400) Text(Point(-1, 0), "0.0K").draw(win) Text(Point(-1, 2500), "2.5K").draw(win) Text(Point(-1, 5000), "5.0K").draw(win) Text(Point(-1, 7500), "7.5K").draw(win) Text(Point(-1, 10000), "10.0K").draw(win) draw_bar(win, 0, principal) for year in range(1, 11): principal = principal * (1 + apr) draw_bar(win, year, principal) input("Press <Enter> to quit.") win.close()
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 = Button(win, Point(5,4.5), 6, 1, "Roll Dice") rollButton.activate() quitButton = Button(win, Point(5,1), 2, 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(): # create the application window win = GraphWin("Slye's Dyce", 1200, 800) win.setCoords(0, 0, 120, 80) type = gettype(win) playsummary(win, type) playgame(win, type)
def main(): win = GraphWin("Three Button Monte", 500, 300) win.setCoords(-12, -12, 12, 12) win.setBackground("green3") door1 = Button(win, Point(-7.5, -8), 5, 18, "Door 1") door2 = Button(win, Point(0, -8), 5, 18, "Door 2") door3 = Button(win, Point(7.5, -8), 5, 18, "Door 3") direction = Text(Point(0, 10), "Pick a Door") direction.draw(win) x = random() * 3 pt1 = pt2 = pt3 = False if 0 <= x < 1: pt1 = True elif 1 <= x < 2: pt2 = True else: pt3 = True door1.activate() door2.activate() door3.activate() click = win.getMouse() while door1.clicked(click) or door2.clicked(click) or door3.clicked(click): if door1.clicked(click): if pt1 == True: door1.update(win, "Victory!") else: if door2.clicked(click): door2.update(win, "Correct Door.") click = win.getMouse() else: door3.update(win, "Correct door.") click = win.getMouse() elif door2.clicked(click): if pt2 == True: door2.update(win, "Victory!") else: if door3.clicked(click): door3.update(win, "Correct Door.") click = win.getMouse() else: door1.update(win, "Correct door.") click = win.getMouse() else: if pt3 == True: door3.update(win, "Victory!") else: if door2.clicked(click): door2.update(win, "Correct Door.") click = win.getMouse() else: door1.update(win, "Correct door.") click = win.getMouse() win.getMouse()
def createGameWindow(): win = GraphWin("Projectile Animation", 640, 480, autoflush=False) width = 210 win.setCoords(-10, -10, width, 155) # Draw baseline Line(Point(-10, 0), Point(210, 0)).draw(win) # Draw labeled ticks every 50 meters for x in range(0, 210, 50): Text(Point(x, -5), str(x)).draw(win) Line(Point(x, 0), Point(x, 2)).draw(win) return win, width
def create_labeled_window(): window = GraphWin("Investment Growth Chart", 320, 240) window.setBackground("white") window.setCoords(-1.75, -200, 11.5, 10400) Text(Point(-1, 0), "0.0K").draw(window) Text(Point(-1, 2500), "2.5K").draw(window) Text(Point(-1, 5000), "5.0K").draw(window) Text(Point(-1, 7500), "7.5K").draw(window) Text(Point(-1, 10000), "10.0K").draw(window) return window
def draw_houses(houses, size, door_colour, prob): win = GraphWin("Street", 50 * houses * size, 50 * size) win.setCoords(0, 0, houses * size, 1) for house in range(houses): lights_on = random() if lights_on > prob: lights_on = False else: lights_on = True draw_house(win, door_colour, lights_on, house, size) win.getMouse()
def test(): window = GraphWin() window.setCoords(0, 0, 10, 10) r = Rectangle(Point(1, 1), Point(2, 2)) r.setFill("red") r.draw(window) for i in range(10): time.sleep(1) r.undraw() r.move(1, 0) r.draw(window) window.getMouse() window.close()
def main(): # create the application window window = GraphWin("Dice Roller") window.setCoords(0, 0, 10, 10) window.setBackground("green2") # draw the interface widgets # event loop # close the window window.close()
def main(): win = GraphWin("Five-Click House", 500, 400) win.setCoords(0.0, 0.0, 15.0, 15.0) message = Text(Point(7.5, 1), "Choose 5 points that draw a house and a door.") message.draw(win) p1 = win.getMouse() #choose the five points wisely for building the house p1.draw(win) p2 = win.getMouse() p2.draw(win) p3 = win.getMouse() p4 = win.getMouse() p5 = win.getMouse() x1 = p1.getX() # get the corresponding values for x and y y1 = p1.getY() x2 = p2.getX() y2 = p2.getY() x3 = p3.getX() y3 = p3.getY() x4 = p4.getX() y4 = p4.getY() last_leg_triangle = Point(x1, y2) Rectangle1 = Rectangle(p1, p2) #build the rectangle; the home Rectangle1.draw(win) #draw the building p_rect1 = Point((x3 - (((x2 - x1) / 5) / 2)), y3) p_rect2 = Point(x3 + (((x2 - x1) / 5) / 2), y1) p_square = Point(x4 + (((x2 - x1) / 5) * (3 / 4)), y4 + (((x2 - x1) / 5) * (3 / 4))) p_square_2 = Point(x4 - (((x2 - x1) / 5) * (3 / 4)), y4 - (((x2 - x1) / 5) * (3 / 4))) Rect = Rectangle(p_rect1, p_rect2) Rect.draw(win) Rect4 = Rectangle(p_square, p_square_2) Rect4.draw(win) Triangle = Polygon(p5, p2, last_leg_triangle) Triangle.draw(win) win.getMouse()
def draw_circle(): """ 2. Write a draw_circle() function which asks the user for the radius of a circle and then draws the circle in the centre of a graphics window. """ radius = float(input("Enter the radius: ")) / 100 centre = Point(0.5, 0.5) win = GraphWin("Circle") win.setCoords(0, 0, 1, 1) Circle(centre, radius).draw(win) await_user_input(win)
def main(): # create GraphWin win = GraphWin("Three Button Monte", 800, 600) coordWidth = 10; coordHeight = 10 factorWidth = win.width / coordWidth; factorHeight = win.height / coordHeight win.setCoords(0, 0, coordWidth, coordHeight) # place three buttons in window # doors = [Button(win, Point(2, 5), 2, 4, door1(), factorWidth, factorHeight), # Button(win, Point(5, 5), 2, 4, door2(), factorWidth, factorHeight), # Button(win, Point(8, 5), 2, 4, door3(), factorWidth, factorHeight)] doors = [Button(win, Point(2, 5), 2, 4, "Door 1"), Button(win, Point(5, 5), 2, 4, "Door 2"), Button(win, Point(8, 5), 2, 4, "Door 3")] for door in doors: door.setSize(36) door.activate() # place Quit button in window # quitButton = Button(win, Point(8.5, 1.5), 1, 1, "Quit", factorWidth, factorHeight) quitButton = Button(win, Point(8.5, 1.5), 1, 1, "Quit") quitButton.setSize(24) quitButton.activate() # place blank message text in window result = Text(Point(3.5, 1.5), '') result.setSize(18) result.draw(win) # get mouse click pt = win.getMouse() # event loop...until Quit clicked while not quitButton.clicked(pt): # randomly determine which door is correct secretDoor = randrange(0, 3) # take action for button clicked (1, 2, 3, or Quit) if doors[secretDoor].clicked(pt): result.setText('Congratulations, you are correct!') else: result.setText('Sorry, the correct door was door #{}'.format(secretDoor + 1)) sleep(2) result.setText('') # get mouse click pt = win.getMouse() win.close()
def table_tennis_scorer(): """ 8. Write a table_tennis_scorer() function that allows the user to keep track of the points of two players in a game of table tennis. In table tennis, the first player to reach 11 points wins the game; however, a game must be won by at least a two point margin. The points for the players should be displayed on two halves of a graphics window, and the user clicks anywhere on the appropriate side to increment a player's score. As soon as one player has won, a 'wins' message should appear on that side. """ win = GraphWin("Table tennis scorer", 500, 500) win.setCoords(0, 0, 1, 1) left_score = 0 right_score = 0 divider = Line(Point(0.5, 0), Point(0.5, 1)) divider.draw(win) left_score_text = Text(Point(0.25, 0.6), 0) left_score_text.setSize(35) left_score_text.draw(win) right_score_text = Text(Point(0.75, 0.6), 0) right_score_text.setSize(35) right_score_text.draw(win) while not ((left_score >= 11 or right_score >= 11) and (left_score >= (right_score + 2) or right_score >= (left_score + 2))): cursor = win.getMouse() if cursor.getX() <= 0.5: left_score += 1 left_score_text.setText(left_score) else: right_score += 1 right_score_text.setText(right_score) if left_score > right_score: winner_text_centre = Point(0.25, 0.5) else: winner_text_centre = Point(0.75, 0.5) winner_text = Text(winner_text_centre, "Winner") winner_text.setSize(20) winner_text.draw(win) cursor = win.getMouse() win.close()
def clickable_box_of_eyes(cols=None, rows=None): """ 9. [harder] Write a function clickable_box_of_eyes() that takes two parameters columns and rows, and displays a rows x columns grid of blue eyes (all of radius 50) within a box (rectangle). There should be a border of size 50 between the box and the edge of the window. For each click of the mouse inside the box, the function should behave as follows: if the click is on an eye, the row and column of that eye should be displayed in the space below the box, for example as shown below given the click denoted by the dot. Note: row and column numbers should begin at 1. If the user clicks within the box but not on an eye, the displayed message should be 'Between eyes'. The window should close when the user clicks outside the box. """ if cols is None or rows is None: cols = int(input("Enter the number of cols > ")) rows = int(input("Enter the number of rows > ")) b_and_cols = cols + 1 # columns plus border b_and_rows = rows + 1 # rows plus border win = GraphWin("Clickable box of eyes", 100 * b_and_cols, 100 * b_and_rows) win.setCoords(0, b_and_rows, b_and_cols, 0) for i in range(1, b_and_rows): for j in range(1, b_and_cols): draw_blue_eye(win, Point(j, i), 0.5) container = Rectangle(Point(0.5, 0.5), Point(b_and_cols - 0.5, b_and_rows - 0.5)) container.draw(win) message = Text(Point(b_and_cols / 2, b_and_rows - 0.25), "") message.setSize(20) message.draw(win) while True: cursor = win.getMouse() col = int(cursor.getX() + 0.5) row = int(cursor.getY() + 0.5) if col == 0 or row == 0 or col == b_and_cols or row == b_and_rows: win.close() break if distance_between_points(cursor, Point(col, row)) > 0.5: message.setText("Between eye") else: message.setText(f"Eye at row {row}, column {col}")
def main(): win = GraphWin("Paint a Mosaic", width=400, height=400) win.setCoords(0.0, 0.0, 100.0, 100.0) # This while loop will run indefinitely (Ctrl + C to kill the process) while True: point = win.getMouse() topLeft = getClosestGridVertix(point) bottomRight = Point(topLeft.getX() + 10, topLeft.getY() + 10) rectangle = Rectangle(topLeft, bottomRight) # Randomly set a hexadecimal color as fill. color = "#{0:06x}".format(random.randint(0, 0xFFFFFF)) rectangle.setFill(color) rectangle.draw(win)
def main(): #Create the game view window win = GraphWin("GameView") win.setCoords(0, 0, 10, 10) win.setBackground("green2") winCount = 0 loseCount = 0 correct = randrange(1,4) choice = 0 # draw the buttons cb1 = Button(win, Point(1.75,3), 3, 5, "Option 1") cb1.activate() cb2 = Button(win, Point(5.125,3), 3, 5, "Option 2") cb2.activate() cb3 = Button(win, Point(8.5,3), 3, 5, "Option 3") cb3.activate() qb = Button(win, Point(8.5,9), 3, 1, "Quit") pt = win.getMouse() while not qb.clicked(pt): if cb1.clicked(pt): choice = 1 elif cb2.clicked(pt): choice = 2 elif cb3.clicked(pt): choice = 3 if choice == correct: winCount = winCount + 1 print("you win, keep going or quit") else: loseCount = loseCount + 1 print("you lose, keep going or quit") qb.activate() pt = win.getMouse() wMessage = str(winCount) lMessage = str(loseCount) print("you won "+wMessage+" times.") print("you lost "+lMessage+" times.") win.close()
def start_simulation(self, simulation_speed=1, framerate=30): # Header initializes window simulation_speed = 1 / simulation_speed win_name = self.name + " - N-Body Problem" win = GraphWin(win_name, self.size, self.size, autoflush=False) win.setBackground("black") win.setCoords(-self.size / (2 * self.scale), -self.size / (2 * self.scale), self.size / (2 * self.scale), self.size / (2 * self.scale)) # Body start_time = time.time() current_time = time.time() draw_time = time.time() while win.checkMouse() is None: # FIXME this should be drawspeed if simulation_speed < time.time() - current_time: current_time = time.time() # draw update to window win.delete("all") # TODO ezen belul legyen self.update(simulation_speed) self.print(win) # If max time is exceeded break out of loop else: continue # Max time reached stop simulation if self.max_time < time.time() - start_time: break # If time passed is greater than refresh rate, refresh # TODO lehet ezt az updaten belul kulon is kene kezelni update(framerate) # Trailer closes window win.close() if not self.collisions: print("Simulation successful; System Stable") else: print("Simulation over; System Unstable\nCollisions: ", self.collisions)
def draw_gui(): """Create the graphics window and draw the target.""" # create the window win = GraphWin("Archery game", 500, 500) win.setBackground("cyan") win.setCoords(0, 0, 1, 1) centre = Point(0.5, 0.5) # draw the grass ground_rect = Rectangle(Point(-0.01, -0.01), Point(1.01, 0.5)).draw(win) ground_rect.setFill("green") # draw the target legs left_target_stand = Polygon(Point(0.02, -0.01), Point(0.45, 0.5), Point(0.55, 0.5), Point(0.12, -0.01)).draw(win) left_target_stand.setFill("brown") left_target_stand.setWidth(2) right_target_stand = Polygon(Point(0.98, -0.01), Point(0.55, 0.5), Point(0.45, 0.5), Point(0.88, -0.01)).draw(win) right_target_stand.setFill("brown") right_target_stand.setWidth(2) # draw the target draw_circle(win, centre, 0.3, "blue") draw_circle(win, centre, 0.2, "red") draw_circle(win, centre, 0.1, "yellow") # draw the text displaying the score score = 0 score_text = Text(Point(0.5, 0.04), f"Score: {score}").draw(win) score_text.setSize(18) score_text.setStyle("bold") # draw the text displaying where an arrow has landed zone_text = Text(Point(0.5, 0.09), "").draw(win) zone_text.setSize(12) zone_text.setStyle("bold") # draw the text describing wind conditions wind_text = Text(Point(0.5, 0.97), "Wind: ").draw(win) wind_text.setSize(12) wind_text.setStyle("bold") return win, wind_text, zone_text, score_text, score
def main(): win = GraphWin("map", 1000, 800) win.setCoords(-88.357, 41.786, -88.355, 41.788) with open("2016-08-04_20-20-41.000.txt", "r") as log: lines = log.readlines() for n in range(0, len(lines) - 1, 3): line = lines[n].split(", ") if len(line) == 6: p = Point(float(line[2]), float(line[1])) p.setFill('red') Line(p, vect(p, radians(-(float(line[3]) + 270)))).draw(win) p.draw(win) else: Text(Point(float(lines[n+1].split(", ")[2]) + .00005, float(lines[n+1].split(", ")[1]) + .00005), line[0]).draw(win) print(line)
class SkewerUI(object): """ Class: SkewerUI Description: A graphical display for the Skewer. It displays the items in on the skewer graphically, as they are added, removed and shifted around by the various commands. """ __slots__ = ( 'win', # the graphical window 'items' # the list of food items (strings) from top to bottom ) def __init__( self, N ): """ Create the SkewerUI. Arguments: self - the SkewerUI being created N - the capacity of the skewer (for the window size) """ # create the window self.createWindow( N ) # (Sets value of win attribute.) self.items = [] def close(self): """ On destruction, close the graphical window. Arguments: self - the SkewerUI being created """ self.win.close() def createWindow(self, N): """ Create the graphics window. Arguments: self - the SkewerUI instance N - the capacity of the skewer """ self.win = GraphWin("Shish Kebab", 800, 200) self.win.setCoords( \ WIN_LOW_LEFT_X, \ WIN_LOW_LEFT_Y - 0.1, \ WIN_LOW_LEFT_X+(N+1)*FOOD_WIDTH, \ WIN_UP_RIGHT_Y + 0.1 \ ) # draw skewer line = Line( \ Point(WIN_LOW_LEFT_X, WIN_LOW_LEFT_Y+WIN_HEIGHT/2.0), \ Point(N, WIN_LOW_LEFT_Y+WIN_HEIGHT/2.0) \ ) line.setWidth(LINE_THICKNESS) line.draw(self.win) handle = Circle( \ Point(N-.1, WIN_LOW_LEFT_Y+WIN_HEIGHT/2.0), \ SKEWER_HANDLE_RADIUS \ ) handle.setFill(BKGD_COLOR) handle.setWidth(LINE_THICKNESS) handle.draw(self.win) self.items = [] class _ShapeInfo(): __slots__ = [ 'shapeClass', 'ctorArgs' ] def __init__( self, shapeClass, ctorArgs ): self.shapeClass = shapeClass self.ctorArgs = ctorArgs Shapes = { \ True: _ShapeInfo( Circle, ( Point( 0, .5 ), .5 ) ), \ False: _ShapeInfo( Rectangle, ( Point( -.5, 0 ), Point( .5, 1 ) ) ) \ } # The bool is for vegetarian def add( self, food ): """ Called whenever an item is added to the Skewer, so the graphics can be updated. It uses the KSpot class to get the food items and display them. Arguments: self - the SkewerUI instance food - the new food added to the skewer (KSpot) """ if food != None: shapeInfo = SkewerUI.Shapes[ food.item.veggie ] graphic = (shapeInfo.shapeClass)( *shapeInfo.ctorArgs ) graphic.setFill(Colors[food.item.name]) graphic.draw( self.win ) for prev in self.items: prev.move( 1, 0 ) self.items.insert( 0, graphic ) def remove( self ): """ Called whenever an item is removed to the Skewer, so the graphics can be updated. It uses the KSpot class to get the food items and display them. Arguments: self - the SkewerUI instance head - the head of the skewer (KSpot) """ if len( self.items ) != 0: self.items[ 0 ].undraw() self.items.pop( 0 ) for prev in self.items: prev.move( -1, 0 )
def setup_graphics_window(winname, glength, gwidth, slength, swidth): win = GraphWin(winname, glength, gwidth) win.setCoords(0, 0, slength, swidth) return win