Java |缓冲图像工作但不是我想要的和键绑定做同样的事情

时间:2016-05-04 18:55:09

标签: java bufferedimage key-bindings

背景资料

<小时/> 所以我在过去的3-4周里一直致力于Java游戏的最终项目。它应该做的是生成一个“地图”,玩家必须通过该地图才能到达右下角的“出口”(现在显示为黑点)。 “地图”是随机生成的,因此没有两个是相同的,并且会导致玩家可能需要使用比他们想要的更多“工具”(尚未添加)。

问题 1.)手头的第一个问题是我试图制作“键绑定”并且他们编译并且不打破游戏,当按下按键时,玩家不会沿指示的方向移动。

我知道问题的一部分是我在“地图”上没有边界,因为不知道如何制作它们导致我的数组出现超出范围的异常。

与此相符的另一个是,当我重新绘制()时,它完全重做整个“地图”,当我想要的是将玩家移动到新位置同时移除他们遇到的任何“地形”时(不移除)直到后来的地形)。

2。)试图找出问题1我回顾了之前的一篇帖子(Rows and columns with multidimensional array java),MadProgrammer说了这句话

就个人而言,在mapGen中,我会创建一个BufferedImage,宽24 * 20,高24 * 20,将所有瓷砖绘制成它,并在paintComponent方法中绘制图像

所以我读了BufferedImage的功能及其工作原理,并尝试将其应用到我的游戏中。结果灾难性的地方。它编译并运行良好,但结果与我想要实现的目标相差甚远

结果: enter image description here

我正在拍摄的内容: enter image description here


代码我添加了这个/不工作代码

“键绑定”:

//I have all imports neccessary
public class gamePanel extends JPanel implements ActionListener, KeyListener
{                                   //added^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    public gamePanel()
    {
        //left out my bounds for shortening reasons
        addKeyListener(this);//added
        setFocusable(true);//added
        setFocusTraversalKeysEnabled(false);//added

    }

    //................................

    public void actionPerformed(ActionEvent e){
        repaint();
    }   
        // below process is repeated for left, right, and down
        //for now just trying to get the player to move
        //not worried about removing terrain just yet
        public void up(){
            if(coords[playerX[pcX]][playerY[pcY-1]] == terrain[floor]){
                playerY[pcY] = playerY[pcY- 1] ;
            }

            if(coords[playerX[pcX]][playerY[pcY]-1] == terrain[dirt]){
                playerY[pcY] = playerY[pcY] - 1;
            }

            if(coords[playerX[pcX]][playerY[pcY]-1] == terrain[stone]){
                playerY[pcY] = playerY[pcY] - 1;
            }

            if(coords[playerX[pcX]][playerY[pcY]-1] == terrain[iron]){
                playerY[pcY] = playerY[pcY] - 1;
            }

            if(coords[playerX[pcX]][playerY[pcY]-1] == terrain[gold]){
                playerY[pcY] = playerY[pcY] - 1;
            }

            if(coords[playerX[pcX]][playerY[pcY]-1] == terrain[diamond]){
                playerY[pcY] = playerY[pcY] - 1;
            }

            if(coords[playerX[pcX]][playerY[pcY]-1] == terrain[emerald]){
                playerY[pcY] = playerY[pcY] - 1;
            }

            //................
            public void keyTyped(KeyEvent e){}//would not compile without these two lines
            public void keyReleased(KeyEvent e){}

        }

}

<小时/> 缓冲图像代码:

    //player coords
    int pcY = 1;
    int pcX = 1;

    int[] playerX = new int[pcX + 1];
    int[] playerY = new int[pcY + 1]; // x,y for the player
    //^^^^pretty sure this is the wrong way to make array start at 1

        @Override
    public void paintComponent(Graphics g)//what will paint each 20x20 square on the grid what it is assigned
    {

        super.paintComponent(g);

        Image img = drawmap();//draw the below bufferedimage
        g.drawImage(img, 520, 520, this);

        g.setColor(Color.red);//creates the player "model"
        g.fillOval((pcX*20),(pcY*20),20,20);

        g.setColor(Color.black);
        g.fillOval((23*20),(23*20),20,20);

    }//end paintComponent

private Image drawmap(){

        BufferedImage map = new BufferedImage(width*20, height*20, BufferedImage.TYPE_INT_RGB);
        Graphics g = map.getGraphics();


    for(int x = 1; x < width; x++)
        {
            for(int y = 1; y < height; y++)
            {   
                if(coords[x][y] == terrain[floor])// paints floor color at marked coordinates
                {
                    g.setColor(new Color(249,249,249));
                    g.fillRect((x*20), (y*20), 20, 20); 

                }
                //^^^^same proccess for all the rest of the terrain

            }
        }

    return map; 
}


备注

<小时/> 如果所有的解释都可以成为外行人的条款,那将是非常棒的,因为工程师的术语会让我感到困惑。

此外,如果这需要两个不同的帖子,请告诉我,不要粗鲁。

这是我编写的代码的链接,它编译并生成我想要的内容,但是键绑定不起作用(如果你想看一下):https://www.dropbox.com/sh/78mj9aes2c0598a/AADZoJWlw79I5Z0Ub3uon6ZVa?dl=0

这是一个指向“工作”代码的链接,我尝试了BufferedImage(如果你想看):https://www.dropbox.com/sh/hujm1ztrttkdq4f/AAB9j9P3PvPQpRNi_ckzTHOha?dl=0

0 个答案:

没有答案