def debug_window(): from graphics import GraphWin win = GraphWin('Floor', SCREEN_WIDTH_PX, SCREEN_HEIGHT_PX) win.setBackground("black") for circle in circle_grid(): for [x, y] in circle.points(factor=1.): win.plotPixel(x, y, "white") # for circle in circle_grid(): # for [x, y] in circle.points(factor=2.): # win.plotPixel(x, y, "white") for donut_slice in donut_grid(): for [x, y] in donut_slice: win.plotPixel(x, y, "white") sleep(0.001) # gaze = RandomPoint.pick() # stimulus = random.choice(STIMULI_COORDENATES) # for i in np.arange(0,1000,0.01): # gaze = gaze.add(gaze.pick(r=1)) # x, y = (gaze.x*10)+stimulus[0]+50, (gaze.y*6)+stimulus[1]+50 # win.plotPixel(x, y, "white") # if x > stimulus[0]+100 or x < stimulus[0]: # gaze = RandomPoint.pick() # stimulus = random.choice(STIMULI_COORDENATES) # if y > stimulus[1]+100 or y < stimulus[1]: # gaze = RandomPoint.pick() # stimulus = random.choice(STIMULI_COORDENATES) win.getKey() win.close()
def main(): win = GraphWin ( "Click and Type", 400, 400) for i in range(10): pt = win.getMouse() key = win.getKey() label = Text (pt, key) label.draw(win)
def main(): global win win = GraphWin("Practica 1 - Robot", tablero.cols * tablero.cell_size, tablero.rows * tablero.cell_size + 50) win.setBackground(color_rgb(192, 192, 192)) tablero.draw(win) butStart.draw(win) butWalls.draw(win) butRobot.draw(win) butClear.draw(win) win.bind("<Button-1>", mousepressHandler) win.bind("<B1-Motion>", mousedragHandler) while True: try: _ = win.getKey() except: break
class Game: __y_offset = 30 def __init__(self, num_players: int, pairs: int): self.win = GraphWin('Matching Game', WIN_SIZE, WIN_SIZE) self.__reset_num_players = num_players self.__reset_pairs = pairs self.reset() def reset(self): self.__state = State(self.__reset_num_players, self.__reset_pairs) colors = set() for _ in range(self.__reset_pairs): c = random_color() while c in colors: c = random_color() colors.add(c) self.__color_map = {n: c for n, c in enumerate(colors)} rect = Rectangle(Point(0, 0), Point(self.win.width, self.win.height)) rect.draw(self.win) def loop(self): while True: self.draw() if self.__state.all_matched(): break pos = self.wait_click_pos() if pos is None: continue r, c = pos did_select = self.__state.select_card(r, c) if not did_select: continue if self.__state.selection_full(): self.draw() if self.__state.selected_match(): self.__state.match_found() else: self.__state.next_player() time.sleep(1) self.__state.clear_selected() self.draw_game_over() def draw(self): y_offset = 30 rows = self.__state.board_rows() cols = self.__state.board_cols() y_size = (self.win.height - y_offset) / rows x_size = self.win.width / cols header = Rectangle(Point(0, 0), Point(self.win.width, y_offset)) header.setFill("grey") header.draw(self.win) score_spacing = self.win.width / (self.__state.num_players() + 1) for i, score in enumerate(self.__state.player_points()): score_text = Text(Point((i + 1) * score_spacing, y_offset / 2), score) if i == self.__state.current_player(): score_text.setStyle('bold') score_text.setSize(18) score_text.setTextColor("black") score_text.draw(self.win) for r in range(rows): for c in range(cols): card = self.__state.card_at(r, c) rx = c * x_size ry = r * y_size + y_offset rect = Rectangle(Point(rx, ry), Point(rx + x_size, ry + y_size)) if card is None: color = "black" else: selected = self.__state.selected() if card.matched() \ or selected[0] == (r, c) \ or selected[1] == (r, c): (color, fg) = self.__color_map[card.value()] else: color = "white" rect.setFill(color) rect.draw(self.win) 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 wait_click_pos(self) -> Optional[Tuple[int, int]]: point = self.wait_click() if point.y < self.__y_offset: return None (x_size, y_size) = self.square_dim() return (int( (point.y - self.__y_offset) / y_size), int(point.x / x_size)) def wait_click(self) -> Point: return self.win.getMouse() def wait_key(self) -> str: return self.win.getKey() def close(self): self.win.close() def square_dim(self) -> Tuple[float, float]: y = (self.win.height - self.__y_offset) / self.__state.board_rows() return (self.win.width / self.__state.board_cols(), y)