Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

selection box in pygame - make selection - set unit destination

SELECTION BOX

1. Store the mouse coordinate of the initial mouse button click, + the panning amount.

2. Subtract the current mouse position with the initial click coordinate value

3. Positive length, when the mouse is on right side from the starting point. 

4. Negative length, when the mouse is on left side from the starting point.

5. Replace the box surface, with the updated size, based on the new length. (self.image)

6. Set the upper left corner coordinate, for the new box surface, using the mouse position, or the starting point. On whether the length is positive or negative.

7. Make the new surface transparent and draw a rectangle on its edge.

8. Use the surface to do collision checking with, once you've finished the selection, by releasing the mouse button.

if not button_1 and len(selection_box.sprites()) == 1:          
    selected = pygame.sprite.spritecollide(selection_box[0], units, False)
    selection_box.empty()

----------------------------------------------------------------------------------------------------------------

box updating mechanism 

    def update(self):  
        length_x = (mouse_x + players[reference].panning_x) - self.start_x
        length_y = (mouse_y + players[reference].panning_y) - self.start_y
        #GENERATE RECTANGLE BASED ON NEW LENGTH
        selection_box_surface = pygame.Surface((abs(length_x), abs(length_y)), pygame.SRCALPHA, 32)
        self.rect = selection_box_surface.get_rect()
        pygame.draw.rect(selection_box_surface,(255,255,255), self.rect, width=2)
        self.image = selection_box_surface   
        
        #SET NEW UPPER LEFT CORNER BASED ON POSITIVE OR NEGATIVE NUMBER
        if length_x < 0:
            self.rect.x = self.start_x - abs(length_x)
        if length_x > 0:
            self.rect.x = self.start_x 
        if length_y < 0:
            self.rect.y = self.start_y - abs(length_y)
        if length_y > 0:
            self.rect.y = self.start_y

----------------------------------------------------------------------------------------------------------------

SET DESTINATION - SECOND MOUSE BUTTON

Calculate the amount of X and Y steps, that's required to reach the destination.

Walk off 1 step during a frame cycle. Keep repeating, until there's no steps left.

sprite.destination_steps_x = -2    #go left, negative value
sprite.destination_steps_y = 20    #go down, positive value

----------------------------------------------------------------------------------------------------------------

destination set script: (calculate amount of steps)

if action == "destination_set":
    destination_steps_x_length = (mouse_x + players[reference].panning_x) - sprite.rect.x 
    destination_steps_y_length = (mouse_y + players[reference].panning_y) - sprite.rect.y 
    sprite.destination_steps_x = round(destination_steps_x_length/MAP_GRID_X/sprite.speed_factor) 
    sprite.destination_steps_y = round(destination_steps_y_length/MAP_GRID_Y/sprite.speed_factor)

----------------------------------------------------------------------------------------------------------------

DIAGONAL MOVEMENT : ISOMETRIC ANGLE ON MODEL

destination move script: (move sprite and reduce with two steps)

#LEFT DOWN DIRECTION 
if sprite.destination_steps_x < 0 and sprite.destination_steps_y > 0:
    sprite.image_list = sprite.animation_left_down
    sprite.rect.x -= (MAP_GRID_X * sprite.speed_factor)          
    sprite.rect.y += (MAP_GRID_Y * sprite.speed_factor)             
    sprite.destination_steps_x += 1      
    sprite.destination_steps_y -= 1      

----------------------------------------------------------------------------------------------------------------

ADDITIONAL MECHANICS

Construct a path sequence, determining how the total amount of destination steps are taken off, in which direction and order. Increase the total amount, while keeping the end destination intact. Go around objects.

Support this post

Did you like this post? Tell us

Leave a comment

Log in with your itch.io account to leave a comment.