creating grid based ascii graphics for a rogue like

So I’m just finishing up a Java 2 class, and I need a project to work on until classes start back up. I’m wanting to make a rogue like game using ascii graphics, but I have a few questions about how to proceed.

I’ve some other postings and guides on this, but none of them quite addressed my questions. I’m planning on using a two dimensional array of chars to create/edit the grid, but my biggest concern is how to output it. I can come up with a few ways to do it, but so far my ideas area really inefficient or cumbersome to work with. (like using jTextFields for each char, using a GridLayout, or just creating separate strings for each row and editing them the chars are changed. These ways would work, but I’m pretty sure there’s a better way of doing it.

So, to put it simply, I need a way to output a 2 dimensional array of chars as a grid, in such a way that the output would redraw anytime one of the chars is changed.

Edit: forgot to mention, that I was to do this without any extra packages. I know that asciiPanel can solve all of this issues, but for now I want to do everything myself from the ground up. Oh, and I’d prefer to not use Java2d or even really any graphics object. I’m really trying to do all of this just as text output (probably in a jFrame).

:point:On my cell. Search t :-Xhe fourm for jcod.

So you essentially want to create 2d tile based game?

You will first need graphics library. Java2d will do for very simple game, use LWJGL or Libgdx for more performance demanding game.


for(int y=0;y<height;y++) { for(int x=0;x<width;x++) { draw(x * tileSize, y * tileSize, sprite); } }

You basically use this pseudo code for drawing your level. Width and height of the level. Sprite is the sprite you want to draw your tiles with.

Essentially its a tile based game, but I don’t want to use sprites, just characters.

I do something similar in my debug modes, by outputting the values of the byte arrays im working with on the screen with my font class, if you’re asking for what I think you are… this method should output the entire byte array on-screen spaced out like a tiled base map:


 //Location of your map, you'll need other methods to change this value to move your map around on the screen. But this will start your generation at 0,0 NOTE: Since you are dealing with ASCII you will probably be fine just using ints instead of floats.
float mapX = 0;
float mapY = 0;

int mapWidth = ??; //Width of the map.
int mapHeight = ??; //Height of the map.
int displayWidth = ??; //Screen size width.
int displayHeight = ??; //Screen size height.
int spacing = ??; //How far apart you want you ASCII "tiles" on your tile map.
byte[][] yourMap = new byte[mapWidth][mapHeight]; //Create byte array matching map height/width. (May need to be an int[][] or whatever[][] depending on what you're doing)

//YOUR CODE TO FILL SAID BYTE/INT/CHAR/WHATEVER ARRAY WITH YOUR "ASCII MAP"!//

//Start looping through the entire byte array.
	for (int x = 0; x < mapWidth; x++) {
		for (int y = 0; y < mapHeight; y++) {
			// Check to see if the coordinates being looked for are visible on the screen. If not, don't bother rendering them.
			if ((x*spacing+mapX > -10) && (x*spacing+mapX < displayWidth+10) && (y*spacing+mapY > -10) && (y*spacing+mapY < displayHeight+10)){
				if (!(yourMap[x][y] == 0)){ //Assuming 0 is a "blank space", but whatever == Blank space here, so you're not trying to output nothing.
					//However you output the text would go here. For example, if you were using the Font class in slick2d:
					font.drawString((x*spacing)+mapX, (y*spacing)+mapY, yourMap[x][y]); 
					//So basically, however you call it, the coords would be (x*spacing)+mapX, (y*spacing)+mapY and you'll be calling character yourMap[x][y]. 
				}
			}
		}
	}


Keep in mind you gain nothing from actually making it ASCII at this point. Once you go through the trouble of using arrays and any real graphics calls you’ve defeated the point of ASCII. If you’re dead set on it, go for it, but keep in mind you’re limiting the audience of your game severely for very little ease of programming/slightly less work.

I’d say he gains an aesthetic.

Yeah, in this day and age of computing, really speed/efficiency of using ASCII is sort of irrelevant. Even the crappiest of netbooks could probably run an “ASCII game” that’s really just a basic tile based game. No point worrying about being authentic. :wink:

Plus it let’s you do stuff like this.

now that is pretty damn cool.

I don’t intend on having any audience for this. I’m just looking for a project to keep myself busy and practicing. At this point I’d really like to focus on core java components without branching out too much into Java2d, or packages like slick2d and the like (or are those called libraries?..what’s the difference anyways?)

Yeah, thats along the lines of what I’m talking about. drawString should do the job. So should I just make that for loop into a method that just gets called any time a change is made to yourMap?

Honestly though, what I would really like is to basically make it a command line based game. I just don’t know how to properly output it.

Pretty much. But it should be in whatever you use for a render loop, since the loop doesn’t so much build the map as display it. You’ll want a similar loop to fill the array somehow or another, depending on how you plan to fill it. That similar loop can be called once right after you create the array to input all the characters. Then use the loop I gave you in your render method to display them all.

As for the libraries, Slick2D and LibGDX are wrappers that wrap around LWJGL (An OpenGL library) to make 2D games. If you plan on using OpenGL with 2D games, I’d recommended picking one of those two libraries. I use slick2d personally, but if you don’t know how to use either it would be more logical to use LibGDX since it’s still being updated.

While I certainly plan on moving onto LWJGL eventually, for the time being, I really want to just use what’s already built into java. I know that java’s built in libraries are really inefficient for 2d game development, but again, I’m really just wanting to make a command line based game like rogue, nethack, etc. I’m probably gonna go ahead and try using drawString, but honestly, I’d prefer to not use graphics at all. I’d really like to just output strings or characters.

If it’s in the command line, then your output mechanism is simply [icode]System.out[/icode] drawString() is for graphical applications, not CLI ones.

Well, you should be able to. I just used drawString because that’s what I use. But the same code should be able to pull yourMap[tileX][tileY] into whatever context you want. Although I’ll be honest, I’m not entirely sure how a faithful-to-commandline tiled map would work. I suspect it is very similar to my method anyway though. Just using something like System.out.drawString/println/printf() instead. Refreshing it when a change is applied to the map.

Here’s a sample of what the code I used does, this is a REALLY early alpha shot of my tile engine. I just filled the array I was using with random numbers and used almost the same code I gave you to display it:

http://sixtygig.com/junk/InDev-2014-05-14-2.png

EDIT: Thinking on this, my tiled engine actually operates a lot like your ASCII engine could. My tile engine just uses a large int[][][] array (3D so I have map layers as well) and assigns the array tileID numbers. Then the renderer fetches what TileID is in that place and renders the sprite associated to it. The only difference is mine fetches images to render, yours would just directly render/printout whatever is in that array.

If you are serious about the command line thing, you can always just use the [icode]System.out.printf( “format-string” [, arg1, arg2, … ] );[/icode] method (Detailed here…).

I mean, the weakness about using the command line to start with is that you’ll have multiple print outs. I made a Tic-Tac-Toe out of that as my first assignment in C++. It is a fun pet project for messing with tables, but in no where practical. Since it seems you are just doing this for the sake of doing it, then I’d suggest just using that.

In the worst case, use the Java2D graphics library (standard since Java 1.4). In my opinion, there is no need to go into OpenGL if this is not intended for the public as this seems like a very simple project. Best of luck.

You could always take the middle ground and use a TextArea with a fixed size font. With a few tweaks to the control properties, you get the look and feel of a standard console window without a lot of the limitations such as “screen scroll” whenever the map is updated/output. Use a similarly styled TextField below the TextArea for user input. With a little more creativity in setting control properties, you should be able to make the two controls appear as one unified “console area”. No external libraries needed. :smiley:

That sounds perfect. I just glanced through the methods for TextArea, and its sounding like its exactly what I was looking for. It even lets you set how many rows/columns there are. Yeah…this will be perfect! Thanks!!!

The only thing it doesn’t have is a method for replacing part of the string at an indicated row/columns. Its got a replaceRange() method, that’ll get the job down, but the parameters are just ints for the beginning and end of the range that you are replacing. However, it’ll be easy enough to convert coordinates to that…