def __init__(self, xloc, num=random.randint(1, 6), willDiscard=False): self.num = num self.willDiscard = willDiscard self.xloc = xloc self.img = Rectangle(Point(20 + 50 * self.xloc, 100), Point(20 + 50 * (self.xloc + 1), 50)) self.text = Text(Point(45 + 50 * self.xloc, 75), self.num).draw(win)
def __init__(self, win, center, width, height, label='Button'): """ Creates a rectangular button. Example usage: ============== centerPoint = Point(x, y) button = Button(win, centerPoint, width, height, 'Click Me!') """ # Get most extreme vertices of button half_width = width / 2.0 half_height = height / 2.0 x, y = center.getX(), center.getY() self.x_min, self.y_min = x - half_width, y - half_height self.x_max, self.y_max = x + half_width, y + half_height # Create button outline point1 = Point(self.x_min, self.y_min) point2 = Point(self.x_max, self.y_max) self.rect = Rectangle(point1, point2) self.rect.setFill('lightgrey') self.rect.draw(win) # Create button text self.label = Text(center, label) self.label.draw(win) self.deactivate()
def main(): Maths.generateMathExpressions() print("Available operators:") for o in Maths.Expressions: print(Maths.Expressions[o].getAbbreviation()) print("-------------------------") f = Function("cos(3*x)+6/4*(x+3)", False) print("RPN String", f.getRpnString()) for x in range (2, 11): f.compute(x) print("-------------------------") f = Function("56*((6+2)/(8-x)*2^3", False) print("RPN String", f.getRpnString()) #should give 56 6 2 + 8 7 - / 2 3 ^ * * mainwindow = MainWindow("Function Drawer", 992, 512) fx = f.computeRange(0, 10) max_y = max(fx.values()) min_y = min(fx.values()) print(min_y, max_y) mainwindow.setCoords(-1, min_y, 11, max_y) for x in range(0, 11): print(fx[x]) p = Point(x, fx[x]) p.draw(mainwindow) input("Press a key to quit")
def __init__(self, canvas): self.canvas = canvas self.table = Table(self.canvas, Point(100, 25), col_width=150, font_size=10) self.selected_car = None self.show_route = False self.follow_car = False self.show_route_btn = Button( self.flip_show_route, self.canvas, Point(self.canvas.width / 2, 320), width=200, height=30, label='Show Route', font_size=10, ) self.follow_btn = Button( self.flip_follow_car, self.canvas, Point(self.canvas.width / 2, 360), width=200, height=30, label='Follow Selected Car', font_size=10, ) self.buttons = [self.show_route_btn, self.follow_btn]
def draw(self) -> None: top = Point(self.x + self.__width / 2, self.y - self.__height / 2) bottom_left = Point(self.x + self.__width / 2 - self.__width / 2, self.y + self.__height / 2) bottom_right = Point(self.x + self.__width / 2 + self.__width / 2, self.y + self.__height / 2) graphics.draw_polygon((top, bottom_left, bottom_right), self.color)
def __init__(self, win, p1, p2, background=None): super().__init__(win, p1, p2, background) self.win = win self.background = background self.setFill(self.background) width = p2.getX() - p1.getX() height = p2.getY() - p1.getY() width = self.getWidth() height = self.getHeight() # Draw the widgets die = [SixSidedDie()] * 2 dieSize = min(width // 4, height // 4) anchors = [] offset = dieSize * 2 // 3 anchors.append( Point(p1.getX() + (dieSize + width // 3) - offset, p1.getY() + height // 2 - offset)) anchors.append( Point(p1.getX() + (dieSize + width // 3) + offset, p1.getY() + height // 2 + offset)) self.die1 = DieView(die[0], self.win, anchors[0], dieSize, self.BACKGROUND, self.FOREGROUND) self.die2 = DieView(die[1], self.win, anchors[1], dieSize, self.BACKGROUND, self.FOREGROUND)
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_round(win, Point(5, 4), 1.5, "Roll") rollButton.activate() quitButton = Button_round(win, Point(5, 1), 1.5, "Quit") # Event loop pt = win.getMouse() print("x=" + str(pt.getX()) + ", and y=" + str(pt.getY())) 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 __init__(self, win, center, size): """Create a view of a die, e.g.: d1 = GDie(myWin, Point(40,50), 20) creates a die centered at (40,50) having sides of length 20.""" # first defind some standard values self.win = win self.background = "white" # color of die face self.foreground = "black" # color of the pips self.psize = 0.1 * size # radius of each pip hsize = size / 2.0 # half of size offset = 0.6 * hsize # distance from center to outer pip # create a square for the face cx, cy = center.getX(), center.getY() p1 = Point(cx - hsize, cy - hsize) p2 = Point(cx + hsize, cy + hsize) rect = Rectangle(p1, p2) rect.draw(win) rect.setFill(self.background) # Create 7 circles for standard pip locations self.pip1 = self.__makePip(cx - offset, cy - offset) self.pip2 = self.__makePip(cx - offset, cy) self.pip3 = self.__makePip(cx - offset, cy + offset) self.pip4 = self.__makePip(cx, cy) self.pip5 = self.__makePip(cx + offset, cy - offset) self.pip6 = self.__makePip(cx + offset, cy) self.pip7 = self.__makePip(cx + offset, cy + offset) self.setValue(1)
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 regStage(self, window): background = Image(Point(600, 400), "ImagesAndSprites/StartScreen.gif") background.draw(window) self.platform1 = Image(Point(600, 675), "ImagesAndSprites/blue.gif") self.platform1.draw(window) self.pos1 = Point(400, 700) self.pos2 = Point(800, 700)
def run(self): while self.money >= 10 and self.interface.wantToPlay(): self.playRound() scoreboard = Scoreboard() for score in scoreboard.top10: if self.money > int(score.score): win2 = GraphWin("Dice Poker", 600, 400) win2.setBackground("green3") banner = Text(Point(300, 30), "New High Score!\nPlease enter your name:") banner.setSize(24) banner.setFill("yellow2") banner.setStyle("bold") banner.draw(win2) textbox = Entry(Point(300, 150), 30) textbox.draw(win2) enter_btn = Button(win2, Point(300, 200), 60, 30, "Enter") enter_btn.activate() p = win2.getMouse() playerName = str(textbox.getText()) if enter_btn.clicked(p): scoreboard.addScore(playerName, self.money) scoreboard.sortScores() scoreboard.saveScores() break self.interface.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 draw_game_over(self): width = 300 height = 50 winners = self.__state.highest_score_players() tx = self.win.width / 2 - width / 2 ty = self.win.height / 2 - height / 2 rect = Rectangle(Point(tx, ty), Point(tx + width, ty + height)) rect.setFill("grey") rect.draw(self.win) if len(winners) == 1: text_str = 'Player {} wins, press q to exit'.format(winners[0]) else: text_str = 'Players {} win, press q to exit'.format(', '.join( str(w) for w in winners)) text = Text(Point(tx + width / 2, ty + 20), text_str) text.setTextColor("black") text.draw(self.win) key = self.wait_key() if key == 'q': text.setText('exiting...') time.sleep(1) self.win.close() else: self.reset() self.loop()
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 __init__(self, pos, width, height, type=None, bc=color_rgb(255, 255, 255), text="", size=18, txtc=color_rgb(0, 0, 0), style="normal", font="arial"): oWidth = width / 2 #offset width oHeight = height / 2 #offset height pos1 = Point(pos.getX() - oWidth, pos.getY() - oHeight) pos2 = Point(pos.getX() + oWidth, pos.getY() + oHeight) self.object = Rectangle(pos1, pos2) self.object.setFill(bc) self.object.setOutline(color_rgb(255, 255, 255)) self.pos = pos self.pos1 = pos1 #Left-Up Corner self.pos2 = pos2 #Right-Down Corner self.type = type self.width = width self.height = height self.text = Text(self.object.getCenter(), text, size, txtc, style, font)
def handleCollision(self, other): """Perform physics on ball if in contact with another ball""" if not self.isCollided(other): return m1 = math.pi * math.pow(self.rad, 2) v1 = math.hypot(self.vel.x, self.vel.y) t1 = math.atan2(self.vel.y, self.vel.x) m2 = math.pi * math.pow(other.rad, 2) v2 = math.hypot(other.vel.x, other.vel.y) t2 = math.atan2(other.vel.y, other.vel.x) distX = self.pos.x - other.pos.x distY = self.pos.y - other.pos.y phi = math.atan2(distY, distX) v1f = ((v1 * math.cos(t1 - phi) * (m1 - m2)) + (2 * m2 * v2 * math.cos(t2 - phi))) / (m1 + m2) v1xf = (v1f * math.cos(phi)) + (v1 * math.sin(t1 - phi) * math.cos(phi + (math.pi / 2))) v1yf = (v1f * math.sin(phi)) + (v1 * math.sin(t1 - phi) * math.sin(phi + (math.pi / 2))) v1F = Point(v1xf, v1yf) v2f = ((v2 * math.cos(t2 - phi) * (m2 - m1)) + (2 * m1 * v1 * math.cos(t1 - phi))) / (m2 + m1) v2xf = (v2f * math.cos(phi)) + (v2 * math.sin(t2 - phi) * math.cos(phi + (math.pi / 2))) v2yf = (v2f * math.sin(phi)) + (v2 * math.sin(t2 - phi) * math.sin(phi + (math.pi / 2))) v2F = Point(v2xf, v2yf) self.vel = v1F other.vel = v2F
def foodRespawn(self): if self.graphWin.render: self.food.undraw() noSnakeCoords = self.mapNoSnake() randomCoord = noSnakeCoords[random.randrange(0, len(noSnakeCoords))] p1 = Point(randomCoord[0] * 20, randomCoord[1] * 20) p2 = Point(randomCoord[0] * 20 + 20, randomCoord[1] * 20 + 20) self.food = Food(self.graphWin, p1, p2)
def __init__(self, center): coords = [ Point(center.x - 1, center.y), Point(center.x, center.y), Point(center.x, center.y + 1), Point(center.x + 1, center.y + 1) ] Shape.__init__(self, coords, "yellow")
def __init__(self, center): coords = [ Point(center.x - 1, center.y), Point(center.x, center.y), Point(center.x + 1, center.y), Point(center.x + 1, center.y + 1) ] Shape.__init__(self, coords, "orange1")
def update(self): """Update position of ball, turning the ball around if it gets too close to an edge""" tempPos = Point(0, 0) tempPos.x = self.pos.x + self.vel.x tempPos.y = self.pos.y + self.vel.y self.pos = tempPos self.__moveBall(self.pos)
def __init__(self, center): coords = [ Point(center.x - 1, center.y), Point(center.x - 1, center.y + 1), Point(center.x, center.y + 1), Point(center.x, center.y) ] Shape.__init__(self, coords, "cyan")
def draw_rectangle(win, colour, point1, point2, current_tile, fill=False): """Helper function for drawing rectangles.""" current_rectangle = Rectangle(Point(*point1), Point(*point2)) if fill: current_rectangle.setFill(colour) current_rectangle.setOutline(colour) current_rectangle.draw(win) current_tile.append(current_rectangle)
def __init__(self, xPos, yPos, type): self.xPos = xPos self.yPos = yPos self.type = type self.img = Rectangle(Point((20 * xPos), (20 * yPos)), Point((20 * xPos + 20), (20 * yPos + 20))) self.img.setFill(colors[self.type]) self.img.draw(win)
def __init__(self, center): coords = [Point(center.x - 2, center.y), Point(center.x - 1, center.y), Point(center.x , center.y), Point(center.x + 1, center.y)] Shape.__init__(self, coords, "blue") self.shift_rotation_dir = True self.center_block = self.blocks[2]
def draw_cell(self, x, y, state): (gx, gy) = self.cell_coord(x, y) inset = 1 box = Rectangle( Point(gx + inset, gy + inset), Point(gx + self.cell_width - inset, gy + self.cell_height - inset)) box.setFill(self.bgcolour if state == 0 else self.cellcolour) box.draw(self.win)
def __init__(self, center): coords = [ Point(center.x - 2, center.y), Point(center.x - 1, center.y), Point(center.x, center.y), Point(center.x + 1, center.y) ] Shape.__init__(self, coords, "blue")
def main(): win = GraphWin("An Awesome Car", 700, 300) car = Car(Point(50, 50), 15, Point(100, 50), 15, 40) car.set_color('black', 'gray', 'pink') car.draw(win) win.mainloop()
def checkForHit(shot, target): p = Point(shot.getX(), shot.getY()) while (0 <= shot.getY() and -10 < shot.getX() <= 210) \ and not(target.hit(p)): shot.update(1 / 50) update(50) p = Point(shot.getX(), shot.getY()) return p
def __init__(self, win): self.buttonPos = Point(180, 300) self.buttonDis = 20 self.buttons = [] self.buttonAdd(160, 50, MenuButton.PLAY, "Play") self.buttonAdd(160, 50, MenuButton.CONNECT, "Connect") self.buttonAdd(160, 50, MenuButton.QUIT, "Quit")
def __init__(self, bl, tr, color): self.bl = bl self.tr = tr self.rectangle = Rectangle(Point(self.bl[0], self.bl[1]), Point(self.tr[0], self.tr[1])) self.piece_image = None self.piece_obj = None self.state = {"active": False, "original_fill": color}
class Particle: def __init__(self, window, p=Point(0, 0)): self.particle = None self.drawn = False self.color = "RED" self.position = p self.x = p.getX() self.y = p.getY() self.size = 3 self.dX = 0 self.dY = 0 self.win = window self.particleTurnCount = 0 def setCoord(self, x, y): self.x = x self.y = y def setColor(self, color): self.color = color if self.particle: self.particle.setFill(color) def setSize(self, size): self.size = size if self.drawn: self.undraw() self.draw() def draw(self): self.particle = Circle(Point(self.x, self.y), self.size) self.particle.setFill(self.color) self.particle.draw(self.win) self.drawn = True def undraw(self): self.particle.undraw() self.drawn = False def setParticleMovement(self, dx, dy): self.dX = dx self.dY = dy def move(self): self.particle.move(self.dX, self.dY) self.position = Point(self.position.getX() + self.dX, self.position.getY() + self.dY) self.particle.undraw() self.particle.draw(self.win)
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)
def undraw_entity(entity, win): if win is not None: p = Point(entity.x_pos, entity.y_pos) p.setOutline("red") p.draw(win)
def move(self): self.particle.move(self.dX, self.dY) self.position = Point(self.position.getX() + self.dX, self.position.getY() + self.dY) self.particle.undraw() self.particle.draw(self.win)