Difference between revisions of "Scrolling map"
(I'm creating a Maps category. This belongs in it.) |
m (Added link to C# implementation) |
||
Line 20: | Line 20: | ||
== Implementation == | == Implementation == | ||
===Python=== | |||
Here's a handy utility function I use often for scrolling maps in Python: | Here's a handy utility function I use often for scrolling maps in Python: | ||
Line 41: | Line 43: | ||
return p - hs | return p - hs | ||
</pre> | </pre> | ||
===C#=== | |||
[http://www.evilscience.co.uk/?p=587 Here's a link to C# implementation.] | |||
[[Category:Articles]][[Category:Maps]] | [[Category:Articles]][[Category:Maps]] |
Revision as of 12:48, 9 January 2014
A scrolling map is a term used in graphical 2D games, including roguelikes, for a map with a "camera" that follows the player. It is used for maps that are larger than that can be displayed on a single screen.
Implementing scrolling maps is mathematically very simple. Let s be the dimensions of the screen, p be the player coordinates, and c be the coordinates of the upper left of the camera:
- c := p - (s / 2)
All this means is that the camera location is the same as the player location, except offset by half the screen dimensions so that the player is nicely centered in the viewport.
If the player is near the edge of the map, then the blank area outside the map might become visible. If you don't want this area to be displayed, and instead have the camera dock to the edge of the map, then the formula is revised to this, where m is the map size:
- If p < s / 2, then c := 0.
- If p > m - (s / 2), then c := m - s.
- Otherwise, c := p - (s / 2).
Apply this procedure to each of the individual coordinates—first the x coordinates, and then the y ones.
The formula looks rather complicated, but all it says is this:
- If the player is near an edge, push the camera against that edge.
- Otherwise, center the camera on the player.
Implementation
Python
Here's a handy utility function I use often for scrolling maps in Python:
# (Public domain code) def scrolling_map(p, hs, s, m): """ Get the position of the camera in a scrolling map: - p is the position of the player. - hs is half of the screen size, and s is the full screen size. - m is the size of the map. """ if p < hs: return 0 elif p > m - hs: return m - s else: return p - hs