gpt4 book ai didi

java - 一个简单的 slider 游戏(游戏逻辑和绘制组件方法)

转载 作者:行者123 更新时间:2023-12-01 15:54:48 26 4
gpt4 key购买 nike

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

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

这个谜题让我困惑的是:)在我绘制了新的方框后,之前的空白方框仍然出现。这对你来说有意义吗?

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

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

佐伊

我的 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 类:

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

类 slider 模型 {

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) {

}

}

最佳答案

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

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

您调用幻灯片函数,然后重新绘制(这并不全是错误的),但在您的幻灯片函数中,您只移动空白 block ,在任何时刻滑动带有图像的 block 。在幻灯片功能中,您需要首先移动带有图像的部分,然后移动空白部分,然后重新绘制。

关于java - 一个简单的 slider 游戏(游戏逻辑和绘制组件方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5289807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com