How to Write Games in Java

How to Write Games in Java thumbnail
Write Games in Java

The Java programming language has become a popular choice for development due to its object-oriented structure and its virtual-machine based interface that allows programs written in Java to run without modification on a wide variety of operating systems and computers, from cell phones to workstations.

It's also a quite capable platform for game development, and if you are just starting to learn game development, it's best to start simple and create a Pong-clone.

Things You'll Need

  • Java
Show More

Instructions

    • 1

      Create a class and name it JPong. If you are using an IDE like Netbeans, you can do this from the file menu. However, even a simple text editor will do the job: just create a file named "JPong.java."

      Paste the following skeleton into the file:

      import java.awt.Canvas;
      import javax.swing.JFrame;
      import java.awt.event.KeyListener;
      import java.awt.event.KeyEvent;
      import java.awt.Color;
      import java.awt.Graphics;

      public class Pong implements KeyListener {

      public Pong() { }

      public void keyPressed(KeyEvent e) { }

      // These two are required by the compiler, but will not be used in your game.
      public void keyReleased(KeyEvent e) { }
      public void keyTyped(KeyEvent e) { }

      public void draw() { }

      public boolean detectCollision() { }

      public void play() { }

      public static void main(String args[]) { }

      }

      The rest of the steps will gradually fill in the skeleton to create a complete game.

    • 2

      Define the data the class will need. Insert this at the top of your class:

      private final int WIDTH = 640;
      private final int HEIGHT = 480;
      private final int DELTA = 8;
      private final int PADDLE_WIDTH = 32;
      private final int PADDLE_HEIGHT = 128;
      private final int PUCK_RADIUS = 32;

      Graphics g;

      private int x1 = 20; // location of player A's paddle
      private int y1 = 240;
      private int x2 = 600; // location of player B's paddle
      private int y2 = 240;

      private double x = 60.0; // location of ball
      private double y = 140.0;
      private double vx = 2.0; // velocity of ball
      private double vy = 1.0;

    • 3

      Create the constructor.

      public Pong() {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setTitle("Pong 1.0");
      f.setResizable(false);
      Canvas c = new Canvas();
      c.setSize(640, 480);
      f.add(c);
      f.pack();
      f.setVisible(true);
      g = c.getGraphics();
      f.addKeyListener(this);
      draw();
      }

    • 4

      Create the keyPressed method. This will be called whenever the user presses a key on the keyboard, and it will cause the paddles to move when certain keys are pressed:

      public void keyPressed(KeyEvent e) {
      if (e.getKeyCode() == KeyEvent.VK_UP) y2 = y2 - DELTA;
      else if (e.getKeyCode() == KeyEvent.VK_DOWN) y2 = y2 + DELTA;
      else if (e.getKeyChar() == 'i') y1 = y1 - DELTA;
      else if (e.getKeyChar() == 'k') y1 = y1 + DELTA;
      }

    • 5

      Create the draw method that will draw the screen each game frame.

      public void draw() {
      g.setColor(Color.black);
      g.fillRect(0, 0, WIDTH, HEIGHT);
      g.setColor(Color.red);
      g.fillRect(x1, y1, PADDLE_WIDTH, PADDLE_HEIGHT);
      g.setColor(Color.green);
      g.fillRect(x2, y2, PADDLE_WIDTH, PADDLE_HEIGHT);
      g.setColor(Color.yellow);
      g.fillOval((int)x, (int)y, PUCK_RADIUS, PUCK_RADIUS);

      }

    • 6

      Create the collision detection method that will determine if the puck has hit one of the paddles:

      public boolean detectCollision() {
      // Test for collision with first paddle
      if (y + vy > y1 &&
      y + vy < y1 + PADDLE_HEIGHT &&
      x + vx < x1 + PADDLE_WIDTH &&
      x + vx> x1) {
      return true;
      }

      // Test for collision with second paddle
      else if (y + vy > y2 &&
      y + vy < y2 + PADDLE_HEIGHT &&
      x + vx + PUCK_RADIUS > x2 &&
      x + vx + PUCK_RADIUS < x2 + PADDLE_WIDTH) {
      return true;
      }
      else return false;
      }

    • 7

      Create the game loop. This function coordinates the others by running continually so long as the game is open:

      public void play() {
      while (true) {
      if (x + vx < 0 || x + vx > WIDTH || detectCollision()) vx = -vx;
      if (y + vy < 0 || y + vy > HEIGHT) vy = -vy;

      x = x + vx;
      y = y + vy;
      draw();
      try {
      Thread.sleep(30);
      } catch (Exception e) {
      e.printStackTrace();
      }
      }
      }

    • 8

      Create the main function. This is the entry way to the program and is required by all Java applications. All it does is create the game and start the play function going.

      public static void main(String args[]) {
      Pong p = new Pong();
      p.play();
      }

Related Searches:

References

  • Photo Credit balle de ping pong image by pascal cribier from Fotolia.com

Comments

You May Also Like

  • How to Write a 3D Java Game

    Java 3-D games are based on a concept called a "scene graph," which simplifies game development. After conceiving the idea for a...

  • How To Write Java Programs

    Java is an object-oriented programming language originally developed and released by Sun Microsystems in the mid-1990s. Java was developed from the start...

  • Tutorial for Making a Game in Java

    Creating a game brings together many aspects of computing, psychology and the arts. Among these facets are the visual and musical arts...

  • How to Create a Hangman Game With Java

    Everyone remembers playing the word-guessing game Hangman as a child. One player secretly chooses a word. The other player has to guess...

  • How to Make an Object-Oriented Java Game

    Java is an object-oriented programming language created by Sun Microsystems. The term "object-oriented" refers to a programming approach that involves virtual objects...

  • How to Write a Method in Java

    Java is the most popular programming language, primarily because of its portability. The same Java program can be used on virtually any...

  • How to Write A Java Applet

    First you need to decide on what type of program you want to write. Will it be a game? a utility? I...

  • How to Make Java Games

    Java games are computer games written in the computer programming language "Java." Java games can be as complex as the shoot-'em-up three...

Related Ads

Featured