2013-12-16 4 views
-3

나는 간단한 격자 해적 게임을 코딩 해왔다. 나는 플레이어에게 쉽고 어려운 라벨이 붙은 두 개의 버튼이있는 스크린을 보여주기를 바란다. 버튼을 눌렀을 때 화면이 닫히고 다른 크기의 그리드가있는 새 창이 열립니다. 현재 코드는 열심히 설정하고 쉽게 두 개씩 두 개씩 코딩하려는 3 x 3 격자로 작성되었습니다. 내가 문제가되는 것은 다른 gui를 넣기위한 새로운 방법을 선언하는 것입니다. 이미 가지고있는 것과 분리됩니다. 모든 제안/솔루션을 크게 주시면 감사하겠습니다. 스타터내 간단한 게임의 난이도 설정하기

import java.awt.*; 
    import javax.swing.*; 
    import javax.swing.Timer; 
import java.util.*; 
import java.awt.event.*; 





    public class PirateGame extends JFrame implements ActionListener { 

    JLabel label1, label2, label3; 

    ImageIcon image1, image2, image3, image4, image5; 

    JTextField textResult; 

    JButton [] buttons; 


    int treasureLocation; 

    int clicks = 0; 

public int count = 0;  


public static void main(String[] args){ 


    new PirateGame(); 

    } 


    public PirateGame(){ 




    this.setSize(700,700); 
    this.setLocationRelativeTo(null); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setTitle("Treasure Hunt Game"); 

    JPanel thePanel = new JPanel(); 


    thePanel.setLayout(new GridLayout(0,3,0,0)); 

    image1 = new ImageIcon(getClass().getResource("Treasure.jpg")); 
    image2 = new ImageIcon(getClass().getResource("Pirate.jpg")); 
    image3 = new ImageIcon(getClass().getResource("sand2.jpg")); 
    image4 = new ImageIcon(getClass().getResource("emptyhole.jpg")); 
    image5 = new ImageIcon(getClass().getResource("map.jpg")); 

    label1 = new JLabel("Click the buttons!"); 
    label1.setFont(new Font("Serif", Font.PLAIN, 20)); 
    label1.setForeground(Color.red); 


    label2 = new JLabel(image5); 
    label3 = new JLabel(image2); 


    buttons = new JButton[9]; 
    buttons[0] = new JButton(image3); 
    buttons[1] = new JButton(image3); 
    buttons[2] = new JButton(image3); 
    buttons[3] = new JButton(image3); 
    buttons[4] = new JButton(image3); 
    buttons[5] = new JButton(image3); 
    buttons[6] = new JButton(image3); 
    buttons[7] = new JButton(image3); 
    buttons[8] = new JButton(image3); 





    thePanel.add(buttons[0]); 
    thePanel.add(buttons[1]); 
    thePanel.add(buttons[2]); 
    thePanel.add(buttons[3]); 
    thePanel.add(buttons[4]); 
    thePanel.add(buttons[5]); 
    thePanel.add(buttons[6]); 
    thePanel.add(buttons[7]); 
    thePanel.add(buttons[8]); 
    thePanel.add(label1); 
    thePanel.add(label2); 
    thePanel.add(label3); 

    buttons[0].addActionListener(this); 
    buttons[1].addActionListener(this); 
    buttons[2].addActionListener(this); 
    buttons[3].addActionListener(this); 
    buttons[4].addActionListener(this); 
    buttons[5].addActionListener(this); 
    buttons[6].addActionListener(this); 
    buttons[7].addActionListener(this); 
    buttons[8].addActionListener(this); 



    this.add(thePanel); 

    this.setVisible(true); 



    treasureLocation = new Random().nextInt(buttons.length); 


    System.out.println(treasureLocation); 




    } 




    public void actionPerformed(ActionEvent evt){ 
    Object source = evt.getSource(); 

    if (source == buttons[treasureLocation]) { 


    buttons[treasureLocation].setIcon(image1); 
    label1.setText("You've found me Treasure!"); 

    Timer timer = new Timer(3000, 
      new ActionListener() { 


       public void actionPerformed(ActionEvent evt) { 

       PirateGame.this.setVisible(false); 
       PirateGame.this.dispose(); 
       new PirateGame(); 

       } 
      }); 
    timer.setRepeats(false); 
    timer.start(); 
    } 
    else 
    {((JButton)source).setIcon(image4); 
    label1.setText("Keep tryin' ARGG!"); 

    } 


    clicks++; 
    System.out.println(clicks); 


    if ((clicks == 5) && (source != buttons[treasureLocation])){ 

    label1.setText("One more try Matey!"); 

    } 

    if ((clicks == 6) && (source != buttons[treasureLocation])) { 

    label1.setText("Walk the Plank!"); 


    Timer timer2 = new Timer(3000, 
      new ActionListener() { 


       public void actionPerformed(ActionEvent evt) { 

       PirateGame.this.setVisible(false); 
       PirateGame.this.dispose(); 
       new PirateGame(); 

       } 
      }); 
    timer2.setRepeats(false); 
    timer2.start(); 
    } 

    if (clicks == 7){ 

    PirateGame.this.setVisible(false); 
    PirateGame.this.dispose(); 
    new PirateGame(); 
    } 

    } 





} 

답변

0

- 별도의 방법으로 UI를 생성 코드의 모든 이동

인 createUI (INT의 GridSize) 등 당신의 버튼을 만들 수있는 루프에 대한

사용

int numButtons = GridSize * GridSize; 
buttons = new JButton[Grid]; 
for (int i = 0; i < numButtons; i++) 
{ 
buttons[i] = new JButton(image3); 
} 

초보자를 위해 UI가 작동하도록 2 ~ 3을 전달하고 수동으로 코드를 변경하고 두 가지가 모두 작동하는지 테스트합니다.

그런 다음 실행하는 동안 UI를 전환하는 방법에 대해 알아야합니다. 당신은 thePanel를 제거하는 방법을 알아 내야

this.add(thePanel); 

    this.setVisible(true); 

, 새로운 하나를 만들어 추가 : 내 생각 엔 은 당신의 코드에 대한 것입니다. 그것들은 시작을위한 몇 가지 아이디어입니다 ...