Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Drawing a map grid for a game

Hey I am trying to make a dungeon crawler and I am stuck on the map generation. I want there to be random rooms generated but before I can do all of that I have to actually draw the map on the screen. I want the map to be top down and pretty much be a grid that I will later add the floor image to it. My way of thinking is to create a JFrame and draw a Rectangle2D with paintComponent and a for loop to make a grid of squares. Is this the right way to go? And Also how would I get the character to move from square to square with a keyListener?

EDIT: A dungeon crawler is a top-down game where you control one character and move him around a grid. You kill monsters, get weapons and items they drop, level up, etc. Right now I just want to figure our the best way to print the map on the screen. i am starting with just a grid where the character moves from square to square and hitting the arrow keys. Then I will move on to random map generation!

like image 445
user1198199 Avatar asked Aug 26 '26 13:08

user1198199


1 Answers

It's a good idea to create abstraction level first. Start from array, that contains information about each cell (empty, obstacle, enemy, player, item etc.).

Something like that

int[][] a={
  {0,0,0,0,0}, 
  {0,1,0,0,0}, //"1" - obstacle
  {0,0,1,0,0},
  {2,0,1,0,0}, //for example "2" is player
  {0,0,1,0,0}
}

Now just draw your rectangles (or images, which actually looks better :) ) using to that map. Usually each cell should be same size, so just draw rectangles with some step.

like image 71
Dmitry Zaytsev Avatar answered Aug 29 '26 04:08

Dmitry Zaytsev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!