def display(self, screen: pygame.display) -> None: """Display the current Connect Four Board on screen""" w, h = screen.get_size() screen.fill((0, 0, 255)) # Draw the lines on the board for i in range(1, self.n): pygame.draw.line(screen, (0, 0, 0), (0, h * i // self.n), (w, h * i // self.n)) pygame.draw.line(screen, (0, 0, 0), (w * i // self.n, 0), (w * i // self.n, h)) # Draw the markers for x in range(self.n): for y in range(self.n): if self.board[x][y] == 1: color = (255, 0, 0) elif self.board[x][y] == 0: color = (255, 255, 0) else: color = (255, 255, 255) pygame.draw.circle(screen, color, ((y + 0.5) * (w // self.n), (x + 0.5) * (h // self.n)), h // (3 * self.n)) pygame.display.update()
def display(self, screen: pygame.display) -> None: """Display the current TicTacToe Board on screen""" w, h = screen.get_size() screen.fill((255, 255, 255)) # Draw the lines on the board pygame.draw.line(screen, (0, 0, 0), (0, h // 3), (w, h // 3)) pygame.draw.line(screen, (0, 0, 0), (0, 2 * h // 3), (w, 2 * h // 3)) pygame.draw.line(screen, (0, 0, 0), (w // 3, 0), (w // 3, h)) pygame.draw.line(screen, (0, 0, 0), (2 * w // 3, 0), (2 * w // 3, h)) # Draw the markers font = pygame.font.SysFont('Calibri', 100) for x in range(3): for y in range(3): piece = font.render( self.board_object(x, y), True, (0, 0, 0) ) screen.blit( piece, ( (y + 0.3) * (w // 3), (x + 0.3) * (h // 3) ) ) pygame.display.update()
def show_message(screen: pygame.display, msg: str) -> None: screen.fill(colors.BLACK) font = pygame.font.FontType('freesansbold.ttf', 20) surface = font.render(msg, True, colors.WHITE) rectangle = surface.get_rect() rectangle.center = (width // 2, height // 2) screen.blit(surface, rectangle) pygame.display.update()
def in_room(screen: pygame.display, clock: pygame.time.Clock, my_acc: Account, accounts: List[Account], room: Room) -> None: ViewHandler.clear_views() is_in_room = True sent_ready = False def game_listener(view): nonlocal is_in_room nonlocal sent_ready if not sent_ready: n.send("ready") print("Sent ready") sent_ready = True play = Button(WIDTH / 2 - 150, 50, 300) \ .set_text("Ready") \ .set_on_click_listener(game_listener) \ .set_on_hover_listener(on_hover) \ .set_on_unhover_listener(on_unhover) my_textview = TextView(50, 150, 200).set_text(my_acc.username) accounts_display = [] # type: List[TextView] for i, acc in enumerate(accounts): # type: int, Account accounts_display.append(TextView(WIDTH / 2, 200 + (i * 50), 300) .set_text(acc.username)) while is_in_room: data = n.receive() if data: if type(data) == Account: new_account = data print("got another acc") accounts.append(new_account) accounts_display.append(TextView(WIDTH / 2, 200 + ((len(accounts) - 1) * 50), 300) .set_text(new_account.username)) elif data == "ready": print("Got ready") room.running = True is_in_room = False else: print("-----------Weird data!!!!:", data) screen.fill(BACKGROUND_COLOR) events = pygame.event.get() for event in events: if event.type == pygame.QUIT: is_in_room = False pygame.quit() break ViewHandler.handle_view_events(events) ViewHandler.render_views(screen) pygame.display.update() clock.tick(60)
def _draw_mini_map(self, display_surf: pygame.display): """Initialize the minimap in appropriate location Args: display_surf: The main game display """ for y in range(572, 772, 4): for x in range(828, 1028, 4): display_surf.fill((0, 0, 0), (x, y, 4, 4))
def draw_war_place(layer: pygame.display) -> None: """Отрисовка поля основной игры. :return: None. """ layer.fill(0) layer.blit( pygame.image.load("Picture/Back_ground_fight.jpg").convert(), (0, 0)) characters.update(int(player.get_coins_count())) characters.draw(layer) snaps.update(player.has_finish_step) snaps.draw(layer) for btn in buttons: btn.draw(layer)
def draw(window: pygame.display, grid: list, rows, width): """Draws everything on the surface. Args: window (pygame.display): Main surface window. grid (list): Grid list containing all the nodes. rows ([type]): Number of rows. width ([type]): Width of the surface. """ window.fill(WHITE) for row in grid: for node in row: node.draw(window) draw_grid_lines(window, rows, width) pygame.display.update()
def draw_nodes_in_grid(surface: pygame.display, grid: list, total_rows: int, surface_width: int) -> None: """Draw the nodes and the grid (lines separating the nodes) on the given surface. :param surface: surface to draw on :param grid: grid containing the nodes :param total_rows: total number of rows; is also the number of nodes per row :param surface_width: width of the surface """ surface.fill(colors["white"]) for row in grid: for node in row: node.draw_node(surface) draw_grid_lines(surface, total_rows, surface_width) pygame.display.update()
def redraw_window(win: pygame.display, board: list, time: int, strikes: int): """Redraws window. Args: win (pygame.display): Main surface window. board (list): Sudoku board. time (int): Time elapsed. strikes (int): Number of strikes (wrong attempts). """ win.fill((255, 255, 255)) # Draw time fnt = pygame.font.SysFont("comicsans", 40) text = fnt.render("Time: " + format_time(time), 1, (0, 0, 0)) win.blit(text, (540 - 160, 560)) # Draw Strikes text = fnt.render("X " * strikes, 1, (255, 0, 0)) win.blit(text, (20, 560)) # Draw grid and board board.draw(win)
def render_grid(self, screen: pygame.display, grid: Grid) -> None: """ Draw the grid on a pygame window. """ # Reset the screen to black screen.fill((0,0,0)) nodes = grid.get_nodes() # NOTE: Render only the bottom 20. # This is so new blocks can be spawned # out of the player's view. for line in nodes[4:]: for node in line: pos = node.get_position() colour = node.get_colour() length = node.get_length() pygame.draw.rect(screen, colour, (pos[0], pos[1], length, length)) pygame.display.update()
def draw_board(dis: pygame.display, size: int, my_snake: Snake, my_snack: Snack): dis.fill((30, 32, 40)) # dark blue snake_pos_list = my_snake.get_pos() snack_pos = my_snack.get_pos() increment = min(display.get_size()) / size for y in range(size): for x in range(size): tile_size = increment - 2 tile_pos = (x * increment, (-y + size - 1) * increment) tile = Tile(dis, tile_size, tile_pos) if (x, y) in snake_pos_list: tile.change_state('SNAKE') elif (x, y) == snack_pos: tile.change_state('SNACK') else: tile.change_state('BLANK') tile.draw() pygame.display.update()
def win_animation(self, display_surf: pygame.display, player: Sprite): """Preform a win animation sequence. Args: display_surf: The main game display. player: The Sprite object rendering the player """ pygame.event.pump() display_surf.blit(self.images['door'], (player.pos[0], player.pos[1])) myfont = pygame.font.Font('maze_pro/assets/fonts/breathe_fire.otf', 50) congrats = myfont.render("Congratulations!", True, (0, 0, 0)) display_surf.fill((100, 255, 50), (200, 380, 400, 80)) display_surf.blit(congrats, (250, 400)) pygame.display.flip() tick = time.time() while True: pygame.event.wait() if time.time() - tick > 5.0: break
def main(window: pygame.display, window_width: int) -> None: """Main visualiser loop. :param window: pygame window :param window_width: width of the window; is also the height since it's a square """ grid = create_grid(NUM_ROWS, window_width) # List of the nodes start_node = None end_node = None run = True while run: draw_nodes_in_grid(window, grid, NUM_ROWS, window_width) for event in pygame.event.get(): # Ends game if the user presses the exit button. if event.type == pygame.QUIT: run = False # User pressed left mouse button elif pygame.mouse.get_pressed()[0]: mouse_click_position = pygame.mouse.get_pos() x, y = get_clicked_position(mouse_click_position, NUM_ROWS, window_width) clicked_node = grid[x][y] # Start node not set; doesn't overwrite the end node if not start_node and clicked_node != end_node: start_node = clicked_node start_node.make_start() # End node not set; doesn't overwrite the start node elif not end_node and clicked_node != start_node: end_node = clicked_node end_node.make_end() # Both start and end node set, clicked node will become a barrier elif clicked_node != start_node and clicked_node != end_node: clicked_node.make_barrier() # User pressed right mouse button elif pygame.mouse.get_pressed()[2]: mouse_click_position = pygame.mouse.get_pos() x, y = get_clicked_position(mouse_click_position, NUM_ROWS, window_width) clicked_node = grid[x][y] clicked_node.reset() # User wants to reset the start node if clicked_node == start_node: start_node = None # User wants to reset the end node elif clicked_node == end_node: end_node = None # Key presses elif event.type == pygame.KEYDOWN: # Starts the path generation and visualisation if the key 'SPACE' is pressed if event.key == pygame.K_SPACE: for row in grid: for node in row: # Resets every node, except barriers and the end points. if not node.is_barrier() and not node.is_end( ) and not node.is_start(): node.reset() node.update_neighbors( grid) # Marks neighbors for visit # Starts the path finding and visualisation generate_path( lambda: draw_nodes_in_grid(GAME_WINDOW, grid, NUM_ROWS, SCREEN_WIDTH), grid, start_node, end_node) # Resets every node if the key 'R' is pressed elif event.key == pygame.K_r: window.fill(colors["white"]) for row in grid: for node in row: node.reset() start_node = None end_node = None pygame.quit()
def show(self, w: pygame.display): u"""Nariši crn pravokotnik na self.x, self.y""" w.fill(colors.BLACK, (self.x, self.y, self.sx, self.sy))