1

Finally decided to learn 2d (for now) java game programming. Am working on a game that has a central object that the user will guide with the directional keys. I have that working perfectly, cobbled together from examples and tutorials I've found.

I'm using this method of generating colored background tiles but I'd like to scroll (move) the background as the primary object the user is moving reaches the window edges. I'm fairly sure I can make that work, I have the basics in place, but I can't find a good tutorial or actual demonstration of a way to continue to generate additional tiles to fill in the space the user is moving too.

At this point, this is purely background and I have no need to save the exact tiles generated - but eventually I would like this ability. I'm sure I'll have to find a way to divide the areas into "chunks" like minecraft does.

But for now - how can I continually fill in the area with the same pattern? Or is there a better way to create the tiles that's better for this?

Community
  • 1
  • 1
helion3
  • 34,737
  • 15
  • 57
  • 100
  • What are your requirements for the map? Does it need to be static and unchanging, even after scrolling parts of it off screen? Infinitely large? No repetition? – Patashu May 24 '13 at 04:29
  • Currently, no concern. I'd like it to be very large, so infinite is a good plan. I'd like to eventually change the coloring of the background the further from "spawn" the user goes, but I assume that's just using different colors for different coordinate ranges. I don't need to remember the exact state of tiles seens/already passed - I'm fine with regenerating the tiles for that "zone" when the player returns. Eventually, I'm sure I'll want to save them – helion3 May 24 '13 at 04:33
  • Imo you want something like what Minecraft does, Chunks. A chunk is an x*x group of tiles. You keep the chunk the user is on and the 8 surrounding chunks (or more) in memory. When a chunk is out of range you throw it out. When a new chunk is needed you write it. Then every frame you draw you can print from the current chunks using some math. You can check every frame the character moves if you need to change chunks. – Patashu May 24 '13 at 04:35
  • Thanks, I agree that's what I need - but for now, I'm simply trying to figure out *how* to display and move those tiles. The technique I've got now only creates a static pane, which I'd have to move around manually - and can do - but I'm new to java UI programming, and have no clue how. – helion3 May 24 '13 at 04:43
  • Ok. Notice how that code has a loop that ends with `g.fillRect(x, y, rectWidth, rectHeight);`? Simply change how x and y are calculated, and you will be moving the grid around. (Basic math is all you need.) – Patashu May 24 '13 at 04:44
  • Can you throw a way simple example at me? I'm not new to programming but this day 1 with using java for anything game/ui related. I need to learn how fill rect works, etc. Would that not make a tile that's just larger than I wanted, not actually make additional columns? – helion3 May 24 '13 at 04:53
  • fillrect just marks a corner at x,y, finds the opposite corner at x+rectWidth, y+rectHeight and fills in a rectangle that large. For example, by adding C to every x and D to every y before doing a fillRect call, you will offset all tiles by C on the x-axis and D on the y-axis. – Patashu May 24 '13 at 04:56
  • Something like [this](http://stackoverflow.com/questions/16050723/java-applet-game-2d-window-scrolling/16052048#16052048)? – MadProgrammer May 24 '13 at 04:59

1 Answers1

1

Instead of a solid color you can use a TexturePaint, as shown here. Let your model contain a reference to the desired texture for each grid cell. Let your view use a flyweight pattern for rendering, as illustrated here.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I'll look into this, thanks. Using this method though, how would I scroll the background? This is the main question I had and still have no idea how to handle it. I need to move the background as the user moves, paint new tiles in the area they move to, and likely remove tiles in the areas being moved off screen. – helion3 May 24 '13 at 14:45
  • Ideally, you's just change grid coordinates in the model and let the view repaint itself. Another approach is shown [here](http://stackoverflow.com/a/7203419/230513). – trashgod May 24 '13 at 15:53