Click here to Skip to main content
15,039,244 members
Articles / Programming Languages / JScript .NET
Posted 21 Jul 2008


25 bookmarked

Star Trek "Galactic Conquest" Game Contest Submission (Java)

Rate me:
Please Sign up or sign in to vote.
4.27/5 (6 votes)
6 Aug 2008CPOL3 min read
Star Trek "Galactic Conquest" Game Contest Submission (Java)
Image 1


I based this application mostly on my own memories of a game that I played in days long gone by. I used to connect to something called "Compu-serve" on my Commodore-Vic 20 using a 300 baud modem and played online Star-Trek. Neat simple game. Round two with this is when I got a Commodore 64 and spent 3 days typing in the code into the Vic-Basic O/S only to have the computer crash on running it and losing all my work. That was a tad upsetting. But I learned a lot. So now, I base this application on those designs with my own thinking on how they may have achieved what they did to assemble their game into a cool application. I do the same but add my own touch.


Check out information that was really cool at the time.

Using the Code

  1. Obviously get the file and unzip it. There will be one file contained within it - StarTrekGalacticConquestExeJar-rb2008.jar.
  2. Make sure you have the latest Java installed (if you can run the sample, you should be ok).
  3. Double Click the Jar file. Associate it with javaw.exe that was installed in step 2.
  4. Try out the game (it should run).

Points of Interest

This game was built from the ground up as shown on the above revision listing. This was first designed using the Java Terminal, although it had some drawbacks. The first thing is that when feeding output to the terminal, you can't determine if the class that is accessing it is first generation or another instance of that class (which means you could accidentally have 300 of the same object sending information to the terminal), when crossing over to the GUI I did in fact find that this was occurring (part of the learning curve I guess). Anyway, once the basic foundation was created, I switched to a GUI display that was a bunch of text windows. From that, I switched the main display to Full Graphics. Once the Full Graphics display was working, I added and expanded on the graphics to allow a more favourful game play experience.

Java Design

Here is the Design Info


The construction of the app was broken up into sections.

  1. Galaxy Design (Quadrant & Sectors) which is laid out in a two dimensional Array
  2. Commands (to handle commands to the game and a master valid command list)
  3. Player Stats such as Available Energy, Score, nukes, etc. All the stuff that would effect command ability. It was also built this way so that if I wish I can have multiple players in a galaxy later on (Multiplayer)
  4. Movement handling
  5. Utilities such as Graphics, sounds and external files

Game Routines Breakdown

To start the game...

We have the following setup:

INITIALIZATION (which includes all Variables and Galaxy Randomization):

private void init()                       	// This is the Whole Kit and Ka-boodle.
    {                                     	// ========================================
        util = new Util(5);               	// Initialize the Utilities Class
        util.initWindow();                	// Initialize GUI Window
        sound = new Sound();          	// Initialize the Sound Class
        util.setScroll(true);		// Set No Scrolling
        sound.setAudio(true);		// Set No Audio
        initWelcome();// Welcome Notice
        LRS = new boolean[quadSizeX+1][quadSizeY+1];	// Set LRS Size 
        initLRS();// Clear LRS
        quad = new Quadrant[quadSizeX+1][quadSizeY+1];	// Set Galaxy Size
        commands = new ArrayList<command>();// Initiate Command Space. 
        initCommands();// Initiate Menu Commands & Damage Control
        player = new Player("New Player","Galactica",28,40);	// Create Player Instance
					// (Name, Ship, Baddies, # of Turns)
        createGalaxy();// Create Galaxy (Empty)
        player.setPlayerName(util.inStr("Please Enter your Name : "));// Set Player Name
        player.setShipName(util.inStr("Ship Name : "));// Set Ship Name
        difficulty();// Difficulty Set
        populateStuffUnEven((player.getBaddie()*750)/1000,"X",0);// Enemy Fighter 
						// (UnEven Distribute Test)
	"V",(util.rNumber(350)+150));  // Enemy Battleship(UnEven Distribute Test)
        populateStuffEven((2*(quadSizeX+1)*(quadSizeY+1)),"*");// Stars (Even Distribute)
        populateStuffUnEven(bases,"@",0);// Bases (UnEven Distribute)
        populateStuffUnEven(planets,"P",0);// Planets (UnEven Distribute)
        populateStuffUnEven(wormHoles,"W",0);// WormHoles (UnEven Dist.)
        populateStuffUnEven(sun,"S",0);// Sun (UnEven Dist.)
        populateStuffUnEven(1,"Y",0);// Ship (UnEven Distribute)

