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 render(self, display: pygame.display, target: list[type[int, int, int]], launcher: tuple[int, int, int], ps_t: int, first_time: bool) -> NoReturn: if first_time: sound_manager.start_in_first_empty_taunt(self.sound) self.random_vec = [(random.uniform(-1, 1), random.uniform(-1, 1), random.randint(0, 100), random.randint(300, 400)) for _ in range(10)] for t in target: if 150 < ps_t < 600: x_start, y_start = t[0] + 90, t[1] - 150 edit = (ps_t - 150) / 600 * 80 display.blit( self.cut, (x_start - self.cut.get_size()[0] - edit, y_start + edit)) if ps_t > 250: ps_t -= 250 x_start, y_start = t[0], t[1] - 40 for bu in self.random_vec: vec = [bu[0], bu[1]] if 0 < (n_pst_t := ps_t - bu[2]) < bu[3]: adv = (n_pst_t / bu[3]) vec[0] *= adv * 150 vec[1] *= adv * 150 display.blit( self.ball, (x_start - 16 + vec[0], y_start - 16 + vec[1]))
def render(self, display: pygame.display, target: list[type[int, int, int]], launcher: tuple[int, int, int], ps_t: int, first_time: bool) -> NoReturn: if first_time: sound_manager.start_in_first_empty_taunt(self.sound) self.random_vec = [ (random.uniform(-2, 2), random.uniform(-2, 2), random.randint(0, 1000), random.randint(400, 1000)) for _ in range(20)] for t in target: if ps_t < 1000: x1, y1 = launcher[0] + 40, launcher[1] - 50 x2, y2 = t[0], t[1] - 30 a = (y2 - y1) / (x2 - x1) b = y1 - a * x1 max_delta_x = x2 - x1 x = min((ps_t / 1000), 1) * max_delta_x + x1 y = (a * x + b) + (0.002 * (x - x1) * (x - x2)) display.blit(pygame.transform.scale(self.bubble, (64, 64)), (x - 32, y - 32)) else: ps_t -= 1000 x_start, y_start = t[0], t[1] - 40 for bu in self.random_vec: vec = [bu[0], bu[1]] if 0 < (n_pst_t := ps_t - bu[2]) < bu[3]: adv = (n_pst_t / bu[3]) vec[0] *= adv * 150 vec[1] *= adv * 150 display.blit(self.bubble, (x_start - 16 + vec[0], y_start - 16 + vec[1]))
def render(self, display: pygame.display, target: list[tuple[int, int, int]], launcher: tuple[int, int, int], ps_t: int, first_time: bool) -> NoReturn: if first_time: self.current_vars = [[False] * self.last_nb_hit, [(random.randint(-50, 50), random.randint(-50, 50)) for _ in range(15)]] c_hit = min(self.last_nb_hit - 1, ps_t // 1000) if not self.current_vars[0][c_hit]: self.current_vars[0][c_hit] = True sound_manager.start_in_first_empty_taunt(self.sound) for t_i in range(len(target)): x, y = target[t_i][0], target[t_i][1] - 55 for i in range(15): i_60 = int((i / 15) * ((1000 * (c_hit + 1)) - SPEED)) if i_60 < ps_t < i_60 + SPEED: rdm = self.current_vars[1][i] display.blit( self.img_y if (ps_t - i_60) // 50 else self.img_g, (x + rdm[0] - 14, y + rdm[1] - 14))
def display(self, screen: pygame.display): # Set the rect for the text to the center of the button text_rect = self.text.get_rect(center=self.rect.center) # Draw the button pygame.draw.rect(screen, self.color, self.rect) # Draw the text screen.blit(self.text, text_rect)
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 render(self, display: pygame.display, target: list[tuple[int, int, int]], launcher: tuple[int, int, int], ps_t: int, first_time: bool) -> NoReturn: if first_time: sound_manager.start_in_first_empty_taunt(self.sound) self.g_i = gif_manger.CONTACT.get().display( (target[0][0] - 60, target[0][1] - 60), 25) x1, y1 = launcher[0] + 40, launcher[1] - 50 x2, y2 = target[0][0], target[0][1] - 30 a = (y2 - y1) / (x2 - x1) b = y1 - a * x1 max_delta_x = x2 - x1 if 100 < ps_t < 1000 or 2600 < ps_t < 3500: x = min( ((ps_t - (100 if ps_t < 1000 else 2600)) / 900), 1) * max_delta_x + x1 y = (a * x + b) + (0.002 * (x - x1) * (x - x2)) display.blit( pygame.transform.rotate(self.bone, (ps_t % 300) / 300 * 360), (x - 32, y - 32)) elif 1000 <= ps_t <= 1700 or 3500 <= ps_t <= 4200: x = x2 - min( ((ps_t - (1000 if ps_t <= 1700 else 3500)) / 700), 1) * max_delta_x y = (a * x + b) + (-0.002 * (x - x1) * (x - x2)) display.blit( pygame.transform.rotate(self.bone, (ps_t % 300) / 300 * 360), (x - 32, y - 32))
def update(self, screen: pygame.display): # This will be deprecated pending new methods for movement # self.rect.x += self.x_movement # self.rect.y += self.y_movement screen.blit(self.image, self.rect)
def update(self, screen: pygame.display) -> bool: screen.blit(self.background_image, (0, 0)) self.exit_button.draw(screen) self.start_button.draw(screen) self.exit_button.check_state() if self.start_button.check_state(): return True return False
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 end_game(self, screen: pygame.display, grid: Grid) -> None: """ Run when the game is over Display the score """ print("game is over") print("final score = ", grid.get_score()) # open menu surface = pygame.Surface((self._width, self._height)) surface.set_alpha(128) surface.fill((0, 0, 0)) screen.blit(surface, (0, 0)) # draw the text font = pygame.font.SysFont(self._font, 32) pause_text = font.render("Game Over", True, (255, 255, 255)) text_rect = pause_text.get_rect() text_rect.center = (self._width // 2, self._height // 20) screen.blit(pause_text, text_rect) score_text = font.render( "Final Score: " + str(self._player.get_score()), 32, (255, 255, 255)) text_rect = score_text.get_rect() text_rect.center = (self._width // 2, self._height // 7.5) screen.blit(score_text, text_rect) continue_text = font.render("Press any key", 24, (255, 255, 255)) text_rect = continue_text.get_rect() text_rect.center = (self._width // 2, self._height // 4) screen.blit(continue_text, text_rect) continue_text = font.render("to restart", 24, (255, 255, 255)) text_rect = continue_text.get_rect() text_rect.center = (self._width // 2, self._height // 3.25) screen.blit(continue_text, text_rect) pygame.display.update() restart = False # wait for player to hit a key to restart while not restart: for event in pygame.event.get(): # player wants to quit if event.type == pygame.QUIT: self._quit = True self.quit_game() return False # player wants to restart if event.type == pygame.KEYDOWN: restart = True if self._player: self._player.lose()
def showDistanceBetweenVertices(vertices: List[Vertex], screen: pygame.display): posY = 0 for vertex in vertices: name = vertex.name text = f"{name:^3} dist:{vertex._lastIntersectionMemory}" keyImage = font.render(text, True, Colors.MainColors.onSurfaceColor) screen.blit(keyImage, [0, posY]) posY += font.size(name)[1]
def draw(self, screen: pygame.display, position: List[int]) -> None: """ Draws the tile on the specified screen and position :param screen: The screen the tile should be drawn on to :param position: """ self._position = position.copy() self._rect = self._image.get_rect().move(position[0], position[1]) screen.blit(self._image, position)
def display_won(screen: pygame.display) -> None: text_rect_shadow = copy(win_rect) text_rect_shadow.w += 20 text_rect_shadow.h += 20 text_rect_shadow.x -= 10 text_rect_shadow.y -= 10 gfxdraw.box(screen, text_rect_shadow, (0, 0, 0, 120)) screen.blit(win_text, win_rect) draw_play_again_button(screen)
def render(self, display: pygame.display, target: list[type[int, int, int]], launcher: tuple[int, int, int], ps_t: int, first_time: bool) -> NoReturn: if first_time: sound_manager.start_in_first_empty_taunt(self.sound) move = (ps_t / 600) * 120 for t in target: display.blit(self.img_l, (t[0] - 100 + move, t[1] - 40)) display.blit(self.img_r, (t[0] + 100 - move, t[1] - 40))
def text_to_screen(screen: pygame.display, font, text: str, position: tuple, color=(0, 0, 0)) -> None: """Renders text to a screen objects in a specified color and position. Default font color is black and default size is 100px""" text = font.render(text, True, color) screen.blit(text, (position[0], position[1]))
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 show(self, w: pygame.display): """Pokazi se na w""" # Obrni svojo sliko rotated_image = rotate(self.image, self.rotation) # Nastavi svojo velikost na velikost nove slike, da bo zaznavanje dotikov bolj natancno self.sx, self.sy = rotated_image.get_size() w.blit(rotated_image, (self.x, self.y))
def draw(self, display: pygame.display) -> None: """ A function used to draw a button """ if self.active: color = (self._color[0], self._color[1], self._color[2] + 255) else: color = self._color pygame.draw.rect(display, color, self._rect) display.blit(self._text_surface, self._text_pos)
def draw(self, display: pygame.display, adjust: (int, int)): x, y = adjust if self.__transform is None: image = self.__image else: angle, scale = self.__transform image = pygame.transform.rotozoom(self.__image, angle, scale) display.blit(image, [self.x - x, self.y - y], self.__area_rect)
def Eyes(screen: pygame.display, x, y, zoom, color_body, color_eye_1, color_eye_2): ellipse(screen, color_eye_2, (x + 52 * zoom, y - 138 * zoom, 24 * zoom, 20 * zoom)) ellipse(screen, color_eye_1, (x + 65 * zoom, y - 133 * zoom, 10 * zoom, 8 * zoom)) glasik = pygame.Surface((12 * zoom, 6 * zoom), pygame.SRCALPHA) ellipse(glasik, color_body, (0, 0, 11 * zoom, 11 * zoom)) glasik_rot = pygame.transform.rotate(glasik, -30) screen.blit(glasik_rot, (x + 58 * zoom, y - 138 * zoom))
def draw_await_players(layer: pygame.display) -> None: """Отрисовка слоя для ожидания игрока. :return: None. """ layer.blit(pygame.image.load("Picture/BackGround.jpg").convert(), (0, 0)) font = pygame.font.SysFont("comicsans", 80) text = font.render("Подождите напарника для игры", 1, (255, 0, 0), True) layer.blit(text, (size_main_window[0] / 2 - text.get_width() / 2, size_main_window[1] / 2 - text.get_height() / 2))
def display(self, screen: pygame.display): screen.blit(self.image, self.pos) s = pygame.Surface((self.rect.width, self.rect.height)) if self.is_highlighted: s.set_alpha(100) else: s.set_alpha(0) s.fill(self.highlight_color) screen.blit(s, self.rect.topleft)
def flower(screen: pygame.display, cord_x: int, cord_y: int, size: float, angle: float, colour_flower_1: tuple, colour_flower_2: tuple, colour_flower_3: tuple): surface = pygame.Surface((round(size * 49), round(size * 26)), pygame.SRCALPHA) ellipse(surface, colour_flower_1, (round(size * 18 - size * 8), round(size * 6 - size * 5), round(size * 2 * 8), round(size * 2 * 5))) ellipse(surface, colour_flower_3, (round(size * 18 - size * 8), round(size * 6 - size * 5), round(size * 2 * 8), round(size * 2 * 5)), round(size)) ellipse(surface, colour_flower_1, (round(size * 30 - size * 8), round(size * 5 - size * 5), round(size * 2 * 8), round(size * 2 * 5))) ellipse(surface, colour_flower_3, (round(size * 30 - size * 8), round(size * 5 - size * 5), round(size * 2 * 8), round(size * 2 * 5)), round(size)) ellipse(surface, colour_flower_2, (round(size * 25 - size * 11), round(size * 10 - size * 4), round(size * 2 * 11), round(size * 2 * 4))) ellipse(surface, colour_flower_1, (round(size * 35 - size * 8), round(size * 10 - size * 5), round(size * 2 * 8), round(size * 2 * 5))) ellipse(surface, colour_flower_3, (round(size * 35 - size * 8), round(size * 10 - size * 5), round(size * 2 * 8), round(size * 2 * 5)), round(size)) ellipse(surface, colour_flower_1, (round(size * 13 - size * 8), round(size * 12 - size * 5), round(size * 2 * 8), round(size * 2 * 5))) ellipse(surface, colour_flower_3, (round(size * 13 - size * 8), round(size * 12 - size * 5), round(size * 2 * 8), round(size * 2 * 5)), round(size)) ellipse(surface, colour_flower_1, (round(size * 22 - size * 8), round(size * 16 - size * 5), round(size * 2 * 8), round(size * 2 * 5))) ellipse(surface, colour_flower_3, (round(size * 22 - size * 8), round(size * 16 - size * 5), round(size * 2 * 8), round(size * 2 * 5)), round(size)) ellipse(surface, colour_flower_1, (round(size * 30 - size * 8), round(size * 15 - size * 5), round(size * 2 * 8), round(size * 2 * 5))) ellipse(surface, colour_flower_3, (round(size * 30 - size * 8), round(size * 15 - size * 5), round(size * 2 * 8), round(size * 2 * 5)), round(size)) surface_rot = pygame.transform.rotate(surface, angle) screen.blit(surface_rot, (cord_x - round(size * 27), cord_y - round(size * 13)))
def draw(self, display: pygame.display, adjust: (int, int)): x = self.x - adjust[0] y = self.y - adjust[1] font = pygame.font.Font(self.__font_type, self.__font_size) text = self.__text render = font.render(text, True, (0, 0, 0)) display.blit(render, [x + 1, y + 1]) render = font.render(text, True, self.__color) display.blit(render, [x, y])
def update(self, screen: pygame.display, image: str) -> None: """ Redraws the tile on the given screen with an updated image. Uses the stored position as the position on the screen. :param screen: The screen the tile should be drawn on to :param image: the name of new image to be displayed on the tile """ self._image = pygame.image.load( os.path.join(os.path.dirname(__file__), "assets/" + image + ".png")) self._image = pygame.transform.scale(self._image, self._size) screen.blit(self._image, self._position)
def render(self, screen: pygame.display) -> None: screen.blit(self.texture, ( (self.x * cell_size) + window_padding, (self.y * cell_size) + window_padding ), ( self.texture_y * cell_size, self.black * cell_size, cell_size, cell_size ) )
def eye(screen: pygame.display, cord_x: int, cord_y: int, size: float, colour_body: tuple, colour_eye_1: tuple, colour_eye_2: tuple): ellipse(screen, colour_eye_2, ((cord_x + round(size * 64)) - round(size * 12), (cord_y - round(size * 128) - round(size * 10)), round(size * 2 * 12), round(size * 2 * 10))) ellipse(screen, colour_eye_1, ((cord_x + round(size * 70) - round(size * 5), (cord_y - round(size * 129)) - round(size * 4), round(size * 2 * 5), round(size * 2 * 4)))) surface = pygame.Surface((round(size * 12), round(size * 6)), pygame.SRCALPHA) ellipse(surface, colour_body, (0, 0, round(size * 11), round(size * 5))) surface_rot = pygame.transform.rotate(surface, -30) screen.blit(surface_rot, (cord_x + round(size * 58), cord_y - round(size * 138)))
def draw(self, screen: pygame.display) -> None: for sensor in self.sensors: gfxdraw.line(screen, int(np.round(sensor.coords.xy[0][0])), int(np.round(sensor.coords.xy[1][0])), int(np.round(sensor.coords.xy[0][1])), int(np.round(sensor.coords.xy[1][1])), Const.COLORS.red) screen.blit( Const.FONT_SENSORS.render( f'{np.round(sensor.length if sensor.length > 0 else 0.0, decimals=1)}', True, Const.COLORS.white), (int(np.round(sensor.coords.xy[0][1])), int(np.round(sensor.coords.xy[1][1]))))
def show(self, window:pygame.display): windowResolution = (window.get_width(),window.get_height()) windowHeightOn10 = int(windowResolution[1]/10) windowHeightOn5 = int(windowResolution[1]/5) topResolution = (windowResolution[0],windowHeightOn10) bottomResolution = (windowResolution[0],windowHeightOn5) #scaling images if they are different from the current resolution if (self._topInterface.get_width(), self._topInterface.get_height()) != topResolution : topInterface = pygame.transform.scale(self._topInterface,topResolution) else: topInterface = self._topInterface if (self._bottomInterface.get_width(),self._bottomInterface.get_height()) != bottomResolution: bottomInterface = pygame.transform.scale(self._bottomInterface,bottomResolution) else: bottomInterface = self._bottomInterface window.blit(topInterface,ANCHOR_AT_00) window.blit(bottomInterface,(0,windowResolution[1]-windowHeightOn5))