def errorMessage(message): """Displays an error message in a new window""" win = graphics.GraphWin('Error Message', 350, 175) win.setBackground('#F75454') error = graphics.Text(graphics.Point(win.getWidth()/2, win.getHeight()/2 - 50), 'Error') error.setSize(30) error.setStyle('bold') error.draw(win) text = graphics.Text(graphics.Point(win.getWidth()/2, win.getHeight()/2), message) text.setFace('times roman') text.draw(win) #Press OK button to close the window okButton = Button(win, graphics.Point(win.getWidth()/2, win.getHeight()/2 + 55), 100, 30, 'lightgrey', 'OK') loop = True while loop: mousePress = win.getMouse() if okButton.clicked(mousePress): win.close() loop = False
def drawText(text): """Draws text anchored at graphics pen point. The colour is given by the current line colour, and the properties of the text can be changed by setTextProperties(). @param text String to write to screen """ text = graphics.Text(_current_point, text) text.setFace(_text_properties["face"]) text.setSize(_text_properties["size"]) text.setStyle(_text_properties["style"]) text.setTextColor(_current_line_colour) # Hack into the graphics library to set both justification and anchor point text.config["justify"] = _text_properties["align"] text.config["anchor"] = _text_properties["anchor"] _objects.append(text)
def draw_circle_name(): name,color = get_name_color() win = graphics.GraphWin("circle",500, 500) circle = graphics.Circle(graphics.Point(250,250),50) try: circle.setFill(color) circle.draw(win) except: print("Invalid color, please use the color in the graphics library!") else: name_label = graphics.Text(graphics.Point(250,250),name) name_label.draw(win)
def clickHere(): message = graphics.Text(graphics.Point(200, 100), "Click Here") rect = graphics.Rectangle(graphics.Point(160, 60), graphics.Point(240, 140)) newwin = graphics.GraphWin("Clicky Window", 400, 400) rect.draw(newwin) rect.setFill("yellow") message.draw(newwin) pt = newwin.getMouse() while ((160 < pt.getX() < 240) and (60 < pt.getY() < 140)) == False: pt = newwin.getMouse() rect.undraw() message.setText("click to close") newwin.getMouse() newwin.close()
def addMessage(window, message, size=20, color="black", reset=False, bold=False): '''message add''' if reset: addMessage.y = WINDOW_HEIGHT / 10 msg = gfx.Text(gfx.Point(WINDOW_WIDTH / 2, addMessage.y), message) msg.setSize(size) msg.setFill(color) msg.draw(window) msg.setStyle("bold" if bold else "normal") addMessage.y += 40 return msg
def drawWin(gameWin): '''Draws stars all over the screen to indicate a win by the user Parameter: gameWin - window the graphic is drawn into ''' for i in range(0, 1200, 100): for j in range(0, 800, 100): winStar = graphics.Image(graphics.Point(i, j), "goldstar.gif") winStar.draw(gameWin) finalStar = graphics.Image(graphics.Point(500, 325), "goldstar.gif") finalStar.draw(gameWin) winText = graphics.Text(graphics.Point(500, 325), "YOU WIN!") winText.setFace("arial") winText.setStyle('bold') winText.setSize(36) winText.draw(gameWin)
def clickable_eye(): window = g.GraphWin('clickable_eye', 500, 500) centre = g.Point(250, 250) draw_brown_eye_in_centre(window, centre, 100) label = g.Text(g.Point(50, 50), '') label.draw(window) while True: m = window.getMouse() if distance_between_points(centre, m) < 25: label.setText('pupil') elif distance_between_points(centre, m) < 50: label.setText('iris') elif distance_between_points(centre, m) < 100: label.setText('sclera') else: window.close() return
def visualize_dg(self, win, dg): """ :param win: which window to draw :param dg: dijkstras grid :return: """ for r in range(self.rows): for c in range(self.cols): if dg[r, c] != math.inf: x, y = self.map2_visgrid((r, c)) label = graphics.Text(graphics.Point(x * self.stepsize + self.stepsize * 0.5, y * self.stepsize + self.stepsize * 0.5), dg[r, c]) label.setTextColor('red') # label.setSize(20) # label.setStyle('italic') label.draw(win)
def drawTie(gameWin): '''Draws ties all over the screen to indicate it was a tie game also draws text Parameter: gameWin - window the graphic is drawn into ''' for i in range(0, 1200, 100): for j in range(0, 800, 100): tie = graphics.Image(graphics.Point(i, j), "tie.gif") tie.draw(gameWin) finalTie = graphics.Image(graphics.Point(500, 325), "finalTie.gif") finalTie.draw(gameWin) tieText = graphics.Text(graphics.Point(500, 250), "It's a tie!") tieText.setFace("arial") tieText.setStyle('bold') tieText.setSize(36) tieText.draw(gameWin)
def motion(event): #It's right here # print('motion {}, {}'.format(event.x, event.y)) global x global y x = event.x y = event.y global dT p = 0 z = len(X1s) - 1 while z > -1: if (X1s[z] > x and x > X2s[z] and Y1s[z] > y and y > Y2s[z]): # dT.undraw() dT = graphics.Text(graphics.Point(1050, 40), building[z]) dT.draw(BSUmapimage.Map) z -= 1
def update(self): self.__box = g.Rectangle( self.corner, g.Point(self.corner.getX() + self.w, self.corner.getY() + self.h)) self.__box.setWidth(3) self.__label = g.Text( g.Point(self.corner.getX() + (self.w / 2), self.corner.getY() + (self.h / 2)), self.number) self.__label.setFace('arial'), self.__label.setStyle( 'bold'), self.__label.setSize(30) if self.selected == True: self.__box.setOutline('gray'), self.__box.setFill('black') self.__label.setFill('red') else: self.__box.setOutline('white'), self.__box.setFill('gray') self.__label.setFill('black')
def updateWindow(gameWindow, figurelist, rounds): """Update the game window""" for item in gameWindow.items[:]: item.undraw() #Remove al elements in game window gameWindow.update() #update to ensure al is gone for i in range(15): #For loops to draw new figures for k in range(25): todraw = figurelist[i][k] todraw.draw(gameWindow) scoreText = str( "Pick number:" + str(rounds) ) #Show how many times a color has been picked(Shows 1 on first round) toPrint = g.Text(g.Point(100, 775), scoreText) #Create text to print toPrint.draw(gameWindow) #Draws text with rounds
def collect_key(key): """ Removes key from screen and adds key to counter. requires key object type input """ global keys_text if key != None: key.loop.undraw() key.keybody.undraw() key.prong1.undraw() key.prong2.undraw() Key.keys_collected += 1 keys_text.undraw() keys_text = g.Text(g.Point(60, 40), f"KEYS: {str(Key.keys_collected)}") keys_text.setFill("white") keys_text.draw(key.w)
def drawLoss(gameWin): '''Draws sad faces to indicate a loss by the user all over the screen Parameter: gameWin - window the graphic is drawn into ''' for i in range(0, 1200, 100): for j in range(0, 800, 100): loseFace = graphics.Image(graphics.Point(i, j), "lose.gif") loseFace.draw(gameWin) finalLose = graphics.Circle(graphics.Point(500, 325), 150) finalLose.setFill("blue") finalLose.draw(gameWin) loseText = graphics.Text(graphics.Point(500, 325), "Computer Wins") loseText.setFace("arial") loseText.setStyle('bold') loseText.setSize(36) loseText.draw(gameWin)
def show(self, graphics_window, location=None, color=None): if color is None: if self.peer.registered: color = "green" else: color = "white" if location is None: location = self.location else: self.location = location circle = gf.Circle(location, CIRCLE_R) circle.setFill(color) circle.setOutline("yellow") circle.draw(graphics_window) label = gf.Text(location, self.name) label.setTextColor('red') label.setSize(CIRCLE_R - CIRCLE_R//4) label.draw(graphics_window)
def renderFrame(self, reward=0): for gObj in self.graphicsObjectList: gObj.undraw() self.graphicsObjectList = [] textpos = graphics.Point(50, 5) self.text.undraw() self.text = graphics.Text(textpos, str(reward)) self.text.draw(self.window1) i = 0 for obj in self.objectList: points = obj.get2DpointList() for point in points: p1 = graphics.Point(point[0], point[1]) c = graphics.Circle(p1, 1) c.setFill(self.colors[i % self.cnumber]) c.draw(self.window1) self.graphicsObjectList.append(c) i += 1
def __draw_current_frame_index(self, current_frame_idx: int) -> gfx.Text: total_frames = self.__LOADED_MATCH.frame_count() frame_id = self.__LOADED_MATCH.ball_frames[current_frame_idx].frame_id # estimated_match_seconds = frame_id / game_data.metadata.fps # text_str = f"{current_frame_idx}/{total_frames} (FrameID: {frame_id})" text_str = "%06d/%06d (FrameID: %09d)" % (current_frame_idx, total_frames - 1, frame_id) text_object = gfx.Text( gfx.Point(self.__CANVAS_SIZE_X_PX / 2, self.__CANVAS_Y_PADDING_PX / 2), text_str) text_object.draw(self.__GFX_GraphWin) self.__DYNAMIC_DRAW_OBJECTS.append(text_object) return text_object
def _create_terminal_window(self): # Creates the terminal window using John Zelle's graphics module. win = graphics.GraphWin("RichEmu86", 890, 408) win.setBackground("black") s = "0123456789" * 8 i = 0 x = 446 y = 12 height = 16 fontSize = 14 for i in range(0, 25): t = graphics.Text(graphics.Point(x, y), s) t.setSize(fontSize) t.setFace("courier") t.setTextColor("white") t.draw(win) y = y + height win.getMouse() win.close()
def drawChar(self, char: str, x: int, y: int, clr=None, fontSize=None): """ Draws char wher spesified """ if clr is None: clr = self._getDrawColor(char) self.erase(x, y) labelBox = gr.Text( gr.Point((x + 1) * self.fontSizeX, (y + 1) * self.fontSizeY), char) labelBox.setTextColor(clr) if not fontSize is None: labelBox.setSize(fontSize) labelBox.setFace('courier') labelBox.draw(self.win) else: labelBox.setSize(12) labelBox.draw(self.win)
def computerDrawPokemon(currentPokemon, window): ''' Draws the current Pokemon on the display window. ''' print("Computer chooses", currentPokemon.getName(), "!") #Get all the relevant information about the Pokemon to display originalHealth = currentPokemon.getOriginalHealth() currentPokemonHealth = currentPokemon.getHealth() currentPokemonType = currentPokemon.getType() currentPokemonWeakness = currentPokemon.getWeakness() currentPokemonResistance = currentPokemon.getResistance() healthInfo = "Health: " + str(currentPokemonHealth) + "\n Type: " + str( currentPokemonType) + ", Weakness: " + str( currentPokemonWeakness) + ", Resistance: " + str( currentPokemonResistance) lostHealth = originalHealth - currentPokemonHealth pokeImage = graphics.Image(graphics.Point(335, 90), currentPokemon.getImage()) greenHealthBar = graphics.Rectangle( graphics.Point(100, 50), graphics.Point(100 + currentPokemonHealth, 60)) redHealthBar = graphics.Rectangle( graphics.Point(100 + currentPokemonHealth, 50), graphics.Point(100 + currentPokemonHealth + lostHealth, 60)) healthDisplay = graphics.Text(graphics.Point(150, 30), healthInfo) greenHealthBar.setFill("green") healthDisplay.setTextColor("white") redHealthBar.setFill("red") currentPokemon.setDisplay(pokeImage) currentPokemon.setGreenHealthBar(greenHealthBar) currentPokemon.setHealthText(healthDisplay) currentPokemon.setRedHealthBar(redHealthBar) currentPokemon.getDisplay().draw(window) currentPokemon.getGreenHealthBar().draw(window) currentPokemon.getHealthText().draw(window) currentPokemon.getRedHealthBar().draw(window) pokedex = currentPokemon.getInfo() print("The pokedex for computer's current Pokemon is:", pokedex, "\n ==============") enter = input("Press enter to continue")
def get_thrust(self, difficulty): """waits for a user's mouse click, depending on which button is hit (thrust/no thrust) returns 1 or 0 """ # user clicks point = self.win.getMouse() while self.thrust1.is_clicked(point) == False and \ self.thrust2.is_clicked(point) == False and \ self.thrust3.is_clicked(point) == False and \ self.no_thrust.is_clicked(point) == False: warning = graphics.Text(graphics.Point(150, 100), 'please '\ 'click either thrust or no thrust') warning.draw(self.win) point = self.win.getMouse() if difficulty == 1: if self.thrust1.is_clicked(point) == True: return 1 elif self.thrust2.is_clicked(point) == True: return 3 elif self.thrust3.is_clicked(point) == True: return 5 elif self.no_thrust.is_clicked(point) == True: return 0 elif difficulty == 2: if self.thrust1.is_clicked(point) == True: return 1 elif self.thrust2.is_clicked(point) == True: return 5 elif self.thrust3.is_clicked(point) == True: return 10 elif self.no_thrust.is_clicked(point) == True: return 0 else: self.thrust2.undraw() self.thrust3.undraw() if self.thrust1.is_clicked(point) == True: return 10 elif self.no_thrust.is_clicked(point) == True: return 0
def resigning(self): global curNode global alphaBoard self.turnText.undraw() start_state = game1.new_game() curNode = Node(start_state, None) alphaBoard.init_board(0) if self.color == 1: color = "Black" else: color = "White" resignText = graphics.Text(graphics.Point(230, -10), color + " Resigned :( \n" + "Better Luck Next Time!") resignText.setSize(36) resignText.setTextColor(color) resignText.setFace("helvetica") resignText.draw(self.board.win) click = self.board.win.getMouse() self.board.win.close() main() return "done"
def h_turn(self, p1, p2): '''This method notify the human player that it is his or her turn to take actions and display current finger status p1 - hand instance of player 1(human) p2 - hand instance of player 2(computer) ''' # the undraw makes it so the objects do not write over each other but actually # erases before drawing again a different instance of the same kind. self.undraw() #this actually draws the text shown in the middle of the window self.h_turn_text = graphics.Text(graphics.Point(330, 375), "Human's Turn") self.h_turn_text.setStyle("bold") self.h_turn_text.setTextColor("Maroon") self.h_turn_text.setSize(15) self.h_turn_text.draw(self.win) human_turn = self.hand_status(p1, p2)
def __init__(self): self.win = g.GraphWin("Longest and Shortest in the Bible", 600, 600) self.win.setCoords(1, 15, 14, 0) self.quitButton = g.Button("Quit", g.Point(2, 1), g.Point(7, 2)) self.quitButton.activate() self.quitButton.draw(self.win) self.mainButton = g.Button("Main Menu", g.Point(8, 1), g.Point(13, 2)) self.mainButton.activate() self.mainButton.draw(self.win) self.introText = g.Text(g.Point(7.5, 3), "Bible Counter") self.introText.setStyle("bold") self.introText.draw(self.win) text1 = g.Text(g.Point(7.5, 4), "Welcome") text2 = g.Text(g.Point(7.5, 5), "This program finds the chapter, book, or verse") text3 = g.Text(g.Point(7.5, 6), "With the most or least chapters, words, characters, or verses") text4 = g.Text(g.Point(7.5, 7), "") text5 = g.Text(g.Point(7.5, 8), "") self.textboxes = [text1, text2, text3, text4, text5] for x in self.textboxes: time.sleep(1) x.draw(self.win) time.sleep(1) self.verseButton = g.Button("Verses", g.Point(2, 9), g.Point(5, 10)) self.verseButton.draw(self.win) self.chapterButton = g.Button("Chapters", g.Point(6, 9), g.Point(9, 10)) self.chapterButton.draw(self.win) self.bookButton = g.Button("Books", g.Point(10, 9), g.Point(13, 10)) self.bookButton.draw(self.win) opt1 = g.Button("Most\nChapters", g.Point(2, 11), g.Point(4, 12)) opt3 = g.Button("Most\nVerses", g.Point(5, 11), g.Point(7, 12)) opt5 = g.Button("Most\nWords", g.Point(8, 11), g.Point(10, 12)) opt7 = g.Button("Most\nCharacters", g.Point(11, 11), g.Point(13, 12)) opt2 = g.Button("Least\nChapters", g.Point(2, 13), g.Point(4, 14)) opt4 = g.Button("Least\nVerses", g.Point(5, 13), g.Point(7, 14)) opt6 = g.Button("Least\nWords", g.Point(8, 13), g.Point(10, 14)) opt8 = g.Button("Least\nCharacters", g.Point(11, 13), g.Point(13, 14)) self.options = [opt1, opt2, opt3, opt4, opt5, opt6, opt7, opt8] for opt in self.options: opt.draw(self.win) time.sleep(1) self.mainMenu()
def drawChessmanAI(self, row, col, color=1): dist_x = (self.board_edge_[1] - self.board_edge_[0]) / self.n_col_ dist_y = (self.board_edge_[3] - self.board_edge_[2]) / self.n_row_ radius_chessman = np.sqrt(dist_x**2 + dist_y**2) * 0.35 p_center = graph.Point(self.x_pos_[col], self.y_pos_[row]) circle = graph.Circle(p_center, radius_chessman) self.position_[row][col] = color if color == 1: circle.setFill('black') circle.setOutline('black') else: circle.setFill('white') circle.setOutline('white') circle.draw(self.win_) text = graph.Text(p_center, "%d" % self.round_) self.round_ += 1 if color == 1: text.setTextColor('white') text.draw(self.win_) return row, col, color
def reveal(self, cell_to_reveal): '''Reveals a cells number value or a bomb Parameters: cell_to_reveal: the cell to reveal bomb or number ''' #if the cell is invalid, end function if cell_to_reveal == None: return #if the cell is a bomb, explode the cell and stop the function if self.cell_nums[cell_to_reveal] == '': animations.bombs(self.win, cell_to_reveal, self.difficult_level) self.bomb = True self.exploded_bomb = cell_to_reveal return #draws the number of surrounding bombs according to the self.cell_nums dictionary value text = graphics.Text(graphics.Point(cell_to_reveal[0],cell_to_reveal[1]), \ self.cell_nums[cell_to_reveal]) text.setFill('black') text.draw(self.win) #adds cell to revealed cell list self.revealed_cells.append(cell_to_reveal) #generates a list of neighbors depending on difficulty level a = self.difficult_level * 10 neighbors = ((-a, a), (0, a), (a, a), (a, 0), (a, -a), (0, -a), (-a, -a), (-a, 0)) if self.cell_nums[ cell_to_reveal] == 0: #if the cell has zero bombs around it for neigh in neighbors: #iterate over the neighbors if ((cell_to_reveal[0] + neigh[0]) > 10) and \ ((cell_to_reveal[0] + neigh[0]) < 690) and \ ((cell_to_reveal[1] + neigh[1]) > 10) and \ ((cell_to_reveal[1] + neigh[1]) < 690): #if the neighbor is in bounds if ((cell_to_reveal[0] + neigh[0]), (cell_to_reveal[1] + neigh[1]))\ not in self.revealed_cells: #if the neighboring cell is not already revealed self.reveal((cell_to_reveal[0] + neigh[0], cell_to_reveal[1] + neigh[1]))
def ch4Ex8(): import math #Make window lineWin = graphics.GraphWin("Draw a line!", 600, 600) lineWin.setCoords(0, 0, 600, 600) lineWin.setBackground("green") #Prompt clicks message = graphics.Text(graphics.Point(300, 550), "Click anywhere to start the line") message.draw(lineWin) pt1 = lineWin.getMouse() point1 = graphics.Point(pt1.getX(), pt1.getY()) point1.draw(lineWin) message.setText("Click again to finish the line") pt2 = lineWin.getMouse() message.undraw() #Draw line newline = graphics.Line(pt1, pt2) newline.draw(lineWin) #Draw midpoint in cyan midpoint = graphics.Point((pt2.getX() + pt1.getX()) / 2, (pt2.getY() + pt1.getY()) / 2) midpoint.draw(lineWin) midpoint.setFill("cyan") #find the length and slope of line(segment) dx = pt2.getX() - pt1.getX() dy = pt2.getY() - pt1.getY() slope = dy / dx length = math.sqrt(dx**2 + dy**2) #Print line data message.setText("Length: " + str(round(length, 2)) + "\nSlope: " + str(round(slope, 2)) + "\n\nClick anywhere to quit") message.draw(lineWin) lineWin.getMouse() lineWin.close()
def __init__(self, win, center, width, height, color, label): """ Creates a rectangular button, eg: qb = Button(myWin, Point(30,25), 20, 10, 'Quit') """ self.color = color self.outline = 'black' w,h = width/2.0, height/2.0 x,y = center.getX(), center.getY() self.xmax, self.xmin = x+w, x-w self.ymax, self.ymin = y+h, y-h p1 = graphics.Point(self.xmin, self.ymin) p2 = graphics.Point(self.xmax, self.ymax) self.rect = graphics.Rectangle(p1,p2) self.rect.draw(win) self.label = graphics.Text(center, label) self.label.draw(win) self.activate()
def __generate_loading_screen(self, match_id: int): self.__loading_screen = gfx.GraphWin( title = "Loading...", width = self.__LOADING_SCREEN_X_PX, height = self.__LOADING_SCREEN_Y_PX ) text_obj = gfx.Text( gfx.Point( 0.5 * self.__LOADING_SCREEN_X_PX, 0.5 * self.__LOADING_SCREEN_Y_PX ), "Loading Match {}".format(match_id) ) text_obj.setSize(20) text_obj.setStyle('bold') text_obj.draw(self.__loading_screen)
def __init__(self, win, center, radius, color, label): """ Creates a circular ring, eg: qb = Button(myWin, Point(30,25), 20, 'blue', 'Quit') """ self.color = color self.outline = 'black' self.center = center self.x, self.y = self.center.getX(), self.center.getY() self.radius = radius self.circle = graphics.Circle(self.center, radius) self.circle.draw(win) self.label = label self.text = graphics.Text(self.center, self.label) self.text.setSize(8) self.text.draw(win) self.activate()