GAME LOOP Until Game End or Quit (Draw The Screen, Get Command, React to Command):

public void play()
        // Initialize Galaxy And Post Welcome. 
        // Play Loop.
        boolean finished = false;
        while (! finished)
            // display Play Screen
            // Get & Process New Command. 
            String commandEntry = util.inStr("Command >> ");
            finished = processCommand(commandEntry);
        // Game Quit/Done. 
        util.toScr(OUTMAIN,"\nMCP reports Simulation Terminated - End of Line. \n");
        //Play Exit Sound
        util.inStr("Hit [EXECUTE]");

What a simple loop!

But there is so much more... In this new version, I even take into consideration that the enemy has options, either shoot, or move and each ship has a damage tolerance. If you hit it, it may not mean it dies.. it also removes damage on return fire if your ship is docked. Let's take a look.

    * Enemy Turn. Included, Flight or Fight and Damage Evaluation.
   private void goEnemy()
       // Enemy in Quad. Then (75%-Shoot or 25%-Move) (1 or the other not Both.)
       String shipType;
       for (int x=0; x<=sectSizeX; x++)
           for(int y=0; y<=sectSizeY; y++)
               // If Enemy Exists in Quadrant
               shipType = quad[player.getPlayerQX()][player.getPlayerQY()].getSpace(x,y);
               if ((shipType.equals("X"))||(shipType.equals("V")))
                   int shuckJive = util.rNumber(100);
                   // Flight! 75 - 100 Fear takes over and they move somewhere in quad.
                   if (shuckJive >= 75)
                       // If Flight Then Plot New Location for ship.
                       // Fight! 0 - 74 - Then Hit with something.
                       if (!jumpFlag)
                           int returnFire = 75+util.rNumber(100);
                           if (shipType.equals("V"))
                               // Bigger Ship Bigger Shot.
                               returnFire = 75+util.rNumber(300);
                           util.toScr(OUTAUX,"Enemy @ ["+x+"]"+",["+y+"]
                   yields ["+returnFire+"] pulse!\n");
                           if (player.getStatus() == "DOCKED")
                               util.sysNotice( "Damage Diverted while Docked.");
                               if (player.getSheildStauts().equals("UP"))
                                   // If Shield is UP then Hit Shields and
                                   // take them down if possible.
                                   if (player.getSheild() < 0)
                                   // Randomize Hull Hit.
                                   int hullDamage = util.rNumber(25);
                                   if (player.getHull() > 0) util.toScr(OUTAUX,
                   "HULL HIT!-Down to ["+player.getHull()+"]%\n");
                               systemsDamage(10);      // If Hit Ship then Maybe
                       // Systems Damage
       // Execute all Enemy Jumps at Once. & Display
       jumpFlag = false;

And a small Rez Routine to put everything on screen.

 * reZ Quadrant Based on Contents.
public void reZ()
    // Pull Quad Info to local String
    String evalQuad = quadStuff;
    // Only process if there is something there.
    if (evalQuad.length() > 0)
        //Set Up offScreen Setup and Local Images.
        Image currentImg = null;
        Image offScreenImage = null;
        Graphics offscreenGraphics;
        offScreenImage = createImage(width, height);
        offscreenGraphics = offScreenImage.getGraphics();

        String spaceString = "";
        int counter = 0;
        int planetNo = 0;
        // Cycle through Quadrant Sectors.
        for (int x =0; x<= sectX;x++)
            for (int y = 0; y<= sectY;y++)
                // Look at Single Character from Quad.
                spaceString = evalQuad.substring(counter, counter+1);
                switch (spaceString.charAt(0))
                 case 'Y':
                    currentImg = gameGraphic[0][0];
                 case 'X':
                    currentImg = gameGraphic[1][0];
                 case 'Z':
                    currentImg = gameGraphic[1][1];
                 case 'V':
                    currentImg = gameGraphic[2][0];
                 case 'C':
                    currentImg = gameGraphic[2][1];
                 case 'W':
                    currentImg = gameGraphic[6][0];
                 case 'S':
                    currentImg = gameGraphic[7][0];
                 case '*':
                    currentImg = gameGraphic[3][0];
                 case '.':
                    currentImg = gameGraphic[4][0];
                 case '@':
                    currentImg = gameGraphic[5][0];
                    planetNo = Integer.valueOf(spaceString).intValue();
                    currentImg = planets[planetNo];
                // Paint Sector Contents to Offscreen Image
                offscreenGraphics.drawImage(currentImg,y * 48,x * 48,this);
                counter +=1;
        // Send Complete offscreen to Custom JPanel.

New Concepts

The old system has been replaced with the new graphic and sound. There is a lot to look at in the source code above but there are no limits with this design as it can be reworked to become multiplay and alter the graphics to cover a wide range of games such as Battlestar Galactica or others easily.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Robert Bettinelli
Web Developer Nottawasaga Valley Conservation Authority
Canada Canada
I have background in programming(many languages), web design, hardware and software. Currently I hold a lead database programming position for the Nottawasaga Valley Conservation Authority... Programming in and java comes 2nd nature to me and of course I do this for fun. I also used to design in cobol and pascal and run a BBS(anyone remember what this was?). Anyways, drop me a line maybe I can help you.

Comments and Discussions

General. Pin
Member 74095419-Dec-10 13:55
MemberMember 74095419-Dec-10 13:55 
GeneralRe: . Pin
Robert Bettinelli3-Dec-12 15:37
MemberRobert Bettinelli3-Dec-12 15:37 
yup. Cool. Smile | :)
GeneralTiles Pin
Jason McBurney15-Aug-08 13:07
MemberJason McBurney15-Aug-08 13:07 
GeneralRe: Tiles Pin
Robert Bettinelli16-Aug-08 10:17
MemberRobert Bettinelli16-Aug-08 10:17 
GeneralSource Code Pin
c24236-Aug-08 4:52
Memberc24236-Aug-08 4:52 
GeneralRe: Source Code Pin
Robert Bettinelli6-Aug-08 6:32
MemberRobert Bettinelli6-Aug-08 6:32 
GeneralRe: Source Code Pin
c24236-Aug-08 10:25
Memberc24236-Aug-08 10:25 
GeneralRe: Source Code Pin
Robert Bettinelli6-Aug-08 10:51
MemberRobert Bettinelli6-Aug-08 10:51 
GeneralRe: Source Code Pin
c24236-Aug-08 22:19
Memberc24236-Aug-08 22:19 
GeneralRe: Source Code Pin
Robert Bettinelli7-Aug-08 16:48
MemberRobert Bettinelli7-Aug-08 16:48 
GeneralRe: Source Code Pin
c24237-Aug-08 22:40
Memberc24237-Aug-08 22:40 
GeneralTried... Pin
Tomas Brennan31-Jul-08 5:56
MemberTomas Brennan31-Jul-08 5:56 
GeneralRe: Tried... Pin
Robert Bettinelli31-Jul-08 19:02
MemberRobert Bettinelli31-Jul-08 19:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.