2012-04-25 92 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