sScroll package - - Simple 2D Side Scroller Engine Demo 1

This is a project I just started on my own. I have built a code base from the ground up that I think I will be able to use as the foundation to make a pretty decent side-scroller in Java. I have borrowed images for animation sprites for Zero… obtained from images.google.com. As well as the background freely obtained.

Anyone that would like their art featured in a game, or engine demo that will be open source once it gets to its point of best usability. I intend to see this project through the rest of my time at Auburn University which will be at least a couple years. 2+. So I imagine I will have plenty done on that. If anyone is looking for a project to do in Java. Or if you know anyone that can make any graphics that I could use for side scrolling type games. Or any 2D backgrounds or sprites at all.

Right now there are a couple things that I am trying to accomplish:

  1. Calculating the actual FPS in my run() method using my Thread object, polling the system time, implementing the Runnable interface. I need to be able to calculate FPS, ect. and output to the screen, fairly often and accurately for debug purposes.

  2. Add a class that manages multiple JFrames and JPanels to render HUD elements to, as my game will not be full screen. I am not sure that I can make this work for an applet that can be run completely in-browser and that was a definite plus when looking at this project. I believe everything I have as of now should run just as fine in an Applet as it does using my JFrame and running as a java application.

  3. Any tips on the best way to handle keyboard input. I notice that when a button is held down, it sends tons of key down events, but then only one key release event when u let up.
    I have found that this can cause problems when using complex boolean operations inside an if else if … statement. This problems seem to disappear when using a switch. Probably has to do with the fact different mechanics of how testing is done with a switch.

  4. The best way to handle state for different objects. Currently for any objects that I need to keep strict track of several states that the object could be in for logic purposes. I currently have several binary flags I call state flags. which can be set to true or false. The combination of all these flags and their values are what makes up an objects state in my code. I was thinking this might be good to put these flags in some kind of order, that I can generate a binary number with, that I can test for specific binary strings to do complex state checks and therefore more complex state logic.

If any programmers have any thoughts on any of this e-mail me at CoryG89@gmail.com

Graphic artists that would be willing to work on an open source project e-mail me at CoryG89@gmail.com The art that I am most interested in making would be for game demos with a style like Megaman or Metroid, but more fast paced. I want some really trippy retro backgrounds though.

First off, nice work! I remember when I was in college I started a similar project with a couple of friends. Not only did it help my programming/communication skills it also helped me get a job once I graduated.

Yes, if I remember correctly it also behaves differently on Win and *nix platforms as well. I recommend keeping your own keyboard states and updating them when you see appropriate.

Here is some pseudocode:


   class Key {
     boolean isDown; // is the key pressed?
     TimeUnit	nextTime  = new TimeUnit();  
     TimeUnit	totalTime = new TimeUnit(); 
  }

  Key keyboardKeys = new Key[255];
  Queue<KeyEvent> keyEvents; // key events this update

  // Process your input
  public void update(TimeStep timeStep) {
    while( ! keyEvents.isEmpty() ) {
       KeyEvent e = keyEvents.poll();
       processKeyboardEvents(timeStep, e.getKey(), e.isDown() );
    }
  }

  private void processKeyboardEvents(TimeStep timeStep, int key, boolean down) {

		if ( key > 0 && key < this.keyboardKeys.length ) {
			Key keyEvent = this.keyboardKeys[key];
			
			// key pressed?
			if (down) {
				// check to see if this is a newly pressed key
				if ( ! keyEvent.isDown ) {
					keyEvent.isDown=true;
					keyEvent.nextTime.toZero();
					keyEvent.totalTime.toZero();
					
					KeyEvent event = new KeyEvent(this, key);
					processKeyEvent(event, true);	
				} 
				// do not allow multiple key presses in a given time frame
				else if ( keyEvent.totalTime.lessThanEq(KEY_DELAY) ) {					
					TimeUnitPlus(keyEvent.totalTime, timeStep.getElapsedTime(), keyEvent.totalTime);					
				}
				// this key has been held, so let it rip.
				else if ( keyEvent.totalTime.greaterThan(KEY_DELAY) ) {
					TimeUnitPlus(keyEvent.nextTime, timeStep.getElapsedTime(), keyEvent.nextTime);
					if (keyEvent.nextTime.greaterThan(NEXT_TIME) ) {
						keyEvent.nextTime.toZero();
						
						KeyEvent event = new KeyEvent(this, key);
						processKeyEvent(event, true);
					}
				}
			} 
			// key released
			else {				
				keyEvent.isDown=false;
				keyEvent.nextTime.toZero();
				keyEvent.totalTime.toZero();
				
				KeyEvent event = new KeyEvent(this, key);
				processKeyEvent(event, false); 
			}
		}
	}

   private processKeyEvent(KeyEvent event, boolean isDown) {
      // let listeners know of the KeyEvent
      for( KeyListener l : this.listeners) {
          l.onKey(event, isDown);
      }
   }

// java KeyListener
void keyPressed(KeyEvent e) {
   keyEvents.add(e); // queue them up so you can process them in the "update"
}
void keyReleased(KeyEvent e) {
   keyEvents.add(e); // queue them up so you can process them in the "update" 
}

This will probably work for simple state machines, however I recommend looking into a more object orientated Finite State Machine class. A quick google search on the topic gives some pretty good examples, this one in particular seems helpful: http://ai-depot.com/FiniteStateMachines/FSM.html

Hope this helps,

Tony