class Background: @staticmethod def get_background_image(): return 'img/background.png' def __init__(self, window, width, height): self.window = window self.x = 0 self.y = 0 self.width = width self.height = height self.image = pg.image.load(Background.get_background_image()) self.image = pg.transform.scale(self.image, (width, height)) self.fire = Element(window, 100, 300, 100, "fire") self.water = Element(window, 180, 240, 100, "water") self.earth = Element(window, 700, 300, 100, "earth") self.wind = Element(window, 620, 240, 100, "wind") def is_hover_flag(self, element, flag): if element == "fire": self.fire.flag = flag if element == "water": self.water.flag = flag if element == "earth": self.earth.flag = flag if element == "wind": self.wind.flag = flag def display(self): self.window.blit(self.image, (self.x, self.y)) self.fire.display() self.earth.display() self.water.display() self.wind.display()
def display(self, winner): text = "Player " + winner.name + " wins" txt_surface = FONT.render(text, True, (0, 0, 0)) text_2 = "Click button to play again" txt_surface2 = FONT_2.render(text_2, True, (0, 0, 0)) player_element = Element(self.window, 100, 300, 100, winner.get_element()) clock = pygame.time.Clock() flag_play_agian = False end_game_flag = True while end_game_flag: clock.tick(30) # get all events ev = pygame.event.get() # proceed events for event in ev: # handle MOUSEBUTTONUP if event.type == pygame.MOUSEBUTTONUP: x, y = pygame.mouse.get_pos() flag_play_agian = self.start_button.is_hover(x, y) if event.type == pygame.QUIT: end_game_flag = False self.window.fill((255, 255, 255)) self.window.blit(self.background_image, (0, 0)) self.is_button_hovered() self.start_button.display() self.window.blit( txt_surface, (self.window_width / 2 - txt_surface.get_width() / 2, 100)) self.window.blit( txt_surface2, (self.window_width / 2 - txt_surface2.get_width() / 2, 200)) player_element.display() self.window.blit( self.crown_image, (self.window_width / 2 + txt_surface.get_width() / 2 - 50, 50)) pygame.display.flip() if flag_play_agian: break return flag_play_agian
# wyswietlanie graczy for p in players: players_list.append(players[p]) x = players[p].ball.middle.x * SCALE - pos_x * SCALE + WINDOW_WIDTH / 2 y = players[p].ball.middle.y * SCALE - pos_y * SCALE + WINDOW_HEIGHT / 2 # smoothly growing if p == player_id: real_r += (players[p].ball.radius - real_r) * 0.1 players[p].ball.radius = real_r r = players[p].ball.radius * SCALE pygame.draw.circle(window, players[p].color, (int(x), int(y)), int(r)) element = Element(window, x, y, r * 0.5, players[p].elem) element.display() # players[p].element # showing name font = pygame.font.SysFont("Consolas", int(r / 2)) text = font.render(players[p].name, True, pygame.Color(0, 0, 0)) window.blit(text, (x - (text.get_width() / 2), y + r)) SCALE = PLAYER_RADIUS / players[player_id].ball.radius # wyswietlanie rankingu players_list = sorted(players_list, key=lambda player: player.score) players_list.reverse() rank.display(players_list) # Crown display