Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 04 Sep 2012
Offline Last Active Nov 19 2012 12:46 PM

Topics I've Started

JFrame KeyEvent question

08 November 2012 - 09:42 AM

So I'm working on a rogue-like, and right wrong or indifferent, I am using swing. My current setup has a GameWindow class that extends JFrame, and implements KeyListener, a GamePanel class that extends JPanel, a GameState class that holds the game panel of whatever state I'm in, and also holds that state's logic, and a GameController class that knows which state the game is in, and can cycle through states. The way I am passing KeyEvents is the frame has the keylistener, and when a key is pressed it passes that KeyEvent to the GameController, which then on the next cycle through the GameLoop it passes it to the GameState through the state's input function, and the state proccesses accordingly. This sounds good, but for whatever reason, it doesn't seem to be working. I am showing the relevent source code below, but I have a hunch my error is in method rather than syntax? Any help would be greatly appreciated.


GameWindow code
package main.java.dungeon.gui;
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import main.java.main.GameController;
public class GameWindow extends JFrame implements KeyListener{
private static final long serialVersionUID = 1L;
private static GameWindow gameWindow;
private static JPanel activePanel;

private GameWindow(){
  super("Dungeon Crawler");
  setBounds(0, 0, 640, 480);

public static GameWindow create(){
  if(gameWindow == null)
   gameWindow = new GameWindow();
  return gameWindow;

public void setActivePanel(JPanel panel){
  activePanel = panel;
  add(activePanel, BorderLayout.CENTER);
public void keyPressed(KeyEvent e) {

public void keyReleased(KeyEvent e) {
  // TODO Auto-generated method stub

public void keyTyped(KeyEvent e) {


GameController code
package main.java.main;
import java.awt.event.KeyEvent;
import main.java.dungeon.GameState;
public abstract class GameController {

private static GameState currentState;
private static GameState nextState;
private static KeyEvent keyPressed;

private GameController(){}

static void input(){
  if(keyPressed != null){
   keyPressed = null;

static void logic(){
  nextState = currentState.getNextState();
  if(nextState != null)
   currentState = nextState;

static void update(){

static void setState(GameState state){
  currentState = state;

static GameState getState(){
  return currentState;

public static void keyPressed(KeyEvent e){
  keyPressed = e;

Main Menu State code
package main.java.dungeon;
import java.awt.event.KeyEvent;
import main.java.dungeon.gui.MainMenuPanel;
public class GameStateMainMenu extends GameState{

private static final String START_GAME = "Start game!";
private static final String OPTIONS = "Options";
private static final String QUIT = "Quit";
private static final String INDENT = "		  ";

private int pointer;
private GameState nextState;
private boolean pointerUp, pointerDown, changeState, update;
private String text;

public GameStateMainMenu(){
  gamePanel = new MainMenuPanel();
  pointer = 1;
  pointerUp = pointerDown = changeState = false;
  update = true;

public void input(KeyEvent keyEvent) {
  if(keyEvent != null){
   case KeyEvent.VK_UP:
	pointerUp = true;
	update = true;
   case KeyEvent.VK_DOWN:
	pointerDown = true;
	update = true;
   case KeyEvent.VK_ENTER:
	changeState = true;
public void logic() {
  if(pointerUp && pointer != 1)
  if(pointerDown && pointer != 3)
   if(pointer == 1)
	nextState = new GameStatePlaying();
   if(pointer == 2)
	nextState = new GameStateMainMenuOptions();
   if(pointer == 3){}
	//TODO generate quit code
public void update() {
   text = "\n\n\n\n" + INDENT + (pointer == 1 ? ">" : " ") + START_GAME +
	 "\n\n\n" + INDENT + (pointer == 2 ? ">" : " ") + OPTIONS +
	 "\n\n\n" + INDENT + (pointer == 3 ? ">" : " ") + QUIT;
   update = false;
public GameState getNextState() {
  return nextState;

and the game panel's code
package main.java.dungeon.gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JTextPane;
public class MainMenuPanel extends JPanel{

  private static final long serialVersionUID = 1L;

  private JTextPane text;

public MainMenuPanel(){
  setPreferredSize(new Dimension(640, 480));
  setBounds(0, 0, 640, 480);
  text = new JTextPane();
  text.setPreferredSize(new Dimension(640, 480));
  add(text, BorderLayout.CENTER);


public void setText(String s){

Lots of code to read, I know. Thank you for taking the time to look at it! If there is any other code you need to see, please let me know! (Also, this is very much a work in progress, I know there are a lot of things that will need cleaned up in this as I work through it.) Thanks again.

Rogue-like tile system display question

31 October 2012 - 07:34 PM

So I'm working on a roguelike, and I have a question about drawing the map and all the entities and items that reside on it. The way I have it programmed at the moment, is the board is a 2 dimensional array of tiles. Each Tile has its own char to represent it, and contains an Entity member. This way, when the draw method is called, each tile spits back either its own icon, or if it has an Entity, the Icon belonging to the entity. These chars are all concatenated into the string that is eventually thrown to the display. Needless to say, this works, though I'm not certain it's the best way to do it. 1.) From a coupling perspective, there's really no reason a tile should know the entity that is on top of it. and 2.) now for every tile, the if(Entity != null) is being checked to determine what char to display. When 99% of the tiles are empty, this seems like an unnecessary check. I don't imagine this is serious overhead, but it still doesn't feel clean to me.

All that said, I've been stewing on alternatives, and none of them sounds particularly appealing either. These are what I've come up with:

1.) Have a graphics manager class with a Board reference, and a list or hashmap of Entities. Then, for every x/y on the display, it checks to see if an entity resides there from the list/map, if so displays it, else displays the tile's icon. This just seems hideous to me, to have to iteratively go through a list for every tile? Though I suppose I could have Entity implement comparable, and make the natural sorting order go left to right, top to bottom, so that only the next entity in the list is being checked vs the coordinates you're drawing at. Still feels heavy...

2.) Same design as above, graphics manager with board and entity references. This time, take the board, and convert it to a char[][] array. Then go through the entity list, replacing the elements of that array with the entities icons. Finally convert the char[][] to a string and display it. This avoids a lot of the iteration problems that are encountered above, but it requires instantiating a temporary array, filling it, and then converting that array back to a string, which all of these steps are iterative in and of themselves, so this doesn't seem like it would end up saving anything.

I would love to be able to simply replace char at index in a string, but I haven't found that such a method exists. (Even if it does, are the underlying mechanics similarly iterative, so would it even save anything over the options described above?)

Either I'm over-thinking this, and one of the above approaches is fine/the best way to do it, or there's some other method that I am missing.

Thoughts and suggestions? Thank you in advance!


TDD in game development?

04 October 2012 - 03:10 PM

Kind of a random question that might be putting the cart before the horse... I've recently been reading Clean Code - A Handbook of Agile Software Craftsmanship at the suggestion of another user on this site in another post. One thing that is mentioned is unit-testing. Being somewhat new to programing (only having programed Tetris, Breakout, a Maze game, and starting on a Gradius-type clone) I had no idea what this even was. So, I did some reading on the concept, and ultimately learned of Test Driven Design (TDD) and conceptually it seems like a good idea.

My two questions:

1.) Is TDD applicable to game development? I'm guessing the answer is yes for large projects/teams, but I don't know about small project games like what I've mentioned I've done/am working on.

2.) If it is, what kinds of tests would one run?

This is probably a case of "If you have to ask, you don't know enough to use it yet" but I want to get an idea if I should spend time learning/practicing the concept, or if it's something I can skip-over.

Thanks in advance for the feedback!

Project Planning? (UML?)

18 September 2012 - 06:31 AM

I am pretty new to programming (I have programmed 3 basic games, tetris, breakout, and a maze with enemies) and am looking to start on a slightly bigger project. Not entirely sure what yet, probably some kind of side-scroller. (Possibly a platformer, or maybe a Gradius clone.) Either of these options would be big enough that programming-as-I-go will probably set me up for disaster, or having to redo code several times, so I am looking into having at least a rough framework sketched up before I start coding. I am wondering what tools or strategies other devs use/have used that might be helpful. I have read a little bit on UML, but everything I have read about it has been mixed reviews. (Also, there are all different kinds of diagrams and charts to it, and it sounds fairly complicated, so I don't know if it's worth the time investment to just learn a diagraming method, especially when reviews are mixed.)

One site I have found and dabbled around with (it's not programming specific) is www.exobrain.co, which seems useful for mental mapping. In its simplicity though, and having only just started with this, I don't know if it will provide everything I need. I have attached a picture of a section of my longer term project that I have started planning on here. My scheme was to pick a color for Class, Interface, Member, and Method, so for each class, I could create all the members and methods I thought it would need, and keep them clear by color coding. I don't think this is a bad method, but I'm curious what strategies other people use.

Thank you,

Attached File  Exobrain.png   83.9KB   90 downloads

Timer vs. thread.sleep();

11 September 2012 - 09:51 AM

I am new to Java (and OOP in general) and have recently been trying my hand at programming some basic games. My first two attempts used a Timer, with an ActionListener to call for game updates, which seemed to work pretty smoothly (granted these games only used a text interface.) In preparing for my most recent game (a breakout clone) I noticed another user on the forum using a sleep() inside a game thread. My initial observations of this in both his game, and in my own when I implemented it, was that the speed didn't feel constant, like the game would randomly speed up and slow down, presumably on processor load. Is this just a problem with implementation? Which is the preferred method, Timers, or threads? If it is threads, which my gut feel seems to be, how would you avoid this problem? Should the game thread that updates movement etc be a separate thread than the one that calls for screen refreshes?

Thank you for the guidance.