[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Pygame Problem Solving 1
- To: pygame-users@xxxxxxxx
- Subject: Re: [pygame] Pygame Problem Solving 1
- From: Luke Paireepinart <rabidpoobear@xxxxxxxxx>
- Date: Thu, 13 Sep 2007 00:00:16 -0500
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Thu, 13 Sep 2007 01:00:31 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:user-agent:mime-version:to:subject:references:in-reply-to:content-type:content-transfer-encoding; bh=+S8Hls+gDsYD0USmBWzQIc9MSQtXPLy0HBa9MgHqfQU=; b=YIdw3SaXhnE0VBnhEzcs0OpmrWRJsWBKGyVG1EzfzUKox7pYoTHNPVK/P8oF2ioMvjse4dZ8jevmm2n5W+tVYh25GOyVKYrdX0EA4V4smCGher7l7yUZ+I+NepXw4HFvWLCGobnoPMcsxXKL8VJUQ3KKyl+wlub2FSYNoDy2yjw=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:user-agent:mime-version:to:subject:references:in-reply-to:content-type:content-transfer-encoding; b=LTSy2zADuwLeR74qNegzXYT/DWrh9CmXqz6mwjR2dy2US1+Q7UT0DyBqV/73aAI3+vQ0FAbwUo/3EAWl2P4v+44IimtAix9vX23+FDMvG8ihMh+Sc5EvXeclg6afk+M5JKJvn3SAFrtzuqprh1gZmo2xVXvjAUl8HaPzBnJo0k4=
- In-reply-to: <eb79828c0709122111g56786665na6a30085883e579c@xxxxxxxxxxxxxx>
- References: <eb79828c0709122111g56786665na6a30085883e579c@xxxxxxxxxxxxxx>
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
- User-agent: Thunderbird 2.0.0.6 (Windows/20070728)
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