简体   繁体   English

Object 层用于 Java RPG

[英]Object Layer for a Java RPG

I'm trying to make a basic RPG in java.我正在尝试在 java 中制作一个基本的 RPG。 I think I have a good hold of the basics of java, but when it comes to this stuff I'm definitely a beginner.我想我对 java 的基础知识有很好的掌握,但说到这些东西我绝对是个初学者。 Anyway, I thought that it would be best to create a map with two layers: a terrain layer with water, rocks, grass, etc... and an object layer with trees, houses, items- things that the player can interact with.无论如何,我认为最好创建一个包含两层的 map:一个包含水、岩石、草等的地形层……以及一个包含树木、房屋、物品的 object 层——玩家可以与之交互的东西。 So far I have a terrain layer, produced using a 2-Dim array.到目前为止,我有一个地形图层,使用 2-Dim 数组生成。 My question is: how can I make an object layer, more specifically, how can I make a layer that contains objects whose coordinates can be interacted with?我的问题是:如何制作 object 图层,更具体地说,如何制作包含坐标可以交互的对象的图层?

Below is what I have so far.以下是我到目前为止所拥有的。 I apologize, my code must be organized strangely- like I said, I'm a beginner with this.抱歉,我的代码必须组织得很奇怪——就像我说的,我是这个的初学者。 Thanks for your time.谢谢你的时间。

-Marcus -马库斯

import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.*;
import javax.imageio.ImageIO.*;
import java.awt.image.*;
import java.awt.image.BufferedImage.*;
import java.io.File.*;
import java.io.*;
import java.awt.Graphics2D.*;

public class mapStoreII extends JFrame implements KeyListener{


BufferedImage pic;
int width = 32;
int height = 32;
int px = 656;
int py = 656;

int[][] map_1 = {
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,1,1,1,1,10,5,11,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,1,1,1,1,3,14,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,1,1,1,1,13,6,12,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},        
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,16,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1}
        };

public mapStoreII(){
    setSize(875,750);
    setLocation(800,150);
    setTitle("Test_Organics_v.1");
    setResizable(true);
    setBackground(Color.black);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    addKeyListener(this);
    setVisible(true);   
}

public void paint(Graphics g){          
    Graphics2D graphics = (Graphics2D) g;
    for(int y = 0; y<20; y++){
        for(int x = 0; x<24; x++){

            try {
            pic = ImageIO.read(new File(getImage(getMap(y,x))));            
            } catch (IOException e) {}

            graphics.drawImage(pic, (x*width)+50, (y*height)+50, null);                         
            graphics.setPaint(Color.red);
            graphics.fill(new Ellipse2D.Double(px,py,32,32));       //Currently the player.
        }
    }
}

public int getMap(int y, int x){
    return map_1[y][x];
}

public String getImage(int a){
    if(a==2){
        return "shoreline_1.png";                           
    }
    else if(a==1){                      
        return "water_1.png";           
    }
    else if(a==9){
        return "player_on_dirt_1.png";
    }
    else if(a==3){
        return "island_left.png";
    }
    else if(a==4){
        return "island_right.png";
    }
    else if(a==5){
        return "island_up.png";
    }
    else if(a==6){
        return "island_down.png";
    }
    else if(a==7){
        return "chemical_1.png";
    }
    else if(a==10){
        return "island_topLC.png";
    }
    else if(a==11){
        return "island_topRC.png";
    }
    else if(a==12){
        return "island_botRC.png";
    }
    else if(a==13){
        return "island_botLC.png";
    }
    else if(a==14){
        return "island_center.png";
    }
    else if(a==15){
        return "shoreline_2.png";
    }
    else if(a==16){
        return "shoreline_3.png";
    }
    else{                                   
        return "grassydirt_1.png";          
    }
}

public void keyPressed(KeyEvent event){ 
    int code = event.getKeyCode();
        if(code == KeyEvent.VK_LEFT || code == KeyEvent.VK_A){
            px-=8;
            repaint();                                                                          
        }
        else if(code == KeyEvent.VK_RIGHT || code == KeyEvent.VK_D){
            px+=8;
            repaint();
        }
        else if(code == KeyEvent.VK_UP || code == KeyEvent.VK_W){
            py-=8;
            repaint();
        }
        else if(code == KeyEvent.VK_DOWN || code == KeyEvent.VK_S){
            py+=8;
            repaint();
        }
        else if(code == KeyEvent.VK_I){         
            lineDraw inventory = new lineDraw();    //Opens an inventory.
        }
    }

public void keyReleased(KeyEvent event){
}

public void keyTyped(KeyEvent event){
}

public static void main(String[] args){
    JFrame frame = new mapStoreII();
}

 }

Sounds like a great project!听起来是个很棒的项目! I had a lot of fun writing a Java RPG / roguelike game called Tyrant a few years back.几年前,我在编写一款名为Tyrant的 Java RPG / roguelike 游戏时获得了很多乐趣。 It's all open source so feel free to explore!这一切都是开源的,所以请随意探索!

Some points you may find helpful:您可能会觉得有帮助的几点:

  • I had an object called Map ( Map.java source )我有一个名为 Map 的 object ( Map.Z93F725A07423FE1C889F448B33D21F4 来源
  • A Map stored both the tiles (in an int[] array) and objects (in a Thing[] array). Map 存储了瓦片(在 int[] 数组中)和对象(在 Thing[] 数组中)。 I used one-dimensional arrays and calculated offsets into them using (x+y*mapWidth) but you can equally well use a 2 dimensional array我使用了一维 arrays 并使用 (x+y*mapWidth) 计算了它们的偏移量,但您同样可以使用二维数组
  • Thing was a separate class that describes all the properties and behaviours of an object.事情是一个单独的 class,它描述了 object 的所有属性和行为。 It also contained a "next" pointer to another Thing, so that you could chain together a list of Things in a single square.它还包含一个指向另一个事物的“下一个”指针,这样您就可以在一个正方形中将事物列表链接在一起。
  • It's a good idea to separate the graphics code from your game logic and game data structures.将图形代码与游戏逻辑和游戏数据结构分开是个好主意。 This helps keep your code maintainable and flexible - for example when you want to have store multiple maps that are not visible.这有助于使您的代码保持可维护性和灵活性 - 例如,当您想要存储多个不可见的地图时。 In my case, I used a MapPanel GUI component which extended JPanel and knew how to draw a Map, but the Map object itself contained no GUI or drawing related code.在我的例子中,我使用了一个MapPanel GUI 组件,它扩展了 JPanel 并且知道如何绘制 Map,但是 Map object 本身不包含 GUI 或绘图相关代码。

Anyway hope this is helpful.无论如何,希望这会有所帮助。 Good luck!祝你好运!

If I may ask, what are you trying to accomplish by keeping the 2 layers separate?如果我可能会问,你想通过保持 2 层分开来完成什么? One would think that a natural complement of interacting with objects is to interact with the terrain as well.人们会认为与对象交互的自然补充是与地形交互。 It would allow for a more extensible game design.它将允许更可扩展的游戏设计。

Perhaps create a model object called Tile to represent each grid and then have a boolean property on that class called interactive .也许创建一个名为Tile的 model object 来表示每个网格,然后在该 ZA2F2ED4F8EBC2CBBDZC21A2 上具有一个interactive属性。 That way you can easily change in the future whether soemthing is interactive.这样,您将来可以轻松地更改某些东西是否是交互式的。 You could also conceivably use interfaces to determine if it serves an interactive role or use inheritance and have a TileClass您还可以想象使用接口来确定它是否提供交互式角色或使用 inheritance 并具有 TileClass

Another thing, you may want to extract your file creation to somewhere other than your paint method.另一件事,您可能希望将文件创建提取到您的paint方法以外的其他地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM