2012-04-25 95 views
0

可能重複:
memory game graphics java記憶遊戲的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(); 
    return true; 
} 

/*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); 
    break; 
    case 1: g.setColor(Color.GREEN); 
    break; 
    case 2: g.setColor(Color.BLUE); 
    break; 
    case 3: g.setColor(Color.ORANGE); 
    break; 
    case 4: g.setColor(Color.CYAN); 
    break; 
    case 5: g.setColor(Color.MAGENTA); 
    break; 
    case 6: g.setColor(Color.PINK); 
    break; 
    case 7: g.setColor(Color.YELLOW); 
    break; 
    } 

} 
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; 
    } 



} 

}

+2

所以它有什麼問題,是你得到的錯誤還是顏色是隻是沒有出現? – 2012-04-25 15:48:32

+0

它只使用(o,o)數組索引中的第一種顏色,無論開關語句分配給該隨機整數,並且每次點擊使用相同的顏色 – user1215307 2012-04-25 15:52:54

+0

您應該總是在for循環中使用大括號。它使代碼更具可讀性。 – Jim 2012-04-25 16:00:51

回答

1

它看起來像一次buildBoard是所謂的氣溫從未改變。這就是爲什麼你總是看到相同的顏色。

+0

我對你在說什麼感到困惑 – user1215307 2012-04-25 16:10:34

+0

@ user1215307 temp字段用於確定SetColor方法中的顏色,但此值在buildBoard()中設置,因此不管點擊什麼都不會改變。 – Jim 2012-04-25 16:15:14

+0

我認爲temp是爲16個板塊中的每個板塊分配的變量,範圍從0到7的對值 – user1215307 2012-04-25 16:25:29

0

這個代碼是罰款

public void setColor(Graphics g) 

{

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

} 

,但你是不是在代碼中選擇這種情況下/顏色,你要使用的任何地方設定溫度。你需要改變溫度,當你點擊一個正方形,通過硬編碼或隨機產生0-7之間的數字來隨機設置顏色

+0

板[(int)xMouse/100] [(int)yMouse/100] = colorIndex; \t \t \t \t開關(colorIndex) 這是我的下一個想法,但它也只是選擇一種顏色,但我不知道爲什麼,因爲我想象它會查找該值在基於每個陣列鼠標點擊並分配它的尊重顏色 – user1215307 2012-04-25 16:37:25