Technology Software

How to Create a Side-Scrolling Game in BlueJ

    • 1). Start the BlueJ program. Click the "New Project" option within the "Project" menu. Type in the project name "SideScroller." in the "Folder Name" text box and click "Create."

    • 2). Click the "New Class" button. Type in class name "MainSideScroller" in the "Class Name" text box. Click "OK." Click the "New Class" button again and type in the class name "scrollerBackground" in the "Class Name" text box. Click "OK."

    • 3). Double-click the "MainSideScroller" icon and type in the editor the code to import the java applet classes and the Java awt (abtract Web toolbox) classes.

      import java.applet.*;
      import java.awt.*;

    • 4). Type in the "class" code, starting at the next line of the editor, to create a class, named moveFrame, that extends the Applet class and implements the class Runnable. Declare a class variable named "xleft" that stores and set the initial location of the background image (a yellow rectangle) to an Applet screen position of 400 pixels.

      public class moveFrame extends Applet implements Runnable
      {

      int xleft =400;

    • 5). Type in the "init" method code, starting on the next line of the editor, to set the background color of the screen to red when the Applet initializes.

      public void init()
      {
      setBackground (Color.red);
      }

    • 6). Type in the "start" method code, starting on the next line of the editor, to create a thread object, named "frameTiming" and start the thread running when the Applet's start sequence is called.

      public void start()
      {

      Thread frameTiming = new Thread (this);

      frameTiming.start ();
      }

    • 7). Type in the "run" method code, starting on the next line of the editor, to run the thread such that every time the thread is awakened, it will move the horizontal position of the side scroller (the yellow rectangle object yet to be coded) one pixel to the left. Set the thread sleep time to 30 milliseconds using the sleep method of the thread class.

      public void run ()
      {

      while (true)
      {

      xleft --;
      repaint();

      try
      {

      Thread.sleep (30);
      }
      catch (Exception e)
      {

      }

      }
      }

    • 8). Type in the "paint" method code, starting at the next line of the editor, to draw the background scroller, a yellow rectangle, at the new horizontal position (xleft) decremented in the "run: method. Code an "if" statement so that the background scroller will move back to its initial position (400 pixels) on the applet screen if the scroller has moved farther to the left than the x=0 position on the applet screen.

      public void paint (Graphics g)
      {
      if (xleft <0) {
      xleft = 400;}
      g.setColor (Color.yellow);

      g.fillRect (xleft,100 , 200, 200);
      }

    • 9). Type in the code, starting at the next line of the editor, that closes the moveFrame class statement.

      }

    • 10

      Click the "Compile" button. Click the "Close" button. Right click the "MainSideScroller" icon. Click the "OK" button. Observe that a yellow rectangle moves across the applet screen from right to left until it reaches the left edge of the screen (at which point it resets back to the initial horizontal position of 400 pixels).

SHARE
RELATED POSTS on "Technology"
How Do I Print Business Cards With Microsoft Publisher?
How Do I Print Business Cards With Microsoft Publisher?
How to Reduce the Size of Photos Using Adobe Photoshop Elements 3.0
How to Reduce the Size of Photos Using Adobe Photoshop Elements 3.0
Features and Benefits of Project Portfolio Management
Features and Benefits of Project Portfolio Management
Fix Registry - Fix Registry Errors Easily
Fix Registry - Fix Registry Errors Easily
Business Intelligence Solutions and Services
Business Intelligence Solutions and Services
AutoCAD Architecture
AutoCAD Architecture
Best Social Networking Apps
Best Social Networking Apps
IKE
IKE
Help Desk Software by help-desk-software
Help Desk Software by help-desk-software
What's Better - Building a BPM Solution Or Buying One?
What's Better - Building a BPM Solution Or Buying One?
How to Make Music Fade out in Moviemaker
How to Make Music Fade out in Moviemaker
How to Shrink a DVD With Nero Instructions
How to Shrink a DVD With Nero Instructions
Live Chat Software: Four Tips To Help You Get the Most Out of Your Software
Live Chat Software: Four Tips To Help You Get the Most Out of Your Software
How to Stop Rootkits
How to Stop Rootkits
3 First-Rate Foundations for Making Movies
3 First-Rate Foundations for Making Movies
How to Build Game Downloads
How to Build Game Downloads
The Linux Modem How-To
The Linux Modem How-To
Text-Terminals on Linux - 11.6 Terminal Server Connection
Text-Terminals on Linux - 11.6 Terminal Server Connection
The Linux Loadable Kernel Module How-To
The Linux Loadable Kernel Module How-To
How to Create Photo Albums With CSS
How to Create Photo Albums With CSS

Leave Your Reply

*