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

Re: [pygame] Working with maps



After reading all these posts, it seems to me the simplest way to do
it, assuming you have to draw out all the countries anyway, is to draw
a layer on the original image with each country being its own RGB
value.  So just draw solid colours of the countires, being as precise
or vague as you care to be, not worrying about keeping to specific
geometric shapes.  The differences could be on just one of the RGB
colour values, so (255,255,100), (255,255,95).  Easy to fit them all
on if you're not really worried about having high-contrast colours.
The layer could be invisible to the player, so, two surfaces.  One
that's blitted to screen and the other with the colours that isn't.
When checking for which country the mouse clicked on, as Henrique
Nakashima said, just check the colour value of the modified-colour
surface.  Based on what colour it is, the program will know which
country.  You could even assign the colours simply based on
alphabetical order of the countries.  So country 1 = (255,255,255), 2
= (255,255,250), etc.  Thus vastly simplifying the testing.  And if a
mouseclick is not on a country but on an ocean or something, the
modified-colour surface won't have a colour there so you don't need to
do any further processing on that click.

If you want to add coloured borders to the countries, you could write
a simple algorithm to create yet another surface from your
modified-colour countries.  So for example, you could write out that
any of the custom colours that is completely surrounded by the same
colour pixels is removed.  In this way, all thecoloured pixels that
aren't border pixels would be removed.  You could then blit the
resulting surface over the display surface.  You could further refine
that to create a more complicated border if you wish, perhaps with
fade.  And all these surfaces could be computed and saved as
individual images before run-time, saving on resources.

But I'm just a newb. so maybe this has huge pitfalls I don't see.

On 18 May 2010 12:54, Bill Coderre <bcbc@xxxxxxx> wrote:
> On May 17, 2010, at 9:47 PM, Miguel Bejar wrote:
>
> I have the same problem as the OP, I need to find a way to have a map with
> irregular shapes and have the exhibit different behaviors depending on user
> input. I don't know what the term for that is so I haven't been able to
> start doing any research as to how to go about it.
>
> In general, the problem is to decide WHICH graphical region (the outlines of
> the various countries) encloses a point (the cursor). This is sometimes
> called "Point in Region (Polygon)" testing, but the more common term is "hit
> testing."
>
> This looks long and somewhat
> promising: http://stackoverflow.com/questions/217578/point-in-polygon-aka-hit-test
>
> PyGame has a Sprite system, as others have mentioned. There's a tutorial
> document here: http://www.pygame.org/docs/tut/SpriteIntro.html
>
> Hint: Do not optimize until
> 1) You have something that actually works, and
> 2) It definitely is not fast enough.
> I suggest this because:
> 1) It is very easy to get distracted by optimizing
> 2) Hit testing is one of the more fascinating places to reinvent
> optimizations
> 3) Optimizations often introduce subtle bugs
> 4) PyGame sprites have many of these optimizations already
> 5) http://en.wikipedia.org/wiki/The_Elements_of_Programming_Style
>
>