[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Working with maps



http://www.pygame.org/project-pyTile-871-.html

This is the game engine I was working on, the code in question is in the tools.py file, e.g. collide_locate() function of the Tool object, which uses the MouseSprite object as the "invisible" sprite. You pass in a sprite group to test against and the cursor position.

def collide_locate(self, mousepos, collideagainst):
    """Locates the sprite(s) that the mouse position intersects with"""
    # Draw mouseSprite at cursor position
    if self.mouseSprite:
        self.mouseSprite.sprite.update(mousepos)
    else:
        self.mouseSprite = pygame.sprite.GroupSingle(
MouseSprite(mousepos))
    # Find sprites that the mouseSprite intersects with
    collision_list1 = pygame.sprite.spritecollide(
self.mouseSprite.sprite, collideagainst, False)
    if collision_list1:
        collision_list = pygame.sprite.spritecollide(
self.mouseSprite.sprite, collision_list1, False, pygame.sprite.collide_mask)
        if collision_list:
            collision_list.reverse()
            for t in collision_list:
                if t.exclude == False:
                    return t
            return None
        else:
            # No collision means nothing to select
            return None
    else:
        return None


On 18/05/2010 15:27, Kenny Meyer wrote:
Timothy Baldock (tb@xxxxxxxxxxxxx) wrote:
Hi Kenny,

I'd do this by making each country a sprite with a transparent
background (colour-key transparency would work), then whenever the user
clicks doing a collision detection between the position of the mouse
cursor (first use rect-collision to build a small number of tiles which
match, then do pixel-perfect collision based on alpha - i.e. using the
sprite's mask). Easiest way to do this IMO is to draw an "invisible"
(e.g. position the sprite, but don't actually draw it to the screen)
1x1px sprite at the position of the mouse cursor, then collide that
against the group containing all the country sprites.

This technique means the countries can be any colour you want, or the
colour can change without messing things up. I used this method for my
isometric game engine to allow selection of tiles and other objects and
found it to be quite fast.

Thanks,

Timothy


On 18/05/2010 05:09, Kenny Meyer wrote:
Hey,

I'd like to work with irregular formed geometric shapes like those in of
country maps in pygame.

I want to do the following:
"Divide" a country map into its states and provinces and make each of them
"click-able", where the map could be an image (*.png) or maybe a vector graphic
(*.svg).

The result should be:
A game where to guess and click the name of the state and province on a map.

Observations:
It would be quite difficult to assign each state/province fixed coordinates as
those are irregular geometric shapes.

Any ideas, pointers to other projects or suggestions?



Timothy,

Thank you for your answer!

I'd do this by making each country a sprite with a transparent
background (colour-key transparency would work), then whenever the user
clicks doing a collision detection between the position of the mouse
cursor (first use rect-collision to build a small number of tiles which
match, then do pixel-perfect collision based on alpha - i.e. using the
sprite's mask). Easiest way to do this IMO is to draw an "invisible"
(e.g. position the sprite, but don't actually draw it to the screen)
1x1px sprite at the position of the mouse cursor, then collide that
against the group containing all the country sprites.
I get the idea and I also think I will try this.  Do you have any concrete
examples of this in action?