一个简单的滑块游戏(游戏逻辑和绘画组件方法)

时间:2011-03-13 13:55:28

标签: java logic paint repaint

我正在构建一个简单的滑块游戏。我使用repaint()方法绘制拼图图像的块,然后借助BufferedImage数组绘制空块(puzzle [empty_row] [empty_col] = null;)。空块是您在拼图中通常拥有的空白框。

问题是,我“调用”绘制方法来绘制我的拼图的所有块,我有一个空的块。每次用户点击它旁边时,我都会将empty_row,empty_col更改为鼠标事件的getX(),getY(),以便将空白框移动到所需位置。

什么......在这个难题中让我感到困惑:)是我在绘制新的空白框之后仍然会出现以前的空白框。这对你有意义吗?

使用repaint()我会希望重新绘制块,只留下一个空白框。

有什么想法吗?这是我在这里的第二篇文章,我看到人们非常愿意提供帮助。对此,我真的非常感激。提前感谢您的回答。

ZOI

我的SlidingBlockPanel类的代码位:

public SlidingBlockPanel(int nc, int nr)
{
    numCols = nc;
    numRows = nr;
    addMouseListener(this);
    SBModel= new SlidingBlockModel(numCols, numRows, "puzzle.jpg");
}

public void mouseClicked(MouseEvent event)
{
    int thisCol = getCol(event.getX());
    int thisRow = getRow(event.getY());
    System.out.println
    ("you clicked in row " + thisRow);
    System.out.println
    ("you clicked in column " + thisCol);

    if (SBModel.slideBlock(thisRow,thisCol)==true)
    repaint();

}


Rectangle getRect(int thisCol, int thisRow)
{
    // if input is out of range, return "null"
    if(thisCol <0 || thisRow < 0)
        return null;
    if(thisCol>=numCols || thisRow>=numRows)
        return null;

    // otherwise, make and return the Rectangle
    int w = getWidth()/numCols;
    int h = getHeight()/numRows;

    int x = thisCol*w;
    int y = thisRow*h;

    Rectangle myRect = new Rectangle(x,y,w,h);
    return myRect;
}

public void paint(Graphics g)
{
    //g.setColor(Color.gray);
    g.fillRect(0,0,getWidth(), getHeight());
    g.setColor(Color.black);

    Graphics2D g2 = (Graphics2D)g;
    // we'll use Graphics2D for it's "drawImage" method this time

    for (int i = 0;i<numCols;i++)
    {
        for(int j = 0;j<numRows;j++)
        {
            Rectangle r = getRect(i, j);
            g2.drawImage(SBModel.getSubimage(i, j),r.x,r.y,r.width,r.height,null);

        }
    } 



}

和SlidingBlockModel类:

import java.awt.image。; import java.io。; import javax.imageio。; import java.awt.Image。; import java.awt.Graphics。*;

类SlidingBlockModel  {

BufferedImage original_image; // the image to be cut up into blocks
int numCols;  // number of columns in the grid of blocks
int numRows;  // number of rows in the grid of blocks
int empty_col=0; // holds the column index of the one empty grid element
int empty_row=3; // holds the row index of the one empty grid element
BufferedImage[][] puzzle; // the two '[][]' allows a 2-D array 
                          // to be created - each element is an image



public SlidingBlockModel (int input_numCols, int input_numRows, String filename) {


String image_filename=filename;

numCols=input_numCols;
numRows=input_numRows;


original_image = null;

try
{
original_image = ImageIO.read(new File(image_filename));
System.out.println("image " + image_filename + " loaded in ok." );
System.out.println("Width: " + original_image.getWidth() + ", height: " + original_image.getHeight());
    }


catch (Exception e)
{
System.out.println("Sorry - couldn't load in file " + image_filename);
    }


//cut up the original image into 'blocks' and 
//assign each of these to the elements of the puzzle 2D array

puzzle = new BufferedImage[numCols][numRows];


for (int i=0;i<numCols;i++) {

    for (int j=0;j<numRows;j++){

    puzzle[i][j]=getImageRect(i,j);

    }

}

//Initialise the empty block

puzzle[empty_row][empty_col] = null; 


}

    //slide the block indicated by the two parameters, if possible
    boolean slideBlock(int thisCol, int thisRow) {

        if(thisCol<0 || thisCol>numCols)
        return false;

        if (thisRow<0 || thisRow>numRows)
        return false;

        if (thisRow==empty_row) {

            if ((thisCol==empty_col-1) || (thisCol==empty_col+1)) {
                empty_col=thisCol;
                puzzle[empty_row][empty_col]=null;
                return true;
                }
        }

        else


            if (thisCol==empty_col) {

            if ((thisRow==empty_row-1) || (thisRow==empty_row+1)) {
                empty_row=thisRow;
                puzzle[empty_row][empty_col]=null;
                return true;
                }
        }



        return false;

    }




    //return the BufferedImage for any given grid element

    BufferedImage getSubimage(int thisCol, int thisRow) {

    if(thisCol<0 || thisCol>numCols)
    return null;
    //if(thisRow<0 || thisRow>=max_num_counters)

    if (thisRow<0 || thisRow>numRows)
    return null;
    else
    return puzzle[thisCol][thisRow];

    }



    private BufferedImage getImageRect(int thisCol, int thisRow){

    // if input is out of range, return "null"
    if(thisCol <0 || thisRow < 0)
        return null;
    if(thisCol>=numCols || thisRow>=numRows)
        return null;
    else {

    // otherwise, make and return the Rectangle
    int w = original_image.getWidth()/numCols;
    int h = original_image.getHeight()/numRows;

    int x = thisCol*w;
    int y = thisRow*h;

    BufferedImage myRect; 

    myRect=original_image.getSubimage(x,y,w,h);

    return myRect;
    }
}





public static void main (String[] args) {

}

}

1 个答案:

答案 0 :(得分:1)

我认为你的问题是下一个问题:

if (SBModel.slideBlock(thisRow,thisCol)==true)
repaint();

你调用你的幻灯片功能,然后重新绘制(并非完全错误),但在你的幻灯片功能中,你只需移动空白部分,在任何时候你用图像滑动它。在你的幻灯片功能中,你需要首先移动带有图像的部分,然后移动空白部分,然后重新绘制。