sách gpt4 ai đã đi

java - 内存游戏java

In lại 作者:行者123 更新时间:2023-12-01 15:24:46 26 4
mua khóa gpt4 Nike

Có thể trùng lặp:
memory game graphics java

我正在编写一个 4x4 内存游戏程序。这 16 个盒子里有一对 0 到 7 的整数。我已经将所有这些都随机化并正确编码了。现在,我试图弄清楚每次鼠标单击该框时如何将颜色与相应的整数配对。

这里是大部分代码。我知道这个游戏的逻辑还没有开始,但我现在更关注 displayHit 方法和 setColor 方法。只是发布整个代码,因为也许我在其他地方搞砸了。

/*Sets the background of your memory board to black*/
public void init()
{
setSize(400,400);
setBackground(Color.BLACK);
buildBoard(4);

}
/*This is main in java applets
You may need to add (not change) a couple things in this method
*/
public void paint(Graphics canvas)
{
if(firstRun) //for the first run we need to build our random board
{

print2DArray(board);
buildBoard(4);
firstRun = false;
}
else // once our board is built we will display the game
{
displayGame(canvas);
if (mouseClicked) // if the mouse has been clicked
{
displayHit(canvas);//find which box the user clicked
mouseClicked = false;
}
}
}

/*
DO NOT change this method
determines if the mouse has been pressed
sets x and y Mouse to the location of the mouse arrow
redraws the image
*/
public boolean mouseDown(Event e, int x, int y )
{
mouseClicked = true;
xMouse = x;
yMouse = y;
repaint();
trả về giá trị đúng;
}

/*DO NOT change this method
redraws the scene
*/

public void update ( Graphics g )
{
paint(g);
}

/*
pre: none
post: build an array that holds the memory values for a board of size x size
the board will hold two of each int from 0 to size randomly placed in the array
*/


public void buildBoard(int s)

{
int a = 4;
for (int row = 0; row < a; row++)
for (int column = 0; column < a; column++)
{

board[row][column] = count++ % 8;
}
for(int row = 0; row < 4; row++)

for(int column = 0; column < 4; column ++)
{
int x = (int)Math.floor(Math.random()*4);
int y = (int)Math.floor(Math.random()*4);
temp = board[row][column];
board[row][column] = board[x][y];
board[x][y] = temp;


}
}
public static void print2DArray(int[][] arr)
{
for (int row = 0; row < arr.length; row++)
{
for (int col = 0; col < arr[row].length; col++)
{
System.out.print(arr[row][col] + " ");
}
System.out.println();
}
}





public void displayGame(Graphics canvas)
{
canvas.setColor(Color.WHITE);

for(int i =0; i < 400; i+= WIDTH)
for(int j = 0; j < 400; j+= WIDTH)
canvas.drawRect(i, j, WIDTH, WIDTH);
}

/*
Pre: xMouse and yMouse have been initialized
Post: A circle is displayed in the correct box on the screen
Currently the circle is displayed at the mouse location
*/
public void displayHit(Graphics g)
{

setColor(g);
centerHit(xMouse, xMouse);
g.fillOval(xMouse, yMouse, 40, 40);
}

public void setColor(Graphics g)
{

switch(temp)
{
case 0: g.setColor(Color.RED);
phá vỡ;
case 1: g.setColor(Color.GREEN);
phá vỡ;
case 2: g.setColor(Color.BLUE);
phá vỡ;
case 3: g.setColor(Color.ORANGE);
phá vỡ;
case 4: g.setColor(Color.CYAN);
phá vỡ;
case 5: g.setColor(Color.MAGENTA);
phá vỡ;
case 6: g.setColor(Color.PINK);
phá vỡ;
case 7: g.setColor(Color.YELLOW);
phá vỡ;
}

}
public void centerHit(int centerX, int centerY)
{
{
if ((xMouse > 0) && (xMouse <=100))
xMouse = 33;
else if ((xMouse > 100) && (xMouse <=200))
xMouse = 133;
else if ((xMouse > 200) && (xMouse <=300))
xMouse = 233;
else if ((xMouse > 300) && (xMouse <=400))
xMouse = 333;
}
{
if ((yMouse > 0) && (yMouse <=100))
yMouse = 33;
else if ((yMouse > 100) && (yMouse <=200))
yMouse = 133;
else if ((yMouse > 200) && (yMouse <=300))
yMouse = 233;
else if ((yMouse > 300) && (yMouse <=400))
yMouse = 333;
}



}

}

1 Câu trả lời

一旦调用 buildBoard,temp 就永远不会改变。这就是为什么您总是看到相同的颜色。

关于java - 内存游戏java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10319100/

26 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com