Home arrow Java & J2EE arrow Page 7 - Gaming Development Setup

Generating an event - Java

One important step in creating your own computer games is configuring your development environment. Keep reading to learn how. This article is taken from chapter one of Advanced Java Game Programming by David Wallace Croft (Apress, 2004; ISBN 1590591232).

TABLE OF CONTENTS:
  1. Gaming Development Setup
  2. Playing the Demo Online
  3. Introducing XML
  4. Using Open Source
  5. Finding Multimedia for Your Games
  6. Basics Example
  7. Generating an event
  8. Drawing the background and target
By: Apress Publishing
Rating: starstarstarstarstar / 33
March 02, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

When the player resizes the desktop window containing the game, an event is generated. This event is intercepted by an event listener created in the constructor method. The processing of user input events should be delayed until the update() method is called. For this reason, all the event listeners created in the constructor method simply flag or store the events instead of processing them immediately.

One of the event listeners raises the boolean flag componentResized to indicate that the size of the game screen has changed. You almost always want to handle this event in your update() method implementation by resetting the flag to false, requesting a repaint of the entire screen, and storing the new dimensions in componentBounds as shown in the preceding code.

You might then want to provide additional game-specific logic to adjust state coordinates based upon the new dimensions. In this example, the position of the ball is placed at the bottom edge of the screen if it has not already been launched.

boolean rollRequested = false;

if ( mousePressed )
{
  mousePressed = false;

  rollRequested = true;
}

When users press a mouse button, it generates an event that is intercepted by one of the mouse listeners created in the constructor. The mouse listener raises the boolean flag mousePressed so it can be processed later when the update() method is called. In this example, pressing the mouse causes the variable rollRequested to be set to true indicating that the ball should be launched at the target.

int ballMove = 0;

if ( keyEvent != null )
{
  int keyCode = keyEvent.getKeyCode ( );

  switch ( keyCode )
  {
    case KeyEvent.VK_SPACE:

      rollRequested = true;

      break;

    case KeyEvent.VK_LEFT:
    case KeyEvent.VK_KP_LEFT:

      ballMove = -1;

      break;

    case KeyEvent.VK_RIGHT:
    case KeyEvent.VK_KP_RIGHT:

      ballMove = 1;

      break;
  }

  keyEvent = null;

  mousePoint = null;
}

You can also launch the ball at the target by pressing the spacebar on the keyboard. Pressing the left or right arrows sets the ballMove variable to -1 or +1, respectively. After the update() method processes a keyboard event, keyEvent is set to null so that the same keyboard event will not be processed again the next time the update() method is called by the game loop. The mousePoint is set to null whenever a keyEvent is generated as keyboard events have priority over mouse events.

if ( mousePoint != null )
  {
  if ( mousePoint.x < ballRectangle.x + ballRectangle.width / 2 - VELOCITY)
    {
      ballMove = -1;
    }
    else if ( mousePoint.x > ballRectangle.x + ballRectangle.width / 2 + VELOCITY ) 
      {
        ballMove = 1; 
      } 
    else {
      mousePoint = null; 
    }
}

Before it is launched, you can also move the ball left and right using the mouse. When the player moves the mouse, an event listener created in the constructor stores the position of mouse cursor as the mousePoint. Each time the update() method is called, it will move the center of the ball closer to the most recently generated mousePoint.x value. Once the center of the ball has reached the mousePoint.x value, or close enough that any further movement might cause an overshoot, the mousePoint is set to null.

if ( rollRequested )
{
  if ( !rolling )
  {
    audioClip.play ( );

    rolling = true;
  }
}

If the player has requested by either pressing the mouse or pressing the spacebar on the keyboard that the ball be launched at the target, a sound will be played if the ball is not already rolling.

component.repaint ( targetRectangle );

As explained in a subsequent chapter, the update() method must request a repaint of the objects on the screen in their old and new positions in order to generate the animation. In this example, a repaint of the area containing the old position of the target is requested. This is done every time the update() method is called because the target is always moving.

if ( rolling )
{
  component.repaint ( ballRectangle );

  ballRectangle.y -= VELOCITY;

If the ball is rolling toward the target, a repaint of the screen area containing its old position is requested and its y coordinate is decremented by its VELOCITY.

boolean reset = false;

if ( targetRectangle.intersects ( ballRectangle ) )
{

  reset = true;

  targetRectangle.x = -targetRectangle.width;

  audioClip.play ( ); score++;

  component.repaint ( );

}

If the rolling ball collides with the target, the game is reset, a sound is played, the score is incremented, and a repaint of the entire screen is requested.

else if ( ballRectangle.y + ballRectangle.height < 0 )
{
  reset = true;

  if ( score > 0 )
  {
    score--;
  }

  component.repaint ( );
}

If the ball is rolling and it misses the target, the game is reset and the score is decremented. A repaint of the entire screen is requested so that the updated score will be painted.

if ( reset )
{
  ballRectangle.y = componentBounds.height - ballRectangle.height;

  rolling = false;
}

If the game was reset either because the ball hit or missed the target, the ball is moved back to the launching position at the bottom of the screen and the rolling state flag is reset to false.

  component.repaint ( ballRectangle );
}

Because the ball is rolling, it must be repainted in its new position.

else if ( ballMove != 0 )
{
  component.repaint ( ballRectangle );

  ballRectangle.x += ballMove * VELOCITY;

  if ( ballRectangle.x < 0 )
  {  
    ballRectangle.x = 0;
  }

  if ( ballRectangle.x
      > componentBounds.width - ballRectangle.width )
  {
    ballRectangle.x
     = componentBounds.width - ballRectangle.width;
  }

  component.repaint ( ballRectangle );
}

If the ball is not rolling toward the target, it might be moving left or right as the player aims it in the launching area. This is indicated by a non-zero value for ballMove. The ball cannot move off the left or right edges of the screen. Repaints of the screen in the old and new positions of the ball are requested.

if ( score > 1 )
{
  targetRectangle.x += random.nextInt ( score ) * VELOCITY;
}
  else
{
  targetRectangle.x += VELOCITY;
}

The horizontal velocity of the target becomes more random as the player score increases. This makes the game more of a challenge for the player.

if ( targetRectangle.x >= componentBounds.width )
{
  targetRectangle.x = -targetRectangle.width;
}

If the target moves off the right edge of the screen, it continues on from the left side so that it is moving continuously across the top of the screen from left to right.

  component.repaint ( targetRectangle );
}

The final act of the update() method is to request a repaint of the screen area containing the new position of the target. The repaint request for the old position was made earlier in the update() method.

public void paint (
  JComponent component,
  Graphics2D graphics )
///////////////////////////////////////////////////
{

This article is excerpted from Advanced Java Game Programming by David Wallace Croft (Apress, 2004; ISBN 1590591232). Check it out at your favorite bookstore today. Buy this book now.



 
 
>>> More Java & J2EE Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: