2015-01-05 3 views
0

내 코드를 개발하는 데 약간의 어려움이 있습니다. Java로는 너무 발전하지 않았기 때문에 도움이 필요합니다. 스레드를 사용하여 미니 테니스 게임을 개발하려고합니다. 이 게임의 목적은 키보드의 좌우 버튼으로 제어 할 수있는 패들로 창가에서 움직이는 공을 잡는 것입니다.미니 테니스 게임 스레드를 사용하여 Java

그 공들은 창문에서 비스듬히 움직여야하고 그들이 어떤 코너 (바닥에서 벗어남)에 닿으면 빛의 반사와 같은 방식으로 바뀌어야합니다. 이 외에도 볼이 장애물 중 하나에 닿으면 장애물을 변경해야합니다.

왼쪽 및 오른쪽 키를 사용하여 창 하단의 패들을 제어 할 수 있습니다. 플레이어의 작업은 공을 잡는 것입니다. 사용자가 잡은 볼의 수는 점수 부분에 표시되며 볼의 총 개수는 하단 코너로 이동합니다.

사용자는 게임 상태를 저장해야 할 수 있습니다. 사용자가 "게임 저장"버튼을 클릭하면; 공 위치와 점수는 파일에 저장해야합니다. 그리고 사용자가 열림 버튼을 클릭하면 게임 상태를 다시 불러 와야합니다.

내 소스 코드 파일은 다음과 같습니다

public class BallPanel extends JPanel implements Runnable { 

int RED, GREEN, BLUE; 
int Xdirection = 1, Ydirection = 1; 
boolean pleaseWait = false; 

BallPanel(int X, int Y){ 
    locateBall(X, Y, 30, 30);    
/* Random r = new Random(); 
    RED = r.nextInt(255); 
    GREEN = r.nextInt(255); 
    BLUE = r.nextInt(255); 
    */ 
} 
public void paint(Graphics g){ 
    int panelWidth = this.getWidth(); 
    int panelHeight = this.getHeight();  

// g.setColor(new Color(RED, GREEN, BLUE)); 
    g.setColor(Color.ORANGE); 

    g.fillOval(panelWidth/2, panelHeight/2,panelWidth/2, panelHeight/2); 
} 

public void locateBall(int x, int y, int width, int height){ 

    setBounds(x, y, width, height); 
    repaint(); 
} 

public void run() { 

    int width = this.getWidth(); 
    int height = this.getHeight(); 
    Random r = new Random(); 

    while(true){ 
     if(!pleaseWait){ 
      int lastX = this.getX(); 
      int lastY = this.getY(); 

      if (lastX > 675) Xdirection = -1; 
      if (lastY > 485) Ydirection = -1; 
      if (lastX < -5) Xdirection = 1; 
      if (lastY < -5) Ydirection = 1; 

    /*  if(lastX > 280 && lastY > 170){ 
       Xdirection = -1; 
       Ydirection = -1; 
      } 
    */  
      locateBall(lastX + Xdirection*r.nextInt(3), 
         lastY + Ydirection*r.nextInt(3), 
         width, height); 
     } 
     try{ 
      Thread.sleep(5); 
     }catch(Exception e){}; 
    } 

} 

} 


public class BallWindow extends JFrame implements ActionListener{ 
    JButton btnStop = new JButton("STOP"); 
    JButton btnSave = new JButton("SAVE"); 
    Vector<BallPanel> ballVector = new Vector<BallPanel>(); 

    JPanel p1 = createPanel(280, 200, 200, 20, Color.gray); 
    JPanel p2 = createPanel(280, 300, 200, 20, Color.gray); 
    JPanel bottomp = createPanel(345, 540, 70, 15, Color.black); 

    JPanel lborder = createPanel(10, 10, 2, 560, Color.black); 
    JPanel rborder = createPanel(720, 10, 2, 560, Color.black); 
    JPanel tborder = createPanel(10, 10, 710, 2, Color.black); 



public BallWindow() { 
    setLayout(null); 
    btnStop.setBounds(12, 15, 100, 30); 
    btnStop.addActionListener(this); 
    add(btnStop); 
    btnSave.setBounds(12, 50, 100, 30); 
    //btnSave.addActionListener(this); 
    add(btnSave); 
    Random r = new Random(); 
    for(int i=0; i<7; i++){ 
     BallPanel bp = new BallPanel(r.nextInt(740), r.nextInt(590)); 
     Thread t = new Thread(bp); 
     ballVector.add(bp); 
     t.start(); 
     add(bp); 
    } 


    add(p1); 
    add(p2); 
    add(bottomp); 
    add(lborder); 
    add(rborder); 
    add(tborder); 

    setSize(740, 590); 
    setVisible(true); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setResizable(false); 
    repaint(); 
} 


JPanel createPanel(int x, int y, int width, int height, Color pColor){ 
    JPanel temp = new JPanel(); 
    temp.setBackground(pColor); 
    temp.setBounds(x, y, width, height); 
    return temp;  
} 



public static void main(String[] args) { 
    new BallWindow(); 

} 

public void actionPerformed(ActionEvent arg0) { 
    for (BallPanel ball : ballVector) { 
     ball.pleaseWait = !ball.pleaseWait; 
    } 

    if(btnStop.getText().equalsIgnoreCase("STOP")) 
     btnStop.setText("START"); 
    else 
     btnStop.setText("STOP"); 

// if(arg0.getSource()) 

} 

} 

나는 장애물 부분과 모든 KeyListener와 붙어있어. 도움의 모든 유형을 크게 주시면 감사하겠습니다.

+0

https://www.dropbox.com/s/a8pyl9x6cib3s4i/1.png?dl=0 여기 내가 지금까지 개발 한 것 (저는 여기에서 새로 생겨서 아직 그림을 게시 할 수 없습니다)) –

+0

그건 꽤 비트가 독서라면 ... 그리고 당신은 심지어 당신이 문제라고 말한 적도 없을 것입니다/: 또한, 이것을 위해서 다중 스레드가 필요한가요? (EDT 및 가능한 게임 스레드 제외) –

+0

아마도 도움이 될 것입니다 : https://github.com/m1dnight/JPong –

답변

0

하바 당신은 특히 기본 및 애니메이션 섹션을 확인해야 http://zetcode.com/tutorials/javagamestutorial/

봐. 애니메이션 및 실을 정리하는 데 도움이됩니다. 또한 자바 게임을 구현할 수있는 일반적인 패턴을 보여줍니다.

+0

고마워요 ... 나를 도와주고 있습니다. 나는 오늘 밤 불면의 밤을 보낼 예정이다 :)))) –