-1

私はコードがそれ自体のために話すと信じていますが、一般的にコードのポイントは、多くのレイヤーのマップを構成するために、x 値、および y 値Mapの配列を受け取るクラスを持つことです (最初のレイヤーは配列です) x の値を 0 に、y の値を 0 に、など)。マップ クラスの主な仕事は、各画像の各ピクセルを取得し、それらをオブジェクトに変換することです。オブジェクトは、単に色の付いた四角形です ( が含まれています。機能した後、色を画像に置き換えるためです。また、含まれています許可されるレイヤー (1 はインデックス 0) を指定する整数。0 はすべてのレイヤーに存在できることを意味します)。最後に、Render() を呼び出すと、BufferedImagesBufferedImageBlockBufferedImageMapオブジェクトの場合、マップ オブジェクトは、ブロックを正しい位置にレンダリングするすべての作業を行う必要があります。これらすべての最大の問題は、構文エラーやコンパイラ エラーが発生しないことです。そのため、ロジックが混乱していて、それを理解できません。

事前に感謝します。質問がわかりにくい場合は教えてください。

マップ クラス:

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;


public class Map {

    private int width;
    private int height;
    public int getWidth() { return width; }
    public int getHeight() { return height; }

    private int xPos;
    private int yPos;
    public int getX(int i) 
    { 
        return xPos; 
    }
    public int getY(int i) 
    { 
        return yPos; 
    }
    public void setPosition(int x, int y) { xPos = x; yPos = y; }

    private int[] xStarts;
    private int[] yStarts;

    private ArrayList<BufferedImage> layersList = new ArrayList<BufferedImage>();
    public void addLayer(BufferedImage image) { layersList.add(image); }
    public void setLayer(int i, BufferedImage image) { layersList.set(i, image); }

    private Block[][][] blocksArray;
    private boolean beenInitialized = false;


    public Map(BufferedImage[] images, int[] x, int[] y){
        for (BufferedImage image : images){
            layersList.add(image);
            xStarts = x;
            yStarts = y;
        }
    }

    public void initialize(){
        int widthMax = 0;
        int heightMax = 0;
        for (BufferedImage image : layersList){
            if (image.getHeight() > heightMax) { heightMax = image.getHeight(); }
            if (image.getWidth() > widthMax) { widthMax = image.getWidth(); }
        }

        width = widthMax;
        height = heightMax;

        blocksArray = new Block[layersList.size()][width][height];

        for (int i = 0; i < layersList.size(); i++){

            int currentLayer = i;

            for (int y = 0; y < layersList.get(i).getHeight(); y++){
                for (int x = 0; x < layersList.get(i).getWidth(); x++){

                    int colorCode = layersList.get(i).getRGB(x, y);
                    boolean error = true;
                    Block b = null;

                    for (int c = 0; c < Block.BLOCKS.size(); c++){
                        if (Block.BLOCKS.get(i).getColorCode() == colorCode && (Block.BLOCKS.get(i).getLayerCode() == currentLayer || Block.BLOCKS.get(i).getLayerCode() == 0)){
                            b = Block.BLOCKS.get(c);
                            error = false;
                        }
                    }

                    if (!error){
                        blocksArray[currentLayer][x][y] = b;
                    } else {
                        Block bb = new Block(false, colorCode);
                        bb.initialize();
                        blocksArray[currentLayer][x][y] = bb;
                    }

                }
            }

        }

        beenInitialized = true;
    }

    public void render(Graphics2D g2d){
        if (beenInitialized){
            for (int i = 0; i < layersList.size(); i++){

                for (int y = yStarts[i]; y < layersList.get(i).getHeight() + yStarts[i]; y += Block.SIZE){
                    int currentY = 0;
                    for (int x = xStarts[i]; x < layersList.get(i).getWidth() + xStarts[i]; x += Block.SIZE){
                        int currentX = 0;

                        blocksArray[i][currentX][currentY].setPosition(x, y);
                        blocksArray[i][currentX][currentY].render(g2d);

                        currentX ++;
                    }
                    currentY++;
                }

            }
        }
    }

    public void updatePosition(int x, int y){
        xPos += x;
        yPos += y;
    }


}

ブロッククラス:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.ArrayList;


public class Block {
    public static final int SIZE = 32;
    public static final boolean DEBUG = true;
    public static ArrayList<Block> BLOCKS = new ArrayList<Block>();

    private Color debugColor;
    public Color getColor() { return debugColor; }
    public void setColor(Color color) { debugColor = color; }

    private BufferedImage blockIcon;
    public BufferedImage getIcon() { return blockIcon; }
    public void setIcon(BufferedImage icon) { blockIcon = icon; }

    private int xPos;
    private int yPos;
    public int getX() { return xPos; }
    public int getY() { return yPos; }
    public void setPosition(int x, int y) { xPos = x; yPos = y; }

    private Rectangle blockShape;
    public Rectangle getShape() { return blockShape; }

    private int colorCode;
    public int getColorCode() { return colorCode; }

    private boolean colides;
    public boolean doesColide() { return colides; }

    private int layerCode;
    public int getLayerCode() { return layerCode; }

    private boolean beenInitialized = false;


    public Block(boolean colides, int layerCode){
        this.colides = colides;
        this.layerCode = layerCode;
    }

    public void initialize(){
        blockShape = new Rectangle(xPos, yPos, SIZE, SIZE);

        int r = (colorCode >> 16) & 0x000000FF;
        int g = (colorCode >> 8) & 0x000000FF;
        int b = (colorCode) & 0x000000FF;

        debugColor = new Color(r, g, b);

        BLOCKS.add(this);
        beenInitialized = true;
    }

    public void render(Graphics2D g2d){
        if (beenInitialized){
            if (DEBUG){
                g2d.setColor(debugColor);
                if (colides){
                    g2d.fill(blockShape);
                } else {
                    g2d.draw(blockShape);
                }
            } else{

            }
        }
    }
}

そして最後に Game クラス (テスト用のウィンドウを表示するためだけにこれをまとめました):

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Game extends JFrame{

    public Game(){  
        super("Test");

        try{
            layer1 = ImageIO.read(getClass().getResourceAsStream("/layer1.png"));
            layer2 = ImageIO.read(getClass().getResourceAsStream("/layer2.png"));
        } catch (Exception ex) { ex.printStackTrace(); }


        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);

        setLayout(new BorderLayout());
        add(new panel(), BorderLayout.CENTER);
        pack();

        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args){
        new Game();
    }

    private int[] xStartPositions = {0, 0};
    private int[] yStartPositions = {0, 0};

    private BufferedImage layer1;
    private BufferedImage layer2;

    private BufferedImage[] imageArray = {layer1, layer2};

    private Map map;






    public class panel extends JPanel{
        public panel(){
            setMinimumSize( new Dimension(1200, 675));
            setMaximumSize( new Dimension(1200, 675));
            setPreferredSize( new Dimension(1200, 675));

            setVisible(true);

            map = new Map(imageArray, xStartPositions, yStartPositions);
        }

        public void paint(Graphics g){

            Graphics2D g2d = (Graphics2D) g;

            map.render(g2d);

        }
    }


}
4

1 に答える 1