Example #1
0
    def __init__(self, pos, bounds, direction, speed, next_coord_function):
        """ Create a new Creep.
                
            pos:
                A vec2d or a pair specifying the initial position
                of the creep on the screen.
            
            direction:
                A vec2d or a pair specifying the initial direction
                of the creep. Must have an angle that is a 
                multiple of 45 degres.
            
            speed: 
                Creep speed, in pixels/millisecond (px/ms)
        """
        pygame.sprite.Sprite.__init__(self)
        self.speed = int(speed)
        self.rect = bounds

        surface = pygame.Surface((self.rect.w-1, self.rect.h-1))
        surface.fill((0,0,0,0))
        pygame.draw.circle(surface, (0,0,255),((self.rect.w-1)/2+1,(self.rect.h-1)/2+1),
                           self.rect.w/2-1)
        surface.set_colorkey((0,0,0))
        self.image = surface
#        self.image = pygame.transform.rotate(pygame.image.load("..\img\creep_0.png"), -90)
        
        # A vector specifying the creep's position on the screen
        self.pos = v(pos)
        self.prev_pos = v(self.pos)

        # The direction is a normalized vector
        self.direction = v(direction).normalized()
        
        self.next_on_path = next_coord_function
Example #2
0
    def __init__(self, screen, bounds, pos):
        pygame.sprite.Sprite.__init__(self)

        self.rect = bounds

        self.image = pygame.Surface((self.rect.w-1, self.rect.h-1))
        self.image.fill((50, 50, 50))
        self.image.set_alpha(100)

        # A vector specifying the block position on the screen
        self.pos = v(pos)
Example #3
0
    def __init__(self, screen, bounds, pos):
        pygame.sprite.Sprite.__init__(self)

        self.rect = bounds

        self.image = pygame.Surface((self.rect.w - 1, self.rect.h - 1))
        self.image.fill((50, 50, 50))
        self.image.set_alpha(100)

        # A vector specifying the block position on the screen
        self.pos = v(pos)
Example #4
0
    def __init__(self):
        # initialize screen
        pygame.init()
        title = "Tower Defence"
        version = "0.01"
        self.screen_size = (FIELD_RECT.w,FIELD_RECT.h)
        self.screen = pygame.display.set_mode(self.screen_size)
        pygame.display.set_caption(title+" v"+version)
        icon = pygame.image.load("../img/icon.png")
        pygame.display.set_icon(icon)
        # background
        self.background = pygame.Surface(self.screen_size).convert()
        self.background.fill((100,100,100))
        self.screen.blit(self.background, (0,0))
        # the field (playable area)
        self.field_rect = FIELD_RECT
        self.field_size = FIELD_RECT.bottomright
        self.tile_size = TILE_SIZE
        self.field = Field()
        self.screen.blit(self.field, (0,0))

        # clock
        self.clock = pygame.time.Clock()
        
        self.game_over = False
        self.round_over = False
        self.paused = False

        self.creeps = pygame.sprite.Group()
        self.towers = pygame.sprite.Group()
        self.player_towers = pygame.sprite.Group()
        # contains all sprites in this game
        self.sprites = pygame.sprite.RenderUpdates()
        
        self._create_blocks()
        
        self.player = Player()
        self.player.money = randint(5,20)
        self.next = (0,0)
        
        self._create_random_towers()
        
        self.building_mode = False
        self.build_time = 0
        self.time = 0
        self.message_text = ""
        rect = pygame.Rect(self.field.bounds.left+1*self.tile_size, 
                               self.field.bounds.top+1*self.tile_size, 
                               2*self.tile_size, 2*self.tile_size)
        self.building_marker = Tower(rect, v(rect.topleft), 
                                     (255,255,200))
        self.sprites.add(self.building_marker)
        pygame.display.flip()
Example #5
0
    def __init__(self, bounds, pos, color):
        pygame.sprite.Sprite.__init__(self)

        self.rect = bounds
        
        surface = pygame.Surface((self.rect.w-1, self.rect.h-1))

        self.image = pygame.Surface((self.rect.w-1, self.rect.h-1))
        self.image.fill(color)
        self.image.set_alpha(100)

        # A vector specifying the tower's position on the screen
        self.pos = v(pos)
Example #6
0
    def _compute_direction(self, time_passed):
        """ Finds out where to go
        """
        coord = xy2coord(self.pos)
        
        x_mid, y_mid = coord2xy_mid(coord)
#        if ((x_mid - self.pos.x) * (x_mid - self.prev_pos.x) < 0 or
#            (y_mid - self.pos.y) * (y_mid - self.prev_pos.y) < 0):
        if self.pos == (x_mid,y_mid):
            next_coord = self.next_on_path(coord)

            self.direction = v(next_coord[1] - coord[1],
                               next_coord[0] - coord[0]).normalized()
Example #7
0
    def __init__(self, bounds, pos, color):
        pygame.sprite.Sprite.__init__(self)

        self.rect = bounds

        surface = pygame.Surface((self.rect.w - 1, self.rect.h - 1))

        self.image = pygame.Surface((self.rect.w - 1, self.rect.h - 1))
        self.image.fill(color)
        self.image.set_alpha(100)

        # A vector specifying the tower's position on the screen
        self.pos = v(pos)
Example #8
0
    def _compute_direction(self, time_passed):
        """ Finds out where to go
        """
        coord = xy2coord(self.pos)

        x_mid, y_mid = coord2xy_mid(coord)
        #        if ((x_mid - self.pos.x) * (x_mid - self.prev_pos.x) < 0 or
        #            (y_mid - self.pos.y) * (y_mid - self.prev_pos.y) < 0):
        if self.pos == (x_mid, y_mid):
            next_coord = self.next_on_path(coord)

            self.direction = v(next_coord[1] - coord[1],
                               next_coord[0] - coord[0]).normalized()
Example #9
0
 def _create_blocks(self):
     rows = self.field_size[1]/self.tile_size
     cols = self.field_size[0]/self.tile_size
     for x in range(1,9)+range(11,cols-1):
         rect = pygame.Rect(self.field.bounds.left+x*self.tile_size, 
                            self.field.bounds.top, 
                            self.tile_size, self.tile_size)
         block = Block(self.screen, rect, v(rect.topleft))
         self.sprites.add(block)
         coord = xy2coord((self.field.bounds.left+x*self.tile_size, 
                           self.field.bounds.top))
         self.field.block(coord)
         rect = pygame.Rect(self.field.bounds.left+x*self.tile_size, 
                            self.field.bounds.bottom-self.tile_size, 
                            self.tile_size, self.tile_size)
         block = Block(self.screen, rect, v(rect.topleft))
         self.sprites.add(block)
         coord = xy2coord((self.field.bounds.left+x*self.tile_size, 
                                self.field.bounds.bottom-self.tile_size))
         self.field.block(coord)
     for y in range(rows):
         rect = pygame.Rect(self.field.bounds.left, 
                            self.field.bounds.top+y*self.tile_size, 
                            self.tile_size, self.tile_size)
         block = Block(self.screen, rect, v(rect.topleft))
         self.sprites.add(block)
         coord = xy2coord((self.field.bounds.left, 
                                self.field.bounds.top+y*self.tile_size))
         self.field.block(coord)
         rect = pygame.Rect(self.field.bounds.right-self.tile_size, 
                            self.field.bounds.top+y*self.tile_size, 
                            self.tile_size, self.tile_size)
         block = Block(self.screen, rect, v(rect.topleft))
         self.sprites.add(block)
         coord = xy2coord((self.field.bounds.right-self.tile_size, 
                                self.field.bounds.top+y*self.tile_size))
         self.field.block(coord)
Example #10
0
    def update(self, time_passed):
        """ Update the creep.
        
            time_passed:
                The time passed (in ms) since the previous update.
        """
        # Maybe it's time to change the direction ?
        #
        self._compute_direction(time_passed)
        # Make the creep image point in the correct direction.
        # Note that two images are used, one for diagonals
        # and one for horizontals/verticals.
        #
        # round() on the angle is necessary, to make it 
        # exact, despite small deviations that may result from
        # floating-point calculations
        #
#        if int(round(self.direction.angle)) % 90 == 45:
#            self.image = pygame.transform.rotate(self.base_image_45,
#                                                 -(self.direction.angle + 45))
#        elif int(round(self.direction.angle)) % 90 == 0:
#            self.image = pygame.transform.rotate(self.base_image_0,
#                                                 -self.direction.angle)
#        else:
#            assert False
        
        # Compute and apply the displacement to the position 
        # vector. The displacement is a vector, having the angle
        # of self.direction (which is normalized to not affect
        # the magnitude of the displacement)
        #
        displacement = v(self.direction.x * self.speed,
                         self.direction.y * self.speed)
        self.prev_pos = v(self.pos)
        self.pos += displacement
        self.rect.center = self.pos
Example #11
0
    def update(self, time_passed):
        """ Update the creep.
        
            time_passed:
                The time passed (in ms) since the previous update.
        """
        # Maybe it's time to change the direction ?
        #
        self._compute_direction(time_passed)
        # Make the creep image point in the correct direction.
        # Note that two images are used, one for diagonals
        # and one for horizontals/verticals.
        #
        # round() on the angle is necessary, to make it
        # exact, despite small deviations that may result from
        # floating-point calculations
        #
        #        if int(round(self.direction.angle)) % 90 == 45:
        #            self.image = pygame.transform.rotate(self.base_image_45,
        #                                                 -(self.direction.angle + 45))
        #        elif int(round(self.direction.angle)) % 90 == 0:
        #            self.image = pygame.transform.rotate(self.base_image_0,
        #                                                 -self.direction.angle)
        #        else:
        #            assert False

        # Compute and apply the displacement to the position
        # vector. The displacement is a vector, having the angle
        # of self.direction (which is normalized to not affect
        # the magnitude of the displacement)
        #
        displacement = v(self.direction.x * self.speed,
                         self.direction.y * self.speed)
        self.prev_pos = v(self.pos)
        self.pos += displacement
        self.rect.center = self.pos
Example #12
0
    def __init__(self, pos, bounds, direction, speed, next_coord_function):
        """ Create a new Creep.
                
            pos:
                A vec2d or a pair specifying the initial position
                of the creep on the screen.
            
            direction:
                A vec2d or a pair specifying the initial direction
                of the creep. Must have an angle that is a 
                multiple of 45 degres.
            
            speed: 
                Creep speed, in pixels/millisecond (px/ms)
        """
        pygame.sprite.Sprite.__init__(self)
        self.speed = int(speed)
        self.rect = bounds

        surface = pygame.Surface((self.rect.w - 1, self.rect.h - 1))
        surface.fill((0, 0, 0, 0))
        pygame.draw.circle(surface, (0, 0, 255), ((self.rect.w - 1) / 2 + 1,
                                                  (self.rect.h - 1) / 2 + 1),
                           self.rect.w / 2 - 1)
        surface.set_colorkey((0, 0, 0))
        self.image = surface
        #        self.image = pygame.transform.rotate(pygame.image.load("..\img\creep_0.png"), -90)

        # A vector specifying the creep's position on the screen
        self.pos = v(pos)
        self.prev_pos = v(self.pos)

        # The direction is a normalized vector
        self.direction = v(direction).normalized()

        self.next_on_path = next_coord_function
Example #13
0
    def build_tower(self, pos, color, group):
        row,col = xy2coord(pos)

        #@todo: center tower over cursor?
#        pos = pos[0]-self.tile_size/2,pos[1]-self.tile_size/2
#        row,col = xy2coord(pos)

        rect = pygame.Rect(self.field.bounds.left+col*self.tile_size, 
                               self.field.bounds.top+row*self.tile_size, 
                               2*self.tile_size, 2*self.tile_size)

        buildable, message = self._is_buildable(row,col)
        if buildable:
            tower = Tower(rect, v(rect.topleft), color)
            group.add(tower)
            self.sprites.add(tower)
        return (buildable,message)