Breaking News

Game Deveopment Tutorial with Java:part 1


Introduction


 This is simple tutorial to simple game programming concepts with java. I have decided to write this tutorial to help the beginners in the game programming to get the whole idea, I developed two games as applets and I will use theme to as examples to what I’ll explain.
The tools needed for this tutorial
Simply you just need an installed JDK which you can get it from sun web sitehttp://java.sun.com/javase/downloads/index.jsp
                You also need IDE like Eclipse, Netbeans or any other IDE … you can get those from here

Basic Idea

               
The main idea of the game programming is how to utilize the graphics, animations, music, and some of artificial intelligence.  The question is how to do that? This is the question which the tutorial will answer.
                One of The basics of the game as I mentioned before is the Graphics so we need first to know how to achieve this with java. We have two ways to implement such animated game either in swing Frame or Applet, But I’m going to continue with the applet , since my games are applets and it will be good idea to publish them over the internet.
                Also another basic of the game basics is the threading, and managing the user input which it could be various (keyboard, mouse, joysticks  ...). do not worry I will explain every thing in details.

Game Basics


Graphics basics

                I think all of you have used the paint method it’s available for the applet and the frames
public void paint (Graphics g)
The applet structure is simply ..
import java.applet.*;
import java.awt.*;

// Inherit the applet class from the class Applet
public class FirstApplet extends Applet
{
// Now you should implement the following methods

// init - method is called the first time you enter the HTML site with the
// applet
public void init() {... } 

// start - method is called every time you enter the HTML - site with the applet
public void start() {... }

// stop - method is called if you leave the site with the applet
public void stop() {... } 

// destroy method is called if you leave the page finally (e. g. closing
// browser)
public void destroy() {... }

/**
 * paint - method allows you to paint into your applet. This method iscalled
 * e.g. if you move your browser window or if you call repaint()
 */
public void paint (Graphics g) { }
}

And the frame structure
class Game extends JFrame {
public void paint(Graphics arg0) {}
}

Animation


                When I have started to search about tips on the game programming, I found  this web site http://www.javacooperation.gmxhome.de/KontaktEng.html  which  has very simple and easy to understand tutorial. Thanks to Fabian Birzele for this tutorial which I‘ll use it to explain the basics of simple game programming.

Threads

A thread is a piece of program that is able to run parallel to other parts of the program (multithreading). Threads are implemented by the class Thread, the interface Runnable and the method run(), we have already implemented these two things in the step before. Important methods of the class Thread are:
  • Thread.start(): starts a thread
  • Thread.stop(): stops a thread
  • Thread.sleep(time in milliseconds): stops thread for a certain amount of time
You can find more functions of the thread class in the Java API!

And here comes the code! 

public void start() {
            // define a new thread
            Thread th = new Thread(this);
            // start this thread
            th.start();
      }

Now this thread is running in the run() - method of our applet. Every time all methods... in the run - method have been called, we stop the thread for a short time. Your run method should look like this:
     
public void run() {
            // run a long while (true) this means in our case "always"
            while (true) {
                  // repaint the applet
                  repaint();
                  try {
                        // Stop thread for 50 milliseconds
                        Thread.sleep(50);
                  } catch (InterruptedException ex) {
                  }
            }
}


What we have now is a never ending loop that executes all things within the loop, waits 20 milliseconds and executes everything once again and so on. But how can we move a circle that is painted by the applet? Well this is a very simple idea: Our circle has a x - and a y - position. If we would add 1 to the x - position every time the thread executed, the ball should be moving across the applet, because it is painted at a different x - position every time we execute the thread!

Ok, let's start with drawing a circle: Add these lines to the paint - method of the applet: 

public void paint(Graphics g) {
            // paint a filled colored circle
            g.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
      }
And we need the following instance variables at the head of the program:

int x = 15;
      int y = 100;
      int radius = 30;


To move a ball we change the value of the x_pos variable every time the thread is executed. Our run - method should look like this:
public void run ()
      {
            ...
            while (true)
            {
                  // changing the x - position of the ball/circle
                  x ++;
                  ...
            }
      }
If you add this applet to a HTML, a red ball should be moving across the applet one times!
Example 1
import java.applet.*;
import java.awt.*;
/**
 * @author muhammad.hamed
 */
public class Animation1 extends Applet implements Runnable {
      int x = 15;
      int y = 100;
      int radius = 30; // Radius of the Ball

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

      public void start() {
            Thread th = new Thread(this);
            th.start();
      }

      public void stop() {

      }

      public void destroy() {

      }

      public void run() {
            while (true) {
                  x++;
                  repaint();
                  try {
                        Thread.sleep(50);
                  } catch (InterruptedException ex) {
                  }
            }
      }

      public void paint(Graphics g) {
            g.fillOval(x - radiusy - radius, 2 * radius, 2 * radius);
      }

}
Figure 1
Figure 2

But how to draw more than one ball with multiple colors let blue and red , and one of them is moves vertically and the other moves vertically.
Example 2
import java.applet.*;
import java.awt.*;
/**
 * @author muhammad.hamed
 */
public class Animation2 extends Applet implements Runnable {
      int x = 15;
      int y = 100;
      int x1 = 100;
      int y1 = 15;
      int radius = 30; // Radius of the Balls

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

      public void start() {
            Thread th = new Thread(this);
            th.start();
      }

      public void stop() {

      }

      public void destroy() {

      }

      public void run() {
            while (true) {
                  x++;
                  y1++;
                  repaint();
                  try {
                        Thread.sleep(50);
                  } catch (InterruptedException ex) {
                  }
            }
      }

      public void paint(Graphics g) {
            //draw the red ball
            g.setColor(Color.red);
            g.fillOval(x - radiusy - radius, 2 * radius, 2 * radius);
           
            //draw the blue ball
            g.setColor(Color.blue);
            g.fillOval(x1 - radiusy1 - radius, 2 * radius, 2 *radius);
      }
}

 
Figure 3
Figure 4

No comments