7 Replies - 1856 Views - Last Post: 13 March 2013 - 06:25 PM Rate Topic: -----

#1 Kru933  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-March 13

Best way to implement a 2D game's map

Posted 12 March 2013 - 04:19 PM

*I am coding in Java but my questions are not coding specific and are based on concepts. Sorry if the title was vague, I tried to think of something that described my problem better but I came up blank. If a moderator/admin would like to change it to something more appropriate, go ahead.*

I am currently coding a 2D game and I am trying to copy Zelda:Link to the Past as closely as I can. Not because I am trying to make a clone of the game, but because I loved nearly everything about the game as a kid, and I would like my game "engine" to function in a very similar way. The end result should be entirely different from Zelda. Instead of having the map built out of many tiles, I want to have the map made up of one large image. If you remember from LtP, when the player moved, the map moved with him.

The biggest problem I am having right now, is trying to figure out the most effective way to draw the map.
Currently, I have one large image which is displayed on the screen and everything(player/scenery/ai sprites) is layered on top of that. If the player moves close enough to either side of the screen, instead of the player moving, the screen is pushed in that direction (every time the game timer runs, it checks the player coordinates and whether or not the directional keys are being pressed.

It appears that this works well enough for my purpose. I place ai/scenery objects(things with collision) by using pixel coordinates. Each object has its own set of X,Y values and functions that can set/get them. Each time the map moves because of the player, every item has its coordinates reset.
Before implementing this, I assumed that it would work pretty well. Once I actually got it working though, it appears that the game timer runs too fast for this method to work very well, because the items slowly begin to get out of alignment and you can tell that the background and the scenery items are not one image.

If I limit my game to the size of the screen, this is obviously not a problem. I could make it into a bunch of tiles and every time the player goes off screen the next tile is loaded but this is not what I want to do, and it ruins the ideas that I have for this game.

example:
Control Class(this code runs at set intervals and acts as the game timer):

int camera = 150;
public void gameTimer(){
if(player.getX() < camera && player.getLeft()==true){
backgroundX+=1;
adjustX=1;
adjustY=0;
}
scenery.move(adjustX,adjustY);
}

Scenery Class:
public void move(int tempX, int tempY){
x+=tempX;
y+=tempY;
}


This obviously not the entire game and massive parts have been excluded. I felt it would be easier to explain if I showed some simple code. I don't need help writing code, just the concepts behind it. This is Java by the way.

Long story short, does anyone know of a more efficient way to draw the map and populate it with scenery that stays in put? If so, could you please explain? If you could explain how Link to the Past did it that would be even better. Or, is my concept solid and I am just implementing it incorrectly? Although I doubt it, because it seems like it is horribly inefficient to just change the coordinates of everything.
I can post example code of what I have currently but this is mostly conceptual and I would just like to hear of some people's ideas.

Is This A Good Question/Topic? 0
  • +

Replies To: Best way to implement a 2D game's map

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Best way to implement a 2D game's map

Posted 12 March 2013 - 09:21 PM

You need to partition your space - it's never a good idea to load a huge image into a game and overlay with objects; the memory footprint will be a lot larger than it needs to be.

Why aren't you using a tiling system?
Was This Post Helpful? 2
  • +
  • -

#3 Kru933  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-March 13

Re: Best way to implement a 2D game's map

Posted 13 March 2013 - 07:22 AM

By partitioning space, you mean using a tiling system right? As for the overlaying of objects, is that the best way to do what I am trying to accomplish?

I like the idea of the map just being there and not having to worry about transitioning from tile to tile.
So far, with a 5720x1632 image, around 215 enemies can move around the map, randomly, without lagging the game at all. I figured that there was no way that I would need more than 50 enemies loaded at a time so I thought that it would run fine.
Also, I wanted the enemies to walk around the entire map and not be confined to one tile, although I guess I could make a work around for that, just means a little more coding.

This post has been edited by ButchDean: 13 March 2013 - 11:54 AM

Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Best way to implement a 2D game's map

Posted 13 March 2013 - 11:56 AM

The body of you post appeared twice, just fixed that up for you.

Yes, I do mean tiling. And you will still be able to overlay your objects on that tiled map. Unless you keep things very simple you're going to run into performance issues with your current implementation.
Was This Post Helpful? 0
  • +
  • -

#5 Kru933  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-March 13

Re: Best way to implement a 2D game's map

Posted 13 March 2013 - 02:21 PM

Yeah, I accidentally double clicked post. Was going to fix it but I guess I can't edit my own posts.

OK, I will break up the map then. I am assuming that you are talking about having tiles that are the same size of the screen, right? Overlaying over that would be easy because nothing would have to move.

But I would like to have some tiles to be larger than the size of the screen, mainly because I like how the "camera" moves with you and I feel that it gives the map a little more depth because certain things are hidden until you walk over to it. Say if I want one tile to be 300 pixels wider than the screen and I would like to screen to move with the player. Like I said before, I have everything (background/scenery) move together to simulate that the player is moving. After fiddling with the game time, this now works without messing anything up.

My main question is, would this be the best way to achieve what I am trying to do?
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Best way to implement a 2D game's map

Posted 13 March 2013 - 04:19 PM

I've kind of suggested the best approach twice now. Tiling will do everything you want, there is no need to count pixels, you just load a tile that is likely to be visible off-screen so that when it appears it's already loaded.

This isn't complicated.
Was This Post Helpful? 0
  • +
  • -

#7 Kru933  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-March 13

Re: Best way to implement a 2D game's map

Posted 13 March 2013 - 04:38 PM

Alright, thanks man.
Was This Post Helpful? 0
  • +
  • -

#8 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Best way to implement a 2D game's map

Posted 13 March 2013 - 06:25 PM

No problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1