コード例 #1
0
ファイル: td.py プロジェクト: FreddieBoi/pytd
 def __init__(self):
     pygame.Surface.__init__(self, FIELD_RECT.bottomright)
     self.convert()
     self.show_grid = True
     self.fill((100,100,100))
     self.bounds = self.get_rect()
     # define the entrance
     self.entrance = pygame.Rect(self.bounds.left+9*TILE_SIZE, self.bounds.top, 
                                 2*TILE_SIZE, TILE_SIZE)
     # define the exit
     self.exit = pygame.Rect(self.bounds.right-11*TILE_SIZE, 
                             self.bounds.bottom-TILE_SIZE,
                             2*TILE_SIZE,TILE_SIZE)
     
     # Create the grid-path representation of the field
     self.rows = self.bounds.h/TILE_SIZE
     self.cols = self.bounds.w/TILE_SIZE
     end = xy2coord(self.exit.topleft) # will be changed later upon creep spawn
     self.gridpath = GridPath(self.rows, self.cols, end)
コード例 #2
0
ファイル: td.py プロジェクト: FreddieBoi/pytd
class Field(pygame.surface.Surface, GridPath):
    def __init__(self):
        pygame.Surface.__init__(self, FIELD_RECT.bottomright)
        self.convert()
        self.show_grid = True
        self.fill((100,100,100))
        self.bounds = self.get_rect()
        # define the entrance
        self.entrance = pygame.Rect(self.bounds.left+9*TILE_SIZE, self.bounds.top, 
                                    2*TILE_SIZE, TILE_SIZE)
        # define the exit
        self.exit = pygame.Rect(self.bounds.right-11*TILE_SIZE, 
                                self.bounds.bottom-TILE_SIZE,
                                2*TILE_SIZE,TILE_SIZE)
        
        # Create the grid-path representation of the field
        self.rows = self.bounds.h/TILE_SIZE
        self.cols = self.bounds.w/TILE_SIZE
        end = xy2coord(self.exit.topleft) # will be changed later upon creep spawn
        self.gridpath = GridPath(self.rows, self.cols, end)
        
    def get_next(self, coord):
        return self.gridpath.get_next(coord)
    
    def get_path(self, coord):
        return self.gridpath._compute_path(coord)
    
    def block(self, coord):
        self.gridpath.set_blocked(coord, True)
        
    def unblock(self, coord):
        self.gridpath.set_blocked(coord, False)
        
    def is_blocked(self, coord):
        return self.gridpath.map.is_blocked(coord)
    
    def draw(self, screen):
        self._draw_portals(screen)
        if self.show_grid:
            self._draw_grid(screen)
    
    def _draw_grid(self, screen):
        for y in range(self.rows+1):
            pygame.draw.line(screen, pygame.color.Color(50, 50, 50),
                (self.bounds.left, self.bounds.top + y * TILE_SIZE - 1),
                (self.bounds.right - 1, self.bounds.top + y * TILE_SIZE - 1))
        
        for x in range(self.cols+1):
            pygame.draw.line(screen,pygame.color.Color(50, 50, 50), 
                             (self.bounds.left+x * TILE_SIZE - 1, 
                              self.bounds.top),
                             (self.bounds.left + x * TILE_SIZE - 1, 
                              self.bounds.bottom - 1))
    
    def _draw_portals(self, screen):
        entrance_sf = pygame.Surface((self.entrance.w-1, self.entrance.h-1))
        entrance_sf.fill(pygame.color.Color(80, 200, 80))
        entrance_sf.set_alpha(150)
        screen.blit(entrance_sf, self.entrance)
        
        exit_sf = pygame.Surface((self.exit.w-1, self.exit.h-1))
        exit_sf.fill(pygame.color.Color(200, 80, 80))
        exit_sf.set_alpha(150)
        screen.blit(exit_sf, self.exit)
        
    def _get_goal(self):
        return self.gridpath.goal
    
    def _set_goal(self, coord):
        self.gridpath.goal = coord
        # path cahche invalid
        self.gridpath._path_cache = {}
        
    goal = property(_get_goal, _set_goal, "The goal coordinates.")