如何用Java创建阵列的简单图形显示


在过去的几天里,我用Java编写了一个文本RPG游戏。世界上的定位在很大程度上是由一个简单的100x100 2D字符串数组控制的。然后,当玩家真正进入网格时,我将字符串转换为实际对象。

我想的是有一个图形显示,显示这个100x100的网格,网格的每个部分都有一个与阵列中的图像相对应的图像。

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

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

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

有人能为我指明我想要的东西的正确方向吗?或者在这次展览之后提供一个更好的方法吗?

事先非常感谢。请让我知道引用我的代码是否会更好地帮助回答我的问题。

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

私有枚举对象{岩石(1(,硬币(8(,地中海(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);

Draw Rock的一个例子:

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语言编写的,但它的概念将有助于实现您上面提到的内容。

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

这里有参考资料和许多教程,所以即使你不是专家,也不应该很难上手。

作为侧节点:为什么要使用字符串来存储各种块?你能用enum这样更好的吗?

你可能想看看我的老流氓游戏《暴君:》的源代码

  • https://github.com/mikera/tyrant

关键思想:

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

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

最新更新