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 循环中的 if 语句不会对新值做出反应:

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

        }

如果我在循环中添加System.out.printlnor Thread.sleep,那么 if 语句会识别该值并给我输出。

我想可能是编程结构存在主要问题,或者 Java 太慢了。有任何想法吗?谢谢!

4

2 回答 2

1

您的主要问题是您的 startPressed 变量不是易变的,因此在一个线程中更改它可能不会反映在另一个线程中。改变它,你会看到你的开始按钮适当地改变了这个变量:

private volatile boolean startPressed = false;

你的游戏循环不应该像 Swing 线程规则那样飞来飞去。为什么不使用 Swing Timer(我的偏好),或者如果您需要自己的滚动 - 您自己的循环,然后在后台线程中这样做。还可以考虑将 startPressed 设置为“绑定”变量,该变量在更改时会告诉任何属性更改侦听器其状态已更改。这比不断地轮询它的价值要好。

另一条评论:你的代码过度使用了静态,如果你摆脱了大部分静态修饰符,你的代码会更好地组织起来。

于 2013-03-22T22:21:21.613 回答
0

当你做类似的事情时

while(running) {
    ...
}

这意味着,这个循环被一遍又一遍地执行,没有什么能阻止它,它会以你的 PC 可以执行的速度执行。因此,它往往会阻止其他一切。所以是的,你的程序结构是问题所在。尝试在单独的线程中执行您的游戏循环。然后,该线程可以不时更新您的视图。也许考虑不时暂停这个线程或以某种方式安排它,所以你的视图只是每秒更新一次(或任何你想要的)。

于 2013-03-22T22:28:50.643 回答