def apply_gradient_to_surface(self, input_surface: pygame.Surface, rect=None): """ Applies this gradient to a specified input surface using blending multiplication. As a result this method works best when the input surface is a mostly white, stencil shape type surface. :param input_surface: :param rect: The rectangle on the surface to apply the gradient to. """ # scale the gradient up to the right size input_surface_size = input_surface.get_size() inverse_rotated_input = pygame.transform.rotate(input_surface, -self.angle_direction) gradient_size = inverse_rotated_input.get_rect().size gradient_surf = pygame.Surface(gradient_size, flags=pygame.SRCALPHA, depth=32) pygame.transform.scale(self.gradient_surface, gradient_size, gradient_surf) gradient_surf = pygame.transform.rotate(gradient_surf, self.angle_direction) if rect is not None: input_surface.set_clip(rect) input_surface.blit(gradient_surf, rect, special_flags=pygame.BLEND_RGBA_MULT) input_surface.set_clip(None) else: gradient_placement_rect = gradient_surf.get_rect() gradient_placement_rect.center = (int(input_surface_size[0] / 2), int(input_surface_size[1] / 2)) input_surface.blit(gradient_surf, gradient_placement_rect, special_flags=pygame.BLEND_RGBA_MULT)
def get_surface(self): surface = Surface(self.size) if self.colorkey: surface.fill(self.colorkey) if 0 < self.alpha < 255: surface.set_alpha(self.alpha, RLEACCEL) self.blit_templates(surface) ## self.blit_corners(surface) ## self.blit_sides(surface) surface.set_colorkey(self.colorkey, RLEACCEL) surface.set_clip(self.clip) return surface.convert()
def draw(self, screen: pygame.Surface, view_rect): # pygame seems to just set coordinates to 0 if rect is outside of screen and you try and blit it? # so ensure only visible portion of window onscreen is drawn r = screen.get_rect().clip(self.rect) smart_draw( screen, self.background if not self._mouseover else self.background_mouseover, r) # don't let children draw outside of bounds clipping_rect = screen.get_clip() screen.set_clip(r) # draw children super().draw(screen, view_rect) # restore screen as we found it screen.set_clip(clipping_rect)
def draw(self, destination: pg.Surface): center_x, center_y = self.center.ints() self.board_texture.draw(destination, center_x, center_y) coords = self.get_current_coords() x, y = coords.ints() prev_clip = destination.get_clip() destination.set_clip(self.get_rect()) if self._can_hit_player(): self.heart_texture.draw(destination, x, y) else: self.hit_heart_texture.draw(destination, x, y) with self.sprites: for sprite in self.sprites: sprite.draw(destination) destination.set_clip(prev_clip) with self.unrestricted_sprites: for sprite in self.unrestricted_sprites: sprite.draw(destination)
def render(self, screen: pygame.Surface): # draw game backgrounds self.render_ui_sprite(screen) # draw button for b in self.buttons: b.render(screen) # draw hand location = V2(config.hand_start_pos) location.x += self.hand_xoffset screen.set_clip(config.hand_draw_area) for c in self.engine.get_curr_hand(): surf = pygame.image.load(config.assetsroot + c.asset_name()) screen.blit(surf, location) location.x += (config.card_size[0] + config.hand_margin) screen.set_clip(None) # draw visual clues if self.picked_piece and self.legal_positions: for legal_pos in self.legal_positions: # see https://stackoverflow.com/questions/6339057/draw-a-transparent-rectangle-in-pygame surf = pygame.Surface(config.piece_size, pygame.SRCALPHA) surf.fill(config.ui_colors.legal_pos) screen.blit( surf, V2(config.board_pos) + (legal_pos.elementwise() * config.piece_size)) if self.picked_card is not None: self.legal_starts = self.engine.valid_positions(self.picked_card) for legal_pos in self.legal_starts: surf = pygame.Surface(config.piece_size, pygame.SRCALPHA) surf.fill(config.ui_colors.legal_pos) screen.blit( surf, V2(config.board_pos) + (legal_pos.elementwise() * config.piece_size)) surf = pygame.Surface(config.card_size, pygame.SRCALPHA) surf.fill(config.ui_colors.legal_pos) pickedLoc = V2(config.hand_start_pos) pickedLoc.x += self.hand_xoffset for x in range(0, self.picked_card): pickedLoc.x += (config.card_size[0] + config.hand_margin) screen.blit(surf, pickedLoc) # draw pieces for p in self.engine.board.pieces: if p == self.picked_piece: location = pygame.Rect((0, 0), config.piece_size) location.center = pygame.mouse.get_pos() self.legal_positions = p.get_legal_moves() else: location = (p.pos.elementwise() * V2(config.piece_size)) + config.board_pos assetfile = p.asset_name() surf = pygame.image.load(config.assetsroot + assetfile) screen.blit(surf, location) # draw deck size text = "{}/50".format(self.engine.get_curr_deck_size()) screen.blit( config.ui_fonts.s.render(text, True, config.ui_colors.black), config.deck_text_pos) #draw mana bar position = V2(config.mana_start_pos) currMana = self.engine.current_player.turn_mana maxMana = self.engine.current_player.max_mana text = "{}/{}".format(currMana, maxMana) drawCounter = 0 while drawCounter < currMana: drawCounter += 1 image = pygame.image.load("assets/mana_untapped.png") screen.blit(image, position) position.x += 60 while drawCounter < maxMana: drawCounter += 1 image = pygame.image.load("assets/mana_tapped.png") screen.blit(image, position) position.x += 60 screen.blit( config.ui_fonts.s.render(text, True, config.ui_colors.black), config.mana_text_pos) #draw error message if self.error_message != '': error_words = self.error_message.split(' ') space_width = config.error_font.size(' ')[0] x, y = config.error_pos for w in error_words: word_surface = config.error_font.render( w, True, config.error_color) word_width, word_height = word_surface.get_size() if x + word_width >= config.screen_w: x = config.error_pos[0] # Reset the x. y += word_height # Start on new row. screen.blit(word_surface, (x, y)) x += word_width + space_width x = config.error_pos[0] y += word_height word_surface = config.error_font.render('(Right click to dismiss)', True, config.error_color) screen.blit(word_surface, (x, y)) # draw phase indicator if self.engine.phase == 0: text = "Strategy Phase" elif self.engine.phase == 1: text = "Action Phase" elif self.engine.phase == 2: text = "Fall Back Phase" screen.blit( config.ui_fonts.s.render(text, True, config.ui_colors.black), config.phase_text_pos) # draw player indicator if (self.engine.current_player.color == PlayerColor.WHITE) ^ (not self.engine.get_is_my_turn()): p_colour = "white" black_ind = config.black_indicator white_ind = config.white_indicator_active else: p_colour = "black" black_ind = config.black_indicator_active white_ind = config.white_indicator text = 'Player ' + p_colour + ' turn' screen.blit( config.turn_indicator_font.render(text, True, config.ui_colors.black), config.turn_indicator_pos) screen.blit(black_ind, config.black_indicator_pos) screen.blit(white_ind, config.white_indicator_pos) # draw opponent information currMana = self.engine.waiting_player.turn_mana maxMana = self.engine.waiting_player.max_mana text = "opponent's mana: " + "{}/{}".format(currMana, maxMana) screen.blit( config.opponent_mana_font.render(text, True, config.ui_colors.black), config.opponent_mana_pos) text = "opponent's hand: " + str(len(self.engine.waiting_player.hand)) screen.blit( config.opponent_hand_font.render(text, True, config.ui_colors.black), config.opponent_hand_pos)