Java
  Home arrow Java arrow Page 7 - Gaming Development Setup
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Gaming Development Setup
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 31
    2005-03-02

    Table of Contents:
  • Gaming Development Setup
  • Playing the Demo Online
  • Introducing XML
  • Using Open Source
  • Finding Multimedia for Your Games
  • Basics Example
  • Generating an event
  • Drawing the background and target

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Gaming Development Setup - Generating an event
    (Page 7 of 8 )

    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 Articles
    More By Apress Publishing


     

       

    JAVA ARTICLES

    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup
    - Using RPC-Style Web Services with J2EE
    - Integrating XML with J2EE
    - Taming Tiger: Concurrent Collections

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway