Programming games in Java


Java is my favorite programming language: relatively well designed, exceptionally documented, useful also for large projects. Another advantage is diverse tools, for example Eclipse development environment, you can download freely from Internet. You could start with an empty computer and in a few minutes have installed a full set of programs used by professionals, and it would not cost you a single cent.

When I first learned programming - somewhen during elementary school, on a PMD-85 computer, in Basic - I was mostly looking forward to an ability to write my own computer games. It seemed so easy. Those days computer games did not have complicated graphics nor sound effects, only a good idea and a relatively simple algorithm. Though it was not simple to program that algorithm using tools available at that time, understanding it was easy. It was easy to imagine that when you learn to program somewhat better, you could sit at computer during holidays and write something similar. Furthermore, all computers at that time included some programming environment; you started your work by loading and running a program from a command line, so it was not unusual to imagine that once you will write more commands to the command line and create your own simple program.

In this sequence of articles I want to bring back my memories of what brought me to programming, and also show how simple (if you are interested in programming) it is today to make a simple computer game.

These articles will suppose that the reader is familiar with basics of programming, not necessarily in Java language. Sometimes I will explain also simple things, if I will consider that necessary for the topic, but I will skip other things; if I tried to explain everything in detail, it would be long and boring. I will rather focus on applying this knowledge to make a computer game. Further information you can get from the same source as I did: studying programming (the language does not matter; there are many programming languages, but most algorithms are language-independent), reading documentation (Java has extraordinarily good and transparent documentation), searching on the web, experimenting, and discussing with friends who share a similar hobby.


Let's start by downloading the necessary tools from the web. First, we need the Java language; we can find it on the producer's website (originally Sun, today Oracle). We want a standard version of Java, so called „Java SE“. But the download for users is not enough; we need a download for programmers called „Java SDK“ or shortly „JDK“. Download the file from Internet and install it on your computer.

(Explanation: „Java SE“ is a basic version of Java including almost everything you will need, certainly the best version for learning the Java language. „Java EE“ has additional things for creating web server applications. „Java ME“ is a version for mobile phones. The environment necessary to start programs in Java is called „Java Runtime“. The environment for creating programs in Java, that is „Java SDK“ or „JDK“, includes the runtime and some additional programming tools. If you already have an older version of Java installed, you do not need to uninstall it; just add the new version.)

Second, we need the development environment Eclipse (or some other development environment). It is not absolutely necessary, but it is hundred times more convenient, no exaggeration. Eclipse is used for many different purposes; search the version called „for Java Developers“. Download the file, extract in on your disk, and start the program „eclipse.exe“.

First start

After the first start Eclipse will ask you where on your disk do you want to have your workspace. That is a directory, where your projects will be saved; each project in a separate subdirectory. During programming you do not need to care where is which file saved; Eclipse will save them automatically to the right place.

After the first start an introductory window called „Welcome“ is displayed. By clicking a small „x“ besides the title you close this window (just like you close a tab in a web browser); if you will want to see it later, you can find it in the „Help“ menu. After closing the introductory window, your programming environment finally appears.

If you feel like there are too many things on the screen, don't worry. Later you will find out what those things do. On sides and at the bottom of the window there are some named panes; each one of them has a different purpose. (If you close a pane, you can later find it in a „Window | Show View“ menu.) In the bottom of window there is an empty area, where you will write your programs later. You can also later rearrange the panes as you wish.

The first program

The most important pane is „Package Explorer“; it displays your programs. You do not have any yet, that is why there is just a big white area. Click the area using the right mouse button and select „New | Java Project“. As a project name use for example „example“; do not think about the rest of the dialog and just click „Finish“. You have created your first project. In a directory you have specified as your workspace, the subdirectory „example“ is created; it will contain everything related to this project. It contains a subdirectory „src“, which contains (which will contain) your program's source code.

Java is an object-oriented programming language. The running program consists of parts called objects. Each object is based on a template (part of source code) called class. It is not the most exact explanation, but for starters it will work. To make a program we need to make at least one class; the starting class must contain the „main“ method, where the program starts. What happens then, depends on the program we will write.

On the pane „Package Explorer“ click the right mouse button on the project name or on the „src“ directory and select „New | Class“. As a name of the class (attention, this is not the first line in the dialog) write some word starting with a capital letter, such as „Example“. Then check the box called „public static void main(String[] args)“, which will add the main method to the new class. Do not think about the rest of the dialog and click „Finish“.

In a directory „src“ a new file called „“ was created, and it contains the class called „Example“. (The file name and the class name must match.) The file is open for editing. If we ignore the comments, it contains this:

public class Example {

	public static void main(String[] args) {


Now you can write commands that the program will do. We will leave more complicated things to later. Now, just let's write a greeting text on the text output:

public class Example {

	public static void main(String[] args) {


Save the program by clicking the „Save“ button. (If you forget to save, the Eclipse environment would ask you later.) Start the program by clicking on the „Run“ button. The program will write the text „Hello“ on the text output (represented in Eclipse by the „Console“ pane, which appears after you run the program) and quit.

The goal of today's lesson was installing the necessary tools and starting the first program. If you have successfully managed to complete this introduction, you can now take your Java textbook or find some tutorial on the internet and continue independently. When you later turn off the Eclipse environment, all settings will be remembered, so when you start it again, you continue right where you have finished today.

Note: It seems like writing texts to the text output is not related to creating graphical computer games; so why do I mention it here? First, as programmers we can use the text output to write helpful information (like which value is in which variable) to easily see if the program functions according to our wishes, or a mistake happened somewhere. In the finished product we will remove this output. Second, work with text is not similar to work with graphics, but it is similar to other things, such as storing information on disk or sending information on network. What we learn by using the command line we will use later when we will save and load the game or create a network game.