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

Re: [pygame] Pygame Problem Solving 1



                elif event.type == pygame.MOUSEBUTTONDOWN:
                    print pygame.mouse.get_pos() + (25,25)
                    print MAP.blocks[2].point
                    mouse = pygame.mouse.get_pos()
                    rng = mouse[0] - MAP.blocks[2].point[0]
                    rng2 = mouse[1] - MAP.blocks[2].point[1]
                    if rng in range(0,25) and rng2 in range(0,25):
                        print "True"
Checks to see if I'm clicking the third square rendered from the text file map.

http://cleanscript.com/Map/Map2

On 9/13/07, Lamonte Harris <pyth0nc0d3r@xxxxxxxxx> wrote:
Whups major flaw forgot to check if the height also.


On 9/13/07, Lamonte(Scheols/Demonic) < scheols@xxxxxxxxx> wrote:
I'm sort of doing it a big differently, right now I'm just testing, but I can tell if my mouse position is in the second square block like so:

                elif event.type == pygame.MOUSEBUTTONDOWN:
                    print pygame.mouse.get_pos() + (25,25)
                    print MAP.blocks[2].point
                    mouse = pygame.mouse.get_pos()
                    mouse = mouse[0]
                    block = MAP.blocks[2].point[0]
                    rng = mouse - block
                    if rng in range(0,25):
                        print "True"

snippet from my script. Since all the squares are 25,25 height and width  Theres an even amount so it subtracts the first tuple value in the mouse position from the first tuple value in the block position then I check to see if I ranged with in the width of the block to see if im still in it and it works out well.  I'm getting to what I want my script to do :).


On 9/13/07, DR0ID < dr0id@xxxxxxxxxx> wrote:
Hi

as you can see from the documentation, the mouse event provides you the position:

    MOUSEMOTION      pos, rel, buttons
    MOUSEBUTTONUP    pos, button
    MOUSEBUTTONDOWN  pos, button

so no need to use pygame.mouse.get_position() (well it another way to get it, perhaps if you dont have events).

I think you can find the tile colliding with the mouse faster doing:

x_tile = mouse_pos_x/tile_width
y_tile = mouse_pos_y/tile_height

instead of using a rect collision function. (well if you have scrolling then you need to add some offsets,
but that should not be that difficult).

As for the tileset and map I would go the way as Luke has proposed.

~DR0ID






Luke Paireepinart schrieb:
> Lamonte Harris wrote:
>> Problem Solving 1
>> --------
>> Rendering a map switching script.  There will be a simple window.  At
>> the bottom if tge screen it will say pick a map.  Using the mouse
>> events and positioning I will be able to click on the squares that
>> will represent certain maps at the bottom of the screen and the
>> different maps will be displayed.
>>
>> How to do that?
>> ---
>> First make my "genmap" class then in the __init__ function add 2
>> variables "self.cmap " and "self.blockrect".  "self.cmap" would
>> represent the current map to be displayed.  If there isn't a variable
>> assigned then the area above the boxes will not be displayed.  Make a
>> function to display the tile maps.  With that function I would append
>> square rects to te " self.blockrect" variable for later use to detect
>> if the current mouse position will match any of the rects when I left
>> mouse click on the pygame window.  In that list, there could be
>> tuples inside the list for instance:
>>
>> self.blockrect = [((0,0,0,0),1)]
>>
>> The first value in the tuple would be the map rect second value in
>> the tuple would represent the map name in the directory to load.
>>
>> How would I get the current top surface of where my mouse is located?
>> Just giving a guess, I was thinking of making a square surface thats
>> the same size of the sqaures at the bottom of the screen but the
>> sqaures at the bottom of the screen have different possitioning
>> meaning its easier to located different maps. Like example:
>>
>> Pygame Window:
>> ----------------------------------------
>> |                          ___       |
>> |          |--\_/--|  /-\ |___|      |
>> |          |_/\/\_| /---\|            |
>> |                                      |
>> |                                      |
>> |                                      |
>>  --------------------------------------
>> |   ------     ------    ------         |
>> |   |    |     |     |   |      |         |
>> |   ------     ------    ------         |
>>  --------------------------------------
>>
>>  Thats above is the whole window its all one big part but sort of
>> seperated if you get what I mean.  Probably by a pygame line.
>>
>>  So I was thinking if I made an invisible rect that moved when the
>> mouse moved then I left clicked and it would get the current surface
>> position of the rect, then it would run a function to see if that
>> current position matches any of the map squares at the bottom of the
>> screen then the " self.cmap" variable would update.
>>
>>
>> Does anyone think my theory will work, I've only written then down at
>> school and is attempting to do this this weekend for some more
>> learning with pygame.
> It's a little hard to follow.
> For example, what is this talk about an invisible rect that follows
> the mouse?
> All you care about are mouse clicks.  So when the mouse clicks (when
> you're parsing events and you come across a MOUSEBUTTONDOWN or
> whatever the event is called) you just call pygame.mouse.get_pos() and
> you have the current position.  You don't have to constantly track the
> position.
> and I'm not sure what your self.blockrect and self.cmap stuff was about.
> I would say to do it like this off the top of my head:
> import pygame, os
> class Tileset(object):
>  def __init__(self, tiletuplelist):
>    """tiletuplelist is of the form [(tilename, tilelocation)]
>    self.tiles = {}
>    for tilepair in tiletuplelist:
>      self.tiles[tilepair[0]] = pygame.image.load(tilepair[1])
>
> class Map(object):
>  def __init__(self, mapdata, tileset):
>    """mapdata is a 2d array of strings that are the tilenames for each
> location."""
>    self.map = mapdata
>    self.tileset = tileset
>  def drawmap(self, location, maxh, maxw, screen):
>    """location is a tuple"""
>    #using position and maxwidth and maxheight, and the
>    #data about the tile sizes from the tileset, you can make sure
>    #you don't draw out of bounds.
>
> So basically you'd construct one tileset like this
> tiles = Tileset(['grass','grass.bmp','dirt','dirt.bmp'])
> Then you'd make a map like this:
> map1 = Map([["grass","dirt"],["dirt","grass"]], tiles)
>
> then whenever you wanted to draw map1 you'd just call map1.draw
> and it would draw itself using your tileset.
> So if you wanted to change your tileset later you could, easily.
> Also, because the tileset is referenced by variable names instead of
> indices, it'll be easy to list all possible tiles for selection.
> and since each map is a different object, you just have to clear the
> screen and call map2.draw() when you want to switch maps.  It makes
> everything simple.
> I'm not claiming the code works or that's the best way to do it, but
> that's just what I came up with on first glance.
>
> Also as you were mentioning earlier, you wanted to use rects of
> existing tiles to collide with the mouse to see if they had selected a
> tile, but you want them to be able to select any tile, even blank
> ones, right?
> Blah, there are too many considerations that are going to be
> particular to your implementation. You can look at Phil Hassey's PGU
> for ideas on tile editors.  His is pretty slick.
> -Luke
>




--
Join phpdiscovery.com Now!