1. Sami Loone
  2. Fat x Fast

Source

Fat x Fast / FatxFast / gui / hud / minimapframe.py

# This file is part of FatxFast.                                                
#                                                                               
#    FatxFast is free software: you can redistribute it and/or modify           
#    it under the terms of the GNU General Public License as published by       
#    the Free Software Foundation, either version 3 of the License, or          
#    (at your option) any later version.                                        
#                                                                               
#    FatxFast is distributed in the hope that it will be useful,                
#    but WITHOUT ANY WARRANTY; without even the implied warranty of             
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              
#    GNU General Public License for more details.                               
#                                                                               
#    You should have received a copy of the GNU General Public License          
#    along with FatxFast.  If not, see <http://www.gnu.org/licenses/>.          
                                                                                
# Versioning based on:                                                          
# http://en.wikipedia.org/wiki/Versioning#Designating_development_stage         
__author__ = "dryatu (c) 2013"                                                  
__version__ = "1.2.4" 

from FatxFast.gui.hud.frame import Frame
from FatxFast.camera import Camera
from FatxFast.tilemap.editor.tile import TILEWIDTH, TILEHEIGHT
import pygame

class MiniMapFrame(Frame):
    """Creates a progression bar based on the turbo left on the supplied player.
    Base image for the progression bar has to be supplied."""

    _player_colors = [(200,150,0), (150,50,150), (0,200,0)]

    def __init__(self, game, flipped=False, loaded=False,**kwargs):
        super(MiniMapFrame, self).__init__(game, **kwargs)
        self._loaded = loaded
        self._temp_surf = pygame.Surface((0,0)) 
        if loaded:
            self.load_minimap()
        # Flipped: True = Center up right, False Right Corner
        self.flipped = flipped

    def toggle_position(self):
        if self.flipped:
            self.__init__(self.game, flipped=False, loaded=True,
                **self.fetch_template("minimap"))
        else:
            self.__init__(self.game, flipped=True, loaded=True, 
                **self.fetch_template("flippedminimap"))

    def _calculate_size_without_borders(self):
        new_rect = pygame.rect.Rect((0,0),(0,0))
        new_rect.width = self.rect.width - self._border.width
        new_rect.height = self.rect.height - self._border.width
        new_rect.x = self.rect.x + self._border.width
        new_rect.y = self.rect.y + self._border.width
        return new_rect

    def load_minimap(self):
        self.tilemap = self.game.tilemap
        self.camera = Camera(
            (self.tilemap.width*TILEWIDTH, 
                self.tilemap.height*TILEHEIGHT))
        self._set_camera_zoom()
        self.tilemap.draw_map(self._temp_surf, self.camera)

        self._temp_surf = pygame.transform.scale(self._temp_surf, 
            self._calculate_size_without_borders().size)
        self._loaded = True

    def _set_camera_zoom(self):
        # Optimize to cache a resized surface
        if self._temp_surf.get_size() != self.camera.rect.size:
            self._temp_surf = pygame.Surface(self.camera.rect.size)

    def draw(self, surface):
        if not self._loaded:
            return

        surface.blit(self._temp_surf, self.rect.topleft)
        
        player_rect = pygame.rect.Rect((0,0), 
            (10, 10))

        for index, player in zip(xrange(
                0,len(self.game.players)), self.game.players):
            if index >= len(self._player_colors):
                index = len(self._player_colors)-1
            player_rect.width = 2+ (player.rect.width/\
                (self.camera.rect.width/self.rect.width))
            player_rect.height = 2+ (player.rect.height/\
                (self.camera.rect.height/self.rect.height))
            player_rect.x = self.rect.x+(player.rect.x/\
                (self.camera.rect.width/float(self.rect.width)))
            player_rect.y = self.rect.y+(player.rect.y/\
                (self.camera.rect.height/float(self.rect.height)))
            surface.fill(self._player_colors[index], player_rect) 

        super(MiniMapFrame, self).draw(surface)