一个简单的滑块游戏(游戏逻辑和绘画组件方法)
我正在构建一个简单的滑块游戏。我使用repaint()方法绘制拼图图像的块,然后在BufferedImage数组(help [empty_row] [empty_col] = null;)的帮助下绘制一个空块。空块是您通常在拼图中使用的空白框。一个简单的滑块游戏(游戏逻辑和绘画组件方法)
事情是,我“调用”绘制方法来绘制我的拼图的所有块,我有一个空块。每当用户点击它时,我将empty_row,empty_col更改为鼠标事件的getX(),getY(),以便将空白框移动到所需的位置。
什么...困惑我在这个难题:)是先前的空白框仍然出现后,我已经画出了新的。这对你有意义吗?
With 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类:
进口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) {
}
}
我觉得你的问题是下一个:
if (SBModel.slideBlock(thisRow,thisCol)==true)
repaint();
你打电话给你的幻灯片功能,然后重新绘制(至极是不是所有的错),但在滑动功能中,您只需移动空白部分,在任何时候您都可以用图像滑动该部分。在滑动功能中,您需要先将图像移动到图像上,然后移动空白图像,然后重新绘制。
True Chuck,谢谢。这就是我现在正在做的事情。 :) – 2011-03-13 15:49:28
@Zoi很高兴我能帮忙 – 2011-03-13 16:14:55
你能告诉我们一些你的代码吗?顺便说一句,我假设这是Java代码? – 2011-03-13 14:00:24
哦,对不起查克!你应该假设这是Java代码。 :) – 2011-03-13 14:14:19
但丁,那是重绘()做的。还是我误会了?看看我的paint()方法。 – 2011-03-13 14:21:37