Sign in to follow this  
greth22

Object Location management in 2d scrolling (python)

Recommended Posts

Hey all, I'm using Python, specifically the pygame module to work on a 2d side-scrolling game. I was wondering if there were any suggestions for room-contents management. In case that's unclear, I'm basically asking what the best way to organize the data of objects in a room. I'm leaning towards creating a list of sprites for each room (since any character-interacting object will extend at some point pygame.sprite.Sprite) to hold that data, but I'm not sure how best to index them in terms of how to draw them. Should I have them all relative to a global origin, then adjust it as the character moves? I'm really in the dark as to what the most efficient way would be (if an object is offscreen, I don't want to mess with it, be it checking collision or what-have-you). Thanks for any help!

Share this post


Link to post
Share on other sites
Hi,

You could actually have a list of sprites in an array and each screen as a linked list of screens. That way you could easily manage the data in the game. I am assuming as the game is 2d game, you are going to use orthographic projection if you use OpenGL or Direct3d. If thats the case I would use exact screen coordinates for the position of the screen.

Share this post


Link to post
Share on other sites
I'm not sure how the concept of a side-scroller works with the concept of 'rooms', since the first tends to imply a continuous landscape while the second implies a discrete landscape. Could you clarify exactly what you're trying to do?

Generally I store everything using global coordinates. Then you calculate their position relative to the player by merely subtracting their position from the player's position. You can then decide not to draw them if this relative position would put them offscreen.

Share this post


Link to post
Share on other sites
Well, it's both rooms and scrolling, in a Metroid-like way. As in, within a room, it may scroll, but there are discreet rooms encapsulating that data.

Thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by greth22
Well, it's both rooms and scrolling, in a Metroid-like way. As in, within a room, it may scroll, but there are discreet rooms encapsulating that data.

Sounds like you've already pretty much figured out the coordinate systems, you just need to formalise them. I'd have a collection of Room objects, each with their own local coord system (say, the lower left corner is a room's origin). Each Room would then have a collection of Entities/Objects/Whatever with all the gameplay objects inside of it. These entities would have their own coordinates relative to the rooms themselves. Rooms may or may not need a position which specifies where they are in the global world coord system. You may want to have Door objects which link rooms together, objects colliding with a door can be removed from the current room and placed in the target room at the coordinates of the door in that room.

You may want to look into Portal based rendering. It's a method for rendering 3d worlds but the concepts of breaking space into separate rooms/sectors with doors/portals linking them is basically the same.

The neat thing about this method is you can pull off odd spatial abnormalities, like rooms which open into themselves or corridors which are endless in one direction only. [grin]

Share this post


Link to post
Share on other sites
Quote:
Original post by OrangyTang
You may want to have Door objects which link rooms together, objects colliding with a door can be removed from the current room and placed in the target room at the coordinates of the door in that room.

You may want to look into Portal based rendering. It's a method for rendering 3d worlds but the concepts of breaking space into separate rooms/sectors with doors/portals linking them is basically the same.

The neat thing about this method is you can pull off odd spatial abnormalities, like rooms which open into themselves or corridors which are endless in one direction only. [grin]


Well, on that topic, I'm not really sure how to point those data structures at each other here in Python. I imagine that each Room would contain a list of Door objects, with their location coordinates and a Door object as their target... but does that mean I have to have a copy of each Door for all Door objects which reference it?

Thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by greth22
... but does that mean I have to have a copy of each Door for all Door objects which reference it?

I'm going to assume you mean "for all Room objects which reference it".

Python is reference counted. If you have two adjacent Room objects which reference the same Door object, only one Door object needs to exist. This introduces complexity, however, as object ownership is muddled. Typical strategies for dealing with such situations include the creation of "manager" objects which own the objects with multiple references.

Given the structural overhead of such an approach, and the relatively minor costs of just having Room objects hold their own Doors, the latter might be a better alternative. The only challenge will then be synchronizing any data between entering a Door in one Room and exiting in another.

Another option, difficult to set up but easy to use, is to have a Room hold a reference to each of its Doors, and a Door hold a reference to both of its Rooms. This allows for facile traversal from Room to Room via Doors at runtime.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this