1

在过去的几天里,我用 Java 编写了一个(拼凑在一起的)文本 RPG 游戏。世界中的定位几乎由一个简单的 100x100 2D 字符串数组控制。然后,当玩家真正来到网格时,我将字符串转换为实际对象。

我的想法是有一个图形显示来显示这个 100x100 网格,网格的每个部分都有一个与数组中的内容相对应的图像。

例如,如果 block[10][15] 处的字符串是“rock”,则第 10 行第 15 列的网格图形显示部分将显示岩石的图片。

理想情况下,每次我在 do-while 循环中循环时,此图形都会刷新。奇怪的是,我想到的东西看起来与早期的口袋妖怪游戏非常相似。

如果我的描述措辞不当或我的问题太模棱两可,我深表歉意。我的计算机科学课程只学了半个学期的java,所以我的知识仅限于我们一个学期所学的基础知识。我确实喜欢在课外从事各种项目,比如我(自豪地)编写的文字象棋游戏。我更喜欢从头开始创建所有东西,这样我就可以在使用各种库之前学习基础知识。

有人可以为我正在寻找的东西指明正确的方向或提供更好的方法来完成这个展示吗?

非常感谢您提前。请让我知道对我的代码的引用是否能更好地帮助回答我的问题。

4

3 回答 3

0

根据我的个人经验,处理此类任务最容易使用的是Processing,它是一个轻量级框架,能够提供简单的 API 来绘制事物。

参考资料和许多教程,因此即使您不是专家,上手也不难。

作为一个侧节点:你为什么使用字符串来存储各种块?你能用更好的东西enum吗?

于 2013-02-08T04:56:09.067 回答
0

首先,您可以使用枚举而不是上面杰克提到的字符串。例如

私有枚举对象{ Rock(1),Coin(8),Med(45)...等等 }

在您的数组中,您可以将这些对象存储为数字而不是字符串。

例如:

     boolean stopFlag=false;    
do{  
  //check each element of your world array with the enum and draw it  
  for(int i=0;i<yourObjectsArray.length;i++)
   {
      for(int j=0;j<yourObjectsArray[i].length;j++){
        switch(yourObjectsArray[i][j])
        {
          case Objects.Rock: drawRock(i,j);
                             break;
          case Objects.Coin: drawCoin(i,j);
                             break;
          //and so on....
        }
     }
   }
  //you can also put this into a separate function as drawWorld() and pass the objects.

//Key press checking logic here. If user presses exit key [esc] then you set the stopFlag  as true
  if(keypress==KEY_ESC){ stopFlag=true;}  
}while(!stopFlag);

画石的一个例子:

private void drawRock(int i,int j){
      //i and j are the cols and row values so you need to resolve them to coordinates.
      //I am assuming u have a 800*600 screen and you mentioned that your world is 100x100 array. Then each of your object is 8*6 units in size so
     xCoord=i*8;
     yCoord=j*6; 
        //So if you have to draw a rock on [10][15] it will resolve as
        //xCoord=10*8-> 80
        //yCoord=15*6-> 90  so it will draw your rock in (80,90) on the screen

//Now you can either open the rock image from your disk now or u maintain one instance of rock at the beginning of the program so that you can use the same image later rather than opening it everytime you encounter a new Rock object in your array.For now I will open it here.

   String path = "C:\\YourGameDirectory\\rock.jpg";
    URL url = new File(path).toURI().toURL();
    BufferedImage rockImg = ImageIO.read(url);

   //draw it to the screen now if you have the graphics instance.
    yourUIPanel.getGraphics().drawImage(rockImg,xCoord,yCoord,yourUIPanel);

  // You may find many resources that teach you how to draw an image on the screen in         Java. You may repeat the same for all the objects.

 }

我希望以上代码对您有所帮助。如果没有,那是我的坏事。

您可以尝试教程系列以开始使用。虽然它在 C 中,但它的概念可以帮助你实现上面提到的内容。

于 2013-02-08T05:56:08.987 回答
0

您可能想查看我的旧 roguelike 游戏 Tyrant 的源代码:

关键思想:

  • 有一个Map类负责存储表示地图的数据。在 Tyrant 中,Map封装了地形和对象在地图上的存储。
  • 有一个MapPanelMap在 GUI 中显示。这与地图本身是分开的——将 GUI 与核心引擎数据分开通常是个好主意。
  • 有一个Thing类表示地图上的对象。这包括从怪物、物品、树木和烟雾到玩家角色本身的一切。基本上任何不是地形的东西。

至于刷新,其工作方式是 MapPanel 根据需要重新绘制自身,查看地图的内容。查看MapPanel.paint(Graphics g)它调用的各种方法。

于 2013-02-08T06:11:38.603 回答