2013-03-22 91 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