Cobalt
Cobalt

Reputation: 113

pygame- Making mouse-interactive elements on a "scrolling" surface

Ok, so our world map for a Risk-style conquest game we are making is made of a matrix of mouse-interactive "country" objects containing their own values like name, owner, and unit counts.

Right now the entire map is sized to fit within the game screen; we are considering making the map scrollable. A possibly I found was that we generate the map on its own surface that's bigger than the game screen, then blit a portion of the map surface onto a subsurface of the game screen and let player input offset what portion of the map is being drawn at a given time.

I know this type of scrolling works- that's not what this question is about- the question is would using this kind of scrolling prevent us from letting the player properly interact with the countries on the map using the mouse?

We already tried a form of scrolling where we just offset the indexes of the map matrix to move the countries positions that way, but this gave the server problems handling attack orders as the same two countries could be at different coordinates for different players. The scrolling method I described would allow the map itself to remain stationary for all players, but how would we determine the mouse's position relative to the world map as opposed to its position on the game screen?

Upvotes: 1

Views: 222

Answers (1)

sloth
sloth

Reputation: 101142

You have to translate the mouse position on the screen to the world coordinates. When you scroll your map, you generate an offset, and you have to use that for translation.

Say if the map is not scrolled ((0, 0) of the map is at (0, 0) of the screen), and a player clicks on (12, 12), you know she clicked on (12, 12) of the world.

When this player scrolled the map to the right (let's say 100px), then you blit the map surface at (-100, 0) of the screen. Now if the player clicks on (12, 12), you can calculate that (12, 12) of the screen is actually (112, 12) on the map (screenX - scrollX, screenY - scrollY).

So if you want to determinate which country a player clicked on, always use the translated coordinates (not the screen coordinates), and the issue you describe disappears.

Upvotes: 1

Related Questions