2012-04-25 4 views
0

4x4 메모리 게임 인 프로그램을 코딩하고 있습니다. 이 16 개의 상자 안에는 0-7의 정수 쌍이 있습니다. 나는 이미 그 모든 것을 이미 무작위로 분류하고 올바르게 코딩했습니다. 이제는 마우스가 상자를 클릭 할 때마다 해당 정수와 색상을 페어링하는 방법을 알아 내려고합니다.메모리 게임 그래픽 java

여기 대부분의 코드입니다. 나는이 게임의 논리가 아직 시작되지 않았다는 것을 알고 있지만, 나는 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) 
{ 
    buildBoard(temp); 
    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; 
    } 



} 

} 당신이 무슨 짓을했는지에 대해보고 많은이없는 사용자 코드에서

+0

가 왜이 같은 일을 할 코드의 두 덩어리를해야합니까? 왜'temp'를 직접'switch '하지 않고'g.setcolor'를 호출하고'color'를 신경 쓰지 않을까요? –

+0

잘 모르겠다 고 하하. 이전 연구실을보고 있었는데 약간 다른 switch 문을 사용했습니다. – user1215307

+0

고정되어 있고 2D 배열에 0-7을 할당 한 buildBoard 메서드가 호출되었지만 이제는 임의의 색상을 선택하는 것처럼 보입니다. – user1215307

답변

0

,

하지만 난 당신이 다음을 시도해야 같아요

1)이 있는지 확인 컴포넌트는 우리가 그것을 cmp라고 부른다.

2)) (상자를 클릭 cmp.repaint을 확인마다 만들라고

3)) CMP의의 paintComponent (그래픽 g 내에서

을 displayHit (g) 함수를 호출해야합니다 그 해야합니까

또한, temp는 어디에서 조작되고 있습니까?

이 도움이 될해야합니다

public class ColorChanger extends JFrame implements MouseListener { 
    private Color c = Color.black; 
    private JPanel p = new JPanel() { 
     public void paintComponent(Graphics g) { 
      g.setColor(c); 
      g.drawRect(100, 100, 100, 100); 
     } 

    }; 

    public ColorChanger() { 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setPreferredSize(new Dimension(1024, 700)); 
     addMouseListener(this); 
     this.getContentPane().add(p); 
     pack(); 
     setVisible(true); 
    } 


    public static void main (String arg[]) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new ColorChanger(); 
      } 
     }); 
    } 
    @Override 
    public void mouseClicked(MouseEvent arg0) { 
     randomizeColor(); 
     p.repaint(); 

    } 
    private void randomizeColor() { 
     switch ((int)(3*Math.random())) { 
     case 0:c=Color.red;return; 
     case 1:c=Color.blue;return; 
     case 2:c=Color.green;return; 
     } 
    } 
    @Override 
    public void mouseEntered(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void mouseExited(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void mousePressed(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void mouseReleased(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

} 
+0

코드 – user1215307

+0

을 업데이트 할 예정입니다. 놀랍지 만 작동하지 않습니다. –

+0

임시가 시작되었습니다. 맨 위에있는 개인 정적 int – user1215307