The sourcecode of the most important methods.

Contents
Introduction
Basic structure of an applet
Move a ball
Double buffering
Ball bouncing and change the direction
Using sound in applets
Using pictures in applets
Mouse events
Keyboard events
The first complete game
Artificial intelligence for a pong like game
Generation of random 2D landscapes
Leveleditor

As always I won't discuss every single line of code in detail, but I'll talk about the most important methods and classes. These are the class LevelReader and its method readLevels() and the class Level itself. The classes Main, Stone and C_LevelEditor are pretty simple and you should be able to understand them by yourself.

LevelReader

First of all we have to read all defined levels into the applet. This happens in the class LevelReader using the method readLevels(). The method is pretty simple and self explanatory, the only special thing is, that we have to set a reference to the applet class in the class LevelReader. This reference to the Component = Applet is needed to use the getParameter() - method. Well, here comes the code:

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

public class LevelReader
{

    // Variables
    private int levels_in_total;

    // Array, stores all generated instances of the class Level
    private Level [] level_array;
    
    // Appletrefference
    private Component parent;
    
    public LevelReader (Component parent)
    {

        // Initialize appletrefference
        this.parent = parent;
        
        // Get number of levels in total
        levels_in_total = Integer.parseInt 
                    (((Applet)parent).getParameter(C_LevelEditor.total_levels));
        
        // Initialize level_array
        level_array = new Level [levels_in_total];

    }
    
    /* This method reads every level in the HTML - Page, 
    generates a instance of the class Level (for every level) 
    and stores it in the level_array*/
    public Level [] readLevels ()
    {
        for (int i = 1; i <= levels_in_total; i++)
        {
            // generate new level
            Level level = new Level ();
            
            // get and set information parameters
            level.setAuthor (((Applet)parent).getParameter (
                                "Level" + i + "_" + C_LevelEditor.author));
            level.setName (((Applet)parent).getParameter 
                                ("Level" + i + "_" + C_LevelEditor.name));
            level.setComment (((Applet)parent).getParameter 
                                ("Level" + i + "_" + C_LevelEditor.comment));
            
            // read in all the lines and store them in the level
            for (int j = 0; j < C_LevelEditor.number_of_lines; j++)
            {
            
            level.setLine (((Applet)parent).getParameter ("Level" + i + "_Line" + j), j);
            
            }
            
            // store level
            level_array [i-1] = level;

        }
        return level_array;
    }
}

 

Note:Color coded lines have been split for display purposes

The class Level

Now we are able to read in the levels and to store them in Level instances but we still don't know anything about the Level class in detail. Now we have to generate a real level, mainly the stone_map which holds the different level elements, out of the string information we get out of the level definition in the HTML - page. The stone_map 2D array stores the level elements at the same position where they appear in the level definition (for example a "r" occurs at line 3 as the third character of the string, then a red stone object is generated in the array in row 3 and column 3). First of all this class has some set and get methods to get and set the values of the level information parameters. Much more interesting is the method setLine. This method gets one line of the level definition (a string) and translates this string to stone objects and stores these stone instances in the stone_map. Last but not least the class has its own paint method.

import java.util.*;
import java.awt.*;

public class Level
{

    // Variables
    private String author;
    private String name;
    private String comment;

    // Levelmatrix, stores the stone objects
    private Stone [] [] stone_map;
    public Level ()
    {
        // Initialize the stone_map, all fields 
        // are initialized with null
        stone_map = new Stone [C_LevelEditor.number_of_lines]
        [C_LevelEditor.number_of_cols];
    }

    // Method translates information of one line of 
    //the level definition to stone objects 
    public void setLine (String line, int line_index)
    {
            char [] entrys = line.toCharArray();

            // go through all chars and translate them  
            // to stone objects
            for (int i = 0; i < C_LevelEditor.number_of_cols; i++)
            {
                Stone stone = null;
        
                // generate red stone if char equals "r"
                if (entrys[i] == 'r')
                {
                stone = new Stone (line_index, i,Color.red);
                }
                
                // generate different coloured 
                // stones the same way
                ...
                
                // If char is unknown, generate no stone, 
                // which means that this array field stays null 
                else
                {
                    // do nothing
} // store stone in array if it is not null if (stone == null) { // do nothing } else { stone_map [line_index] [i] = stone; } } } // set and get methods for the information strings ... // Method paints level public void paintLevel (Graphics g) { // go through the whole stone map and paint stones for (int i = 0; i < stone_map.length; i++) { for (int j = 0; j < stone_map[i].length; j++) { Stone stone = stone_map [i][j]; // paint stone or do nothing if stone is null if (stone == null) { // draw nothing } else { stone.drawStone(g); } } } // paint level information g.setColor (Color.yellow); g.drawString (comment, 50, 250); g.drawString (name, 50, 270); g.drawString (author, 50, 290); } }

Conclusion

In this chapter I showed you one way to define a level in a HTML - page, to read in this level using a LevelReader and one method to represent this level in our applet (2D array). As always there are many ways to do this maybe much better ones than mine, and even though we might have helped you, because you can use the methods to read in and store the level in the applet in almost every arraybased game the much harder work is still in front of you. You have to make your game work with every level someone defined (which is really hard) and of course you have to generate your own level elements, change the number of level lines... . Ok, I hope I could help you a little bit, if you wrote a game using this editor, I would be glad if you would send it to me. Well, we are finished, here comes the link to download the sourcecode and the link to the working level editor applet (take a look a the sourcecode of the HTML - page to see what the levels look like).

SourceCode download
Take a look at the applet

 1   
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact