65.9K
CodeProject 正在变化。 阅读更多。
Home

星际迷航“银河征服”游戏比赛作品(Java)

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.27/5 (6投票s)

2008年7月22日

CPOL

3分钟阅读

viewsIcon

54908

downloadIcon

985

星际迷航“银河征服”游戏比赛作品(Java)

引言

这个应用程序主要基于我对很久以前玩过的一个游戏的记忆。 我过去使用 300 波特的调制解调器连接到 Commodore-Vic 20 上的一个叫做“Compu-serve”的东西,并在线玩星际迷航。 很棒的简单游戏。 第二轮是我得到了一台 Commodore 64,花了 3 天时间将代码输入到 Vic-Basic O/S 中,结果电脑在运行的时候崩溃了,我所有的工作都丢失了。 这有点令人沮丧。 但我学到了很多。 所以现在,我将这个应用程序建立在那些设计的基础上,用我自己的想法,他们可能如何实现他们所做的一切,将他们的游戏组装成一个很酷的应用程序。 我做同样的事情,但添加我自己的风格。

背景

查看信息,这在当时真的很酷。

Using the Code

  1. 显然要获取 StarTrekGCExeJar-rb2008.zip 文件并解压缩。 它将包含一个文件 - StarTrekGalacticConquestExeJar-rb2008.jar
  2. 确保您安装了最新的 Java http://www.java.com/en/download/installed.jsp(如果您可以运行示例,则应该没问题)。
  3. 双击 Jar 文件。 将其与步骤 2 中安装的 javaw.exe 相关联。
  4. 尝试一下游戏(它应该可以运行)。

关注点

这款游戏是完全从头开始构建的,如上面的修订列表中所示。 最初是使用 Java 终端设计的,尽管它有一些缺点。 首先,当将输出发送到终端时,您无法确定访问它的类是第一代还是该类的另一个实例(这意味着您可能会意外地有 300 个相同的对象向终端发送信息),当切换到 GUI 时,我确实发现这种情况正在发生(我想这是学习曲线的一部分)。 无论如何,一旦创建了基本基础,我就切换到一堆文本窗口的 GUI 显示。 从那以后,我将主显示切换到全图形。 一旦全图形显示工作,我添加并扩展了图形,以允许更受欢迎的游戏体验。

Java 设计

这是设计信息

ST-ClassFiles.jpg

应用程序的构建分为几个部分。

  1. 银河设计(象限和扇区),它以二维数组排列
  2. 命令(处理游戏的命令和主有效命令列表)
  3. 玩家统计信息,例如可用能量、分数、核弹等。所有会影响命令能力的东西。 它的构建方式也是如此,如果我愿意,我可以在以后在一个银河系中拥有多个玩家(多人游戏)
  4. 移动处理
  5. 实用程序,例如图形、声音和外部文件

游戏程序分解

开始游戏...

我们有以下设置

初始化(包括所有变量和银河随机化)

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)
        populateStuffUnEven((player.getBaddie()*250)/1000,
	"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)
    }

游戏循环直到游戏结束或退出(绘制屏幕、获取命令、对命令做出反应)

public void play()
    {
        // Initialize Galaxy And Post Welcome. 
        init();
        // Play Loop.
        boolean finished = false;
        while (! finished)
        {
            // display Play Screen
            drawScreen();
            // 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
        sound.playSound("EOL.wav");
        util.inStr("Hit [EXECUTE]");
        //End.
        System.exit(0);
    }

多么简单的循环!

但还有更多... 在这个新版本中,我甚至考虑到敌人有选择,要么射击,要么移动,并且每艘船都有损伤容限。 如果你击中它,并不意味着它会死亡.. 如果你的船停靠,它也会消除回火造成的伤害。 让我们来看看。

 /**
     * 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.
                        quad[player.getPlayerQX()][player.getPlayerQY()].plotEnemy(x,y);
                    }
                    else
                    {
                        // 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);
                            }
                            sound.playSound("L2.wav");
                            util.shot(x,y,player.getPlayerSX(),player.getPlayerSY());
                            util.toScr(OUTAUX,"Enemy @ ["+x+"]"+",["+y+"] 
					yields ["+returnFire+"] pulse!\n");
                            if (player.getStatus() == "DOCKED")
                            {
                                util.sysNotice( "Damage Diverted while Docked.");
                            }
                            else
                            {
                                if (player.getSheildStauts().equals("UP")) 
                                {
                                    // If Shield is UP then Hit Shields and 
                                    // take them down if possible.
                                    player.hitSheild(returnFire);
                                    if (player.getSheild() < 0)
                                    {
                                        player.sheildDown();
                                    }
                                }
                                else
                                {
                                    // Randomize Hull Hit. 
                                    int hullDamage = util.rNumber(25);
                                    player.hitHull(hullDamage);
                                    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
        util.toScr(OUTAUX,quad[player.getPlayerQX()][player.getPlayerQY()].jumpEnemy());
        jumpFlag = false;
    }

和一个小的 Rez 例程,将所有内容放在屏幕上。

    /**
     * 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];
                        break;
                     case 'X':
                        currentImg = gameGraphic[1][0];
                        break;
                     case 'Z':
                        currentImg = gameGraphic[1][1];
                        break;
                     case 'V':
                        currentImg = gameGraphic[2][0];
                        break;
                     case 'C':
                        currentImg = gameGraphic[2][1];
                        break;
                     case 'W':
                        currentImg = gameGraphic[6][0];
                        break;
                     case 'S':
                        currentImg = gameGraphic[7][0];
                        break;
                     case '*':
                        currentImg = gameGraphic[3][0];
                        break;
                     case '.':
                        currentImg = gameGraphic[4][0];
                        break;
                     case '@':
                        currentImg = gameGraphic[5][0];
                        break;
                     default:
                        planetNo = Integer.valueOf(spaceString).intValue();
                        currentImg = planets[planetNo];
                        break; 
                    }
                    // Paint Sector Contents to Offscreen Image
                    offscreenGraphics.drawImage(currentImg,y * 48,x * 48,this);
                    counter +=1;
                }
            }
            // Send Complete offscreen to Custom JPanel. 
            quadDisplay.drawImage(offScreenImage);
        }
    }

新概念

旧系统已被新的图形和声音所取代。 上面的源代码有很多值得关注的地方,但这种设计没有限制,因为它可以重新设计以成为多人游戏,并更改图形以轻松涵盖广泛的游戏,例如太空堡垒卡拉狄加或其他游戏。

© . All rights reserved.