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") Text(Point(20, 230), ' 0.0K').draw(win) Text(Point(20, 180), ' 2.5K').draw(win) Text(Point(20, 130), ' 5.0K').draw(win) Text(Point(20, 80), ' 7.5K').draw(win) Text(Point(20, 30), '10.0K').draw(win) # Draw bar for initial principal height = principal * 0.02 bar = Rectangle(Point(40, 230), Point(65, 230 - height)) bar.setFill("green") bar.setWidth(2) bar.draw(win) # Draw bars for successive years for year in range(1, 11): # calculate value for the next year principal = principal * (1 + apr) # draw bar for this value xll = year * 25 + 40 height = principal * 0.02 bar = Rectangle(Point(xll, 230), Point(xll + 25, 230 - height)) bar.setFill("green") bar.setWidth(2) bar.draw(win) input(" Press <Enter> to quit ") win.close()
class Dice: 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 reroll(self): self.num = random.randint(1, 6) def swapDiscard(self): print(self.willDiscard) if (self.willDiscard): self.willDiscard = False else: self.willDiscard = True self.drawSelf() def drawSelf(self): self.img.undraw() self.text.undraw() if (self.willDiscard): self.img.setFill("red") else: self.img.setFill("white") self.img.draw(win) self.text.draw(win)
class Button: def __init__(self, win, center, width, height, label): 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 = Point(self.xmin, self.ymin) p2 = Point(self.xmax, self.ymax) self.rect = Rectangle(p1, p2) self.rect.setFill("lightgray") self.rect.draw(win) self.label = Text(center, label) self.label.draw(win) self.deactivate() def clicked(self, p): return (self.active and self.xmin <= p.getX() <= self.xmax and self.ymin <= p.getY() <= self.ymax) def getLabel(self): return self.label.getText() def activate(self): self.label.setFill("black") self.rect.setWidth(2) self.active = True def deactivate(self): self.label.setFill("darkgray") self.rect.setWidth(1) self.active = False def undraw(self): self.rect.undraw() self.label.undraw()
class Car(): WHEEL_TO_TIRE_RATIO = 0.6 def __init__(self, back_wheel_center, back_tire_radius, front_wheel_center, front_tire_radius, body_height): upper_left_point = Point(back_wheel_center.x, back_wheel_center.y - body_height) bottom_right_point = front_wheel_center self.body = Rectangle(upper_left_point, bottom_right_point) self.back_wheel = Wheel(back_wheel_center, back_tire_radius * Car.WHEEL_TO_TIRE_RATIO, back_tire_radius) self.front_wheel = Wheel(front_wheel_center, front_tire_radius * Car.WHEEL_TO_TIRE_RATIO, front_tire_radius) def set_color(self, tire_color, wheel_color, body_color): self.body.setFill(body_color) self.back_wheel.set_color(wheel_color, tire_color) self.front_wheel.set_color(wheel_color, tire_color) def draw(self, win): self.body.draw(win) self.back_wheel.draw(win) self.front_wheel.draw(win) def animate(self, win, dx, dy, n): pass
def draw_hold_button(): # hold button points hold_a = Point(WIN.getWidth() * 2 / 11 - 100, WIN.getHeight() * 3 / 12) hold_b = Point(hold_a.getX() + 200, hold_a.getY() + 100) hold_button = Rectangle(hold_a, hold_b) hold_button.setFill("green") hold_button.setOutline("black") hold_button.setWidth(5) # Obtains the center point of the hold button center_hold_button = Point(0.5 * (hold_a.getX() + hold_b.getX()), 0.5 * (hold_a.getY() + hold_b.getY())) # Text class hold_text = Text(center_hold_button, "Hold") # Style the text hold_text.setFace("helvetica") hold_text.setSize(26) hold_text.setStyle("bold") hold_text.setTextColor("black") hold_button.draw(WIN) hold_text.draw(WIN) return hold_a, hold_b
class Button: 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 activate(self): """ Sets this button to "active". """ self.label.setFill('black') self.rect.setWidth(2) self.active = True def deactivate(self): """ Sets this button to "inactive". """ self.label.setFill('darkgrey') self.rect.setWidth(1) self.active = False def clicked(self, point): """ Tests to see if the point clicked resides within the button's rectangle, and if the button is active. """ if self.active: if (self.x_min <= point.getX() and self.y_min <= point.getY() and self.x_max >= point.getX() and self.y_max >= point.getY()): return True return False def getLabel(self): return self.label.getText()
def drawScoreDisplay(game_panel): # Draws box that serves as the background the score displays score_box = Rectangle(Point(50, 155), Point(250, 240)) score_box.setFill("white") score_box.draw(game_panel) # Call readScores() function, and store top 4 scores from topscores.txt top_scores = readScores() # Print title for top scores table scores = ["TOP SCORES", "=========="] # For each of the top scores, create a string with the name and score for element in top_scores: # Convert tuple to list element = list(element) # Loop through name and score for each player for item in element: # Assign name to name if type(item) == str: name = item # Convert score to int and assign to score elif type(item) == int: score = str(item) # Create a string from the name and score with space between elements string = name + " " + str(score) # Add player to scores list scores.append(string) # Create a text object of the top scores title and players and draw in game # panel score_text = Text(Point(150, 198), "\n".join(scores[:6])) score_text.draw(game_panel) # Return objects return (score_box, score_text, scores)
class Tile: ACTIVE_COLOR = "green" 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} def location(self): return self.bl def getX(self): return self.bl[0] def getY(self): return self.bl[1] def get_moves(self, location): if self.piece_obj: return self.piece_obj.get_moves(location) return [] def set_piece(self, piece_image, piece_name): self.piece_image = piece_image self.piece_obj = PIECE_MAPPINGS[piece_name]() return self def still_active(self): return self.state["active"] def has_piece(self): return self.piece_image != None def onclick(self, window): """Handles highlighting tiles with pieces.""" if self.piece_image: self.rectangle.undraw() active = self.state["active"] color = Tile.ACTIVE_COLOR if not active else self.state[ "original_fill"] self.rectangle.setFill(color) self.rectangle.draw(window) self.piece_image.undraw() self.piece_image.draw(window) self.state["active"] = not active def highlight(self, window): if self.rectangle: self.rectangle.undraw() active = self.state["active"] color = Tile.ACTIVE_COLOR if not active else self.state[ "original_fill"] self.rectangle.setFill(color) self.rectangle.draw(window) self.state["active"] = not active
class Square: def __init__(self, x, y, size): self.col = x #Igual no hace falta self.row = y #Igual no hace falta self.size = size #Igual no hace falta self.wall = False self.square = Rectangle(Point(x*size,y*size+50), Point(x*size+size, y*size+size+50)) def toggle(self): self.wall = not self.wall def setWall(self, wall): self.wall = wall def draw(self, win): if(self.wall): self.square.setFill(color_rgb(55, 55, 55)) else: self.square.setFill(color_rgb(230,230,230)) self.square.undraw() self.square.draw(win) def pintar(self): print("x: " + str(self.col) + " y: "+ str(self.row)) def isWall(self): return self.wall
class Boton(object): def __init__(self, v, centro, ancho, alto, etiqueta): #calculo de la posicion x, y = centro.getX(), centro.getY() w, h = ancho /2, alto /2 self.xmax, self.xmin = x+w, x-w self.ymax, self.ymin = y+h, y-h p1 = Point(self.xmin, self.ymin) p2 = Point(self.xmax, self.ymax) #creacion del boton y etiqueta self.boton = Rectangle(p1, p2) self.boton.draw(v) self.boton.setFill('#CCC') self.etiqueta = Text(centro, etiqueta) self.etiqueta.setTextColor('#666') self.etiqueta.draw(v) self.desactivar() def color(self, c): self.boton.setFill(c) def pulsado(self, p): if p.getX() in range(self.xmin, self.xmax) and p.getY() in range(self.ymin, self.ymax): return True else: return False def activar(self): self.etiqueta.setTextColor('#000') self.estado = True def desactivar(self): self.etiqueta.setTextColor('#666') self.estado = False
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() : # 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 __init__(self, win, center, size): # Define some standard values self.win = win self.background = "white" self.foreground = "black" self.psize = 0.1 * size hsize = size / 2.0 offset = 0.6 * hsize # 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(self.win) rect.setFill(self.background) # Create 7 circles for standard pip locations self.pips = [self.__makePip(cx - offset, cy - offset), self.__makePip(cx - offset, cy), self.__makePip(cx - offset, cy + offset), self.__makePip(cx, cy), self.__makePip(cx + offset, cy - offset), self.__makePip(cx + offset, cy), self.__makePip(cx + offset, cy + offset)] # Create a table for which pips are on for each value self.onTable = [[], [3], [2, 4], [2, 3, 4], [0, 2, 4, 6], [0, 2, 3, 4, 6], [0, 1, 2, 4, 5, 6]] # Draw an initial value self.setValue(1)
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 get_image_path(): width, height = 300, 150 win = GraphWin("Image Path", width, height) win.setBackground(color_rgb(0, 0, 0)) path_entry = Entry(Point(width // 2, height // 2 - 50), 30) path_entry.draw(win) ok_button = Rectangle(Point(width - 50, height - 50), Point(width - 5, height - 5)) ok_button.setFill(color_rgb(255, 255, 255)) ok_button.draw(win) ok_button_text = Text(ok_button.getCenter(), "OK") ok_button_text.setSize(15) ok_button_text.draw(win) while True: click = win.getMouse() clickx = click.getX() clicky = click.getY() if ok_button.getP1().getX() <= clickx <= ok_button.getP2().getX( ) and ok_button.getP1().getY() <= clicky <= ok_button.getP2().getY(): win.close() return str(path_entry.getText())
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 __init__(self, win, center, size): self.win = win self.background = 'white' self.foreground = 'black' self.pip_size = 0.1 * size half_size = size / 2.0 offset = 0.6 * half_size #create square die face x, y = center.getX(), center.getY() point1 = Point(x - half_size, y - half_size) point2 = Point(x + half_size, y + half_size) rect = Rectangle(point1, point2) rect.setFill(self.background) rect.draw(win) #create 7 possible pip designs self.pip1 = self.__makePip(x - offset, y - offset) self.pip2 = self.__makePip(x - offset, y) self.pip3 = self.__makePip(x - offset, y + offset) self.pip4 = self.__makePip(x, y) self.pip5 = self.__makePip(x + offset, y - offset) self.pip6 = self.__makePip(x + offset, y) self.pip7 = self.__makePip(x + offset, y + offset) self.drawValue(1)
class Button(object): def __init__(self, win, center, width, height, label): w, h = width/2, height/2 x, y = center.getX(), center.getY() self.xmax, self.xmin = x+w, x-w self.ymax, self.ymin = y+h, y-h p1 = Point(self.xmin, self.ymin) p2 = Point(self.xmax, self.ymax) self.rect = Rectangle(p1, p2) self.rect.setFill('lightgray') self.rect.draw(win) self.label = Text(center, label) self.label.draw(win) self.deactivate() def clicked(self, p): return self.active and self.xmin <= p.getX() and self.xmax >= p.getX() and self.ymin <= p.getY() and self.ymax >= p.getY() def getLabel(self): return self.label.getText() def activate(self): self.label.setFill('black') self.rect.setWidth(2) self.active = True def deactivate(self): self.label.setFill('darkgrey') self.rect.setWidth(1) self.active = False
def getGFX(self): gfx = Rectangle( Point(self._size * self._pos[0], self._size * self._pos[1]), Point(self._size * self._pos[0] + self._size, self._size * self._pos[1] + self._size)) gfx.setFill(self._color) self._gfx = gfx
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 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 _draw_rect(self, x, y, color): pos_x = x * self.cell_width pos_y = y * self.cell_height rect = Rectangle( Point(pos_x, pos_y), Point(pos_x + self.cell_width, pos_y + self.cell_height)) rect.setFill(color) rect.draw(self.window) return rect
def partial(i, j, color): print(n, size, i, j, color) one = Point(i * size, j * size) two = Point(size * (i + 1), size * (j + 1)) rect = Rectangle(one, two) rect.setFill(color) rect.setOutline(color) rect.draw(win) return rect
def drawResetButton(game_panel): # Creates yellow reset button to create new game with same player reset_button = Rectangle(Point(0, 260), Point(50, 300)) reset_button.setFill("yellow") reset_button.draw(game_panel) reset_text = Text(Point(25, 281), "RESET") reset_text.setSize(14) reset_text.draw(game_panel) # Return objects return reset_button, reset_text
class Rectangulo_centro(object):#clase para crear rectangulos centrados def __init__(self, ventana): ancho = ventana.width / 2 alto = ventana.height / 2 p1 = Point(ancho - 20, alto - 10) p2 = Point(ancho + 20, alto + 10) self.r = Rectangle(p1, p2) self.r.draw(ventana) def color(self, c): self.r.setFill(c)
def __init__(self, *objects): self.root = tk.Tk() ## self.window = GraphWin('quad', self.root.winfo.screenwidth()/2, self.root.winfo.screenheight/2) self.window = GraphWin('quad', 1920, 1080) for obj in objects: rect = Rectangle( (obj['x'] + obj['width'] / 2, obj['y'] + obj['height'] / 2), obj['width'], obj['height']) rect.setFill('black') rect.draw(self.window)
def draw_rec(board_column, board_row, color, dot_size): rtl = (board_column * (size * 2)) + offsetC rtr = (board_row * (size * 2)) + offsetR rbl = (board_column * (size * 2) + offsetC + (dot_size * 2)) rbr = (board_row * (size * 2) + offsetR + (dot_size * 2)) head = Rectangle(Point(rtl, rtr), Point(rbl, rbr)) head.setFill(color) head.setOutline(color) head.draw(win)
def gen_rectangle(o, x, y, l, color='black', dx=100, dy=100): p1 = Point(x * dx, y * dy) if o == 0: # horizontal p2 = Point(x * dx + dx * l, (1 + y) * dy) else: p2 = Point((1 + x) * dx, y * dy + dy * l) rectangle = Rectangle(p1, p2) rectangle.setFill(color) return rectangle
def _initialize(self): self.cells = self.blank_field(0) self.recs = self.blank_field(None) for x, y in self.allpos(): rec = \ Rectangle( Point(x * self.cell_size, y * self.cell_size), Point((x + 1) * self.cell_size, (y + 1) * self.cell_size)) rec.setFill('white') rec.draw(self.window) self.recs[y][x] = rec
def displaygraphicalgrid(win, grid, scale): for i in range(0, len(grid)): for j in range(0, len(grid[i])): Rect = Rectangle(Point(i * scale, j * scale), Point((i + 1) * scale - 1, (j + 1) * scale - 1)) if grid[i][j].solid: Rect.setFill("white") Rect.setOutline("white") else: Rect.setFill("black") Rect.setOutline("black") Rect.draw(win)
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 button(ll_point, ur_point, text, color="white"): but = Rectangle(Point(ll_point[0], ll_point[1]), Point(ur_point[0], ur_point[1])) # points are ordered ll, ur but.setFill(color) text = Text( Point((ll_point[0] + ur_point[0]) // 2, (ll_point[1] + ur_point[1]) // 2), text) but.draw(win) text.draw(win) return but, text
def main(): win = graphics.GraphWin() shape = Rectangle(Point(75,75),Point(125,125)) shape.setOutline('Red') shape.setFill('Red') shape.draw(win) for i in range(10): p = win.getMouse() tx = p.getX()-25 ty = p.getY()-25 bx = p.getX()+25 by = p.getY()+25 Rectangle(Point(tx,ty),Point(bx,by)).draw(win) Text(Point(100,180),'Click again to quit!').draw(win) win.getMouse() win.close()
def main(): win = graphics.GraphWin('Investment Grow Chart',320,240) win.setBackground('White') win.setCoords(-1.75,-9000,11.5,15400) Text(Point(4.87,13500),'Future Value Calculator').draw(win) Text(Point(4.5,-2000),'Input first principal:').draw(win) inputP = Entry(Point(9,-2000),6) inputP.setText('0') inputP.draw(win) Text(Point(3,-4000),' Input interest rate in decimal:').draw(win) inputR = Entry(Point(9,-4000),6) inputR.setText('0.0') inputR.draw(win) button = Rectangle(Point(2,-6000),Point(8,-8500)) button.setFill('Green') button.draw(win) buttontext = Text(Point(5,-7250),'Show Result!:') buttontext.draw(win) win.getMouse() p = eval(inputP.getText()) r = eval(inputR.getText()) Text(Point(-1,0),'0 K').draw(win) Text(Point(-1,2500),'2.5K').draw(win) Text(Point(-1,5000),'5 K').draw(win) Text(Point(-1,7500),'7.5K').draw(win) Text(Point(-1,10000),'10 K').draw(win) bar = Rectangle(Point(0,0),Point(1,p)) bar.setFill('green') bar.setWidth(2) bar.draw(win) for i in range(1,11): p = p*(1+r) bar= Rectangle(Point(i,0),Point(i+1,p)) bar.setFill('Green') bar.setWidth(2) bar.draw(win) buttontext.setText('Quit.') win.getMouse() win.close()
points = [[Point(left_marg + c * dim//size, top_marg + r * dim//size) for c in range(size + 1)] for r in range(size + 1)] dots = [[Circle(p, 5) for p in l] for l in points] for l in dots: for c in l: c.setFill('black') c.draw(win) printer(win, results_dir, f, i, 0) for j, (player, entry) in enumerate(game['history'], start=1): # Box finished is_vert, r, c = entry r = int(r) c = int(c) if is_vert == '': rec = Rectangle(points[r][c], points[r+1][c+1]) rec.setFill(colors[player]) rec.draw(win) scores[player] += 1 score_text[player].undraw() score_text[player].setText('{}:{}'.format(wins[player], scores[player])) score_text[player].draw(win) else: if is_vert: line = Line(points[r][c], points[r+1][c]) else: line = Line(points[r][c], points[r][c+1]) line.draw(win) line.setFill(colors[player]) printer(win, results_dir, f, i, j) (n1, s1), (n2, s2) = scores.items()
class Dado(object): def __init__(self, v, centro, ancho, alto): x, y = centro.getX(), centro.getY() w, h = ancho /2, alto /2 self.ventana = v self.xmax, self.xmin = x+w, x-w self.ymax, self.ymin = y+h, y-h p1 = Point(self.xmin, self.ymin) p2 = Point(self.xmax, self.ymax) self.dado = Rectangle(p1, p2) self.dado.draw(v) self.dado.setFill('#FF0000') '''pintamos los circulos del dado con las posiciones reescalables de los puntos pos1 pos5 pos2 pos4 pos6 pos3 pos7 ''' self.pos1 = Point(self.xmin + w /2, self.ymin + h /2) self.pos2 = Point(self.xmin + w / 2, self.ymin + h) self.pos3 = Point(self.xmin + w / 2, self.ymin + h * 1.5) self.pos4 = Point(self.xmin + w, self.ymin + h) self.pos5 = Point(self.xmin + w * 1.5, self.ymin + h /2) self.pos6 = Point(self.xmin + w * 1.5, self.ymin + h) self.pos7 = Point(self.xmin + w * 1.5, self.ymin + h * 1.5) self.c1 = Circle(self.pos1, 4) self.c1.setOutline('#fff') self.c1.setFill('#fff') self.c1.draw(self.ventana) self.c2 = Circle(self.pos2, 4) self.c2.setOutline('#fff') self.c2.setFill('#fff') self.c2.draw(self.ventana) self.c3 = Circle(self.pos3, 4) self.c3.setOutline('#fff') self.c3.setFill('#fff') self.c3.draw(self.ventana) self.c4 = Circle(self.pos4, 4) self.c4.setOutline('#fff') self.c4.setFill('#fff') self.c4.draw(self.ventana) self.c5 = Circle(self.pos5, 4) self.c5.setOutline('#fff') self.c5.setFill('#fff') self.c5.draw(self.ventana) self.c6 = Circle(self.pos6, 4) self.c6.setOutline('#fff') self.c6.setFill('#fff') self.c6.draw(self.ventana) self.c7 = Circle(self.pos7, 4) self.c7.setOutline('#fff') self.c7.setFill('#fff') self.c7.draw(self.ventana) self.limpiar() def colorDado(self, c):#personalizar el dado self.dado.setFill(c) def colorPunto(self, c):#personalizar los puntos del dado self.c1.setOutline(c) self.c1.setFill(c) self.c2.setOutline(c) self.c2.setFill(c) self.c3.setOutline(c) self.c3.setFill(c) self.c4.setOutline(c) self.c4.setFill(c) self.c5.setOutline(c) self.c5.setFill(c) self.c6.setOutline(c) self.c6.setFill(c) self.c7.setOutline(c) self.c7.setFill(c) def pulsado(self, p):#funcion para saber si se ha pulsado if p.getX() in range(self.xmin, self.xmax) and p.getY() in range(self.ymin, self.ymax): return True else: return False def ponValor(self, valor=1):#funcion para establecer la cara visible del dado self.limpiar() if valor == 1: self.c4.draw(self.ventana) elif valor == 2: self.c1.draw(self.ventana) self.c7.draw(self.ventana) elif valor == 3: self.c1.draw(self.ventana) self.c4.draw(self.ventana) self.c7.draw(self.ventana) elif valor == 4: self.c1.draw(self.ventana) self.c3.draw(self.ventana) self.c5.draw(self.ventana) self.c7.draw(self.ventana) elif valor == 5: self.c1.draw(self.ventana) self.c3.draw(self.ventana) self.c4.draw(self.ventana) self.c5.draw(self.ventana) self.c7.draw(self.ventana) elif valor == 6: self.c1.draw(self.ventana) self.c2.draw(self.ventana) self.c3.draw(self.ventana) self.c5.draw(self.ventana) self.c6.draw(self.ventana) self.c7.draw(self.ventana) def limpiar(self):#limpiar el dado antes de pintar self.c1.undraw() self.c2.undraw() self.c3.undraw() self.c4.undraw() self.c5.undraw() self.c6.undraw() self.c7.undraw() def tirarDado(self):#funcion para visualizar aleatoriamente una cara posibles = [1, 2, 3, 4, 5, 6] num = choice(posibles) self.ponValor(num) return num