View  Info  Edit

Home > Programming tutorial 1

Getting Started

Hello. Welcome to the tutorial that's going to teach you how to program in Java, lightning fast, by making games! Just before we kick off however there's a couple of things you're going to need:

  1. A good text editor (for writing all the code!)
    Everyone has a different preference here so just google text editors with Java syntax highlighting and try them out - see what you like the feel of. Failing that, ask someone else who knows and get them to install a good one for you.
  2. The game framework we'll be using for this tutorial
    So, click this link, save it and extract it to somewhere on your computer. Don't forget where you put it.

Now we've sorted that out, let's continue with some programming basics...

What is a program?

A program is a list of instructions for the computer to follow. Simple as that.
When a computer follows the instructions that make up a program, we say the computer is executing that program.

It is worth saying that a computer will always follow exactly the instructions that are given to it. When a program isn't doing what you intended it will almost always because you're accidently telling your computer something different to what you intended.

So, In short, humans write instructions, their computers execute them.

Creating the game files

Create a file named MyFirstGame.java in the same directory as minigames.jar, with the following content:

import framework.*;
import minigames.*;
import java.awt.*;

public class MyFirstGame extends MiniGame
{
	public void init ()
	{
		// time is in milliseconds
		setTimeout(1500);
	}
	
	public void update (int time) {}
	
	public void draw (Graphics2D g)
	{
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, WIDTH, HEIGHT);
	}
}

For the time being, don't worry about what this is doing; we'll cover each part separately in a moment.

We need to deal with two other files to let the minigame framework know about our new game. Create a file called myfirstgame.ini with the following content:

[Game]
My first game v1: MyFirstGame

Then create or edit the file games.list to contain the name of that file on a new line:

myfirstgame.ini

Compiling the java code

The game code is currently in a human-readable form. We need to convert it to something the computer can understand, a process known as compiling.

http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html#win32-2b

Playing the game

You can now play the game by double-clicking the jar file or running java -jar minigames.jar. Click on "Select Game" and then select our game from the list.

It's quite dull at the moment - we haven't told it to do anything other than display a white background and end after 1.5 seconds.

Understanding the code

import framework.*;
import minigames.*;
import java.awt.*;

These lines tell Java to find the packages we're using.

public class MyFirstGame extends MiniGame
{
	...
}

This defines a class, which contains variables and methods. The curly braces mark the beginning and end of the class, and are used to indicate blocks of code elsewhere as well.

public void init ()
{
	// time is in milliseconds
	setTimeout(1500);
}

This is a method; basically a collection of commands to run in sequence. You can see that again curly braces are used to mark the beginning and end of a section.

There is only one command here - setTimeout(1500). This is itself a method.

Each command goes on its own line, and is ended with a semicolon.

public void update (int time) {}

public void draw (Graphics2D g)
{
	g.setColor(Color.WHITE);
	g.fillRect(0, 0, WIDTH, HEIGHT);
}

Two more methods – these are called repeatedly by the game loop. These are different in that they have extra information sent to them – int time and Graphics2D g. These are variables. The first part (int or Graphics2D) is the type, and tells Java what kind of data the variable stores. The second part (time or g) is the identifier, and is used to refer to the variable and its contents.

Adding images

Add to the class:

private Image target;

Add to init:

target = getImage("target");

Add to draw:

Images.drawCentredImage(g, target, HALFWIDTH, HALFHEIGHT);