2013-03-22 75 views
1
package gameprojekt; 

import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Toolkit; 
import javax.swing.JFrame; 
import javax.swing.WindowConstants; 

//The GameWindow class holds the window 
public class Game extends JFrame { 

    /*Global variable declaration*/ 
    private int width; 
    private int height; 
    private int windowXPos; 
    private int windowYPos; 

    public static String p1 = "Player1"; 
    public static String p2 = "Player2"; 

    public static int playerScore = 0; 
    public static int oponentScore = 0; 

    public static int player1X; 
    public static int Player1Y; 
    public static int player2X; 
    public static int Player2Y; 

    private static boolean running = true; 

    public static int status = 0; 
    public static JFrame frame = new JFrame("Pong"); 
    //public TestDrawPanel testPanel = new TestDrawPanel(); 

    public static int getStatus() { 
     return status; 
    } 

    public static void setStatus(int status) { 
     Game.status = status; 
    } 

    // ------------------------------------------------------------ 

    /** 
    * Creates a new JFrame window with the given size and 
    * center it based on the screen resolution 
    */ 
    public static final long serialVersionUID = 1L; 

     public Game() { 
     /*Local variable declaration*/ 
     //JFrame frame = new JFrame("Pong"); 
     Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 

     width = (int)dim.getWidth(); 
     height = (int)dim.getHeight(); 

     windowXPos = width/2 - (width/2)/2; 
     windowYPos = height/2 - (height/2)/2; 
     // ------------------------------------------------------------ 

     // Set size, half of the screen resolution 
     frame.setSize(width/2, height/2); 
     // Allign the window to the users resolution 
     frame.setLocation(windowXPos, windowYPos); 
     frame.setVisible(true); 
     frame.setResizable(false); 
     // By exiting the window using "X" all relevant data is closed 
     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    } 


    /* zum Testen auskommentiert 
    @Override 
    public void paint(Graphics g) { 
     System.out.println("test"); 
     this.drawPlayer(g); 
    }*/ 

    /** 
    * Draw the Player on the given location and with the given size 
    * @param g Graphics object 
    */ 
    public void drawPlayer(Graphics g) { 

    } 

    private static void gameLoop() { 
     Menue m = new Menue(); 
     m.loadMenue(frame); 

     while (running) { 
      if (m.isStartPressed()) { 
       System.out.println("test"); 
      } 

     } 
    } 

    /** 
    * Create the game and initialize the gameplay 
    */ 
    public static void main(String[] args) { 
     /*Variable declaration*/ 

     // ------------------------------------------------------------ 
     Game game = new Game(); 
     game.gameLoop(); 
    } 
} 

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package gameprojekt; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.LayoutManager; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

/** 
* 
* 
*/ 
public class Menue { 

    /* Global variable declaration */ 
    private int widthMenue; 
    private int heightMenue; 
    private String start = "Start"; 
    private String highscores = "Highscores"; 
    private boolean startPressed = false; 
    public JButton bStart = new JButton(start); 
    public JButton bScore = new JButton(highscores); 
    // ---------------------------------------------------- 

    public boolean isStartPressed() { 
     return startPressed; 
    } 

    public void setStartPressed(boolean startPressed) { 
     this.startPressed = startPressed; 
    } 

    public int getWidthMenue() { 
     return widthMenue; 
    } 

    public void setwidthMenue(int widthMenue) { 
     this.widthMenue = widthMenue; 
    } 

    public int getheightMenue() { 
     return heightMenue; 
    } 

    public void setheightMenue(int heightMenue) { 
     this.heightMenue = heightMenue; 
    } 

    public void loadMenue(JFrame j) { 
     JPanel menue = new JPanel(); 

     LayoutManager border = new BorderLayout(); 
     menue.setLayout(border); 
     menue.setBackground(Color.black); 

     bStart.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       setStartPressed(true); 
      } 
     }); 

     menue.add(bStart, BorderLayout.LINE_START); 
     menue.add(bScore, BorderLayout.LINE_END); 

     j.getContentPane().add(menue); 
    } 
} 

嗨我有一個問題,變量startPressed似乎被忽略。如果啓動按鈕被按下的變量startPressed設置爲true,但如果語句這個while循環不爲新值反應:按鈕動作後不可見變化

 while (running) { 
      if (m.isStartPressed()) { 
       System.out.println("test"); 
      } 

     } 

如果我添加System.out.printlnThread.sleep環路內,則if語句承認價值並給我輸出。

我想也許在編程結構中存在一個主要問題,或者Java太慢了。有任何想法嗎? 謝謝!

+0

爲什麼在上面的發佈代碼中顯示兩個遊戲類? – 2013-03-22 22:23:30

+0

請參見:[Loop沒有看到沒有打印語句的更改值](https://stackoverflow.com/questions/25425130/loop-doesnt-see-changed-value-without-a-print-statement) – Boann 2015-01-17 22:51:26

回答

1

您的主要問題是您的startPressed變量不會變爲易失性,因此在一個線程中更改它可能不會反映到另一個線程中。改變這一點,你會看到你的啓動按鈕適當地改變這個變量:

private volatile boolean startPressed = false; 

你的遊戲循環不應該是因爲它在Swing線程規則背道而馳。爲什麼不使用Swing Timer(我的偏好),或者如果你需要自己的roll-your own loop,那麼在後臺線程中這樣做。還要考慮讓startPressed成爲一個「綁定」變量,當變量告訴任何屬性更改偵聽器時,它的狀態已被更改。這會比不斷投票其價值更好。

另一種評論:你的代碼過度使用靜態,如果你擺脫了大部分的靜態修飾符,組織會好得多。

+0

Thanks for幫助,我會嘗試 – nickfaker 2013-03-23 10:56:43

0

當你做這樣的事情

while(running) { 
    ... 
} 

這意味着,這個循環結束執行一遍又一遍,沒有什麼可以阻止它,因爲你的電腦可以做到這一點它就會以最快的速度執行。因此,它傾向於阻止其他一切。所以是的,你的程序結構是問題。嘗試在單獨的線程中執行你的遊戲循環。此線程可以隨時更新您的視圖。也許想不時暫停這個線程或者以某種方式安排它,所以你的觀點只是每秒更新一次(或任何你想要的)。

+0

感謝您的幫助,我會嘗試它 – nickfaker 2013-03-23 11:11:03