开始游戏

时间:2016-10-17 02:57:54

标签: java

我开始游戏,但它有问题。它实际上没有显示任何内容,也没有给我一个错误。它只是运行它而不做任何其他事情。

以下是我的代码:

CLASS

    package Game;

    import java.awt.Canvas;
    import java.awt.Dimension;

    import javax.swing.JFrame;

    public class Window extends Canvas{

        private static final long serialVersionUID = -2408406005333728354L;

        public Window(int height, int width, String title, Game game){
              JFrame tit = new JFrame(title); 

              tit.setPreferredSize(new Dimension(width, height));
              tit.setMaximumSize(new Dimension(width, height));
              tit.setMinimumSize(new Dimension(width, height));

              tit.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //It make the game close up to the end without bugs
              tit.setResizable(false);   //not able to resize
              tit.setLocationRelativeTo(null);   //put the frame in the middle of the screen, originaly it starts up left
              tit.add(game);    // adding pur game class to the frame
              tit.setVisible(true);  //to make it visible
              game.start();  // makes the game start
        }


    }

这是MAIN

    package Game;
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.image.BufferStrategy;

    public class Game extends Canvas implements Runnable{

        private static final long serialVersionUID 

= 1550691097823471818L;

    public static final int WIDTH = 640, HEIGHT = WIDTH / 12*9;
    private Thread thread;
    private boolean running = false;
    public Game(){
        new Window(WIDTH, HEIGHT, "TITLE OF THAE GAME", this);
    }

    public synchronized void start(){
     thread = new Thread(this);
     thread.start();
     running = true;
    }
    public synchronized void stop(){
    try{
        thread.join();
        running = false;
    }catch(Exception e){
        e.printStackTrace();
    }
        }

    public void run(){
        long lastTime = System.nanoTime();
        double amoutOfTicks = 60.0;
        double ns = 1000000000.0/amoutOfTicks;
        double delta = 0;
        long timer = System.currentTimeMillis();
        int frames = 0;
        while(running){
            long now = System.nanoTime();
            delta += (now-lastTime) / ns;                      
            lastTime = now;
            while (delta>=1){
                tick();
                delta--;
            }
        if(running)
            render();
        frames++;

        if(System.currentTimeMillis() - timer > 1000){
            timer += 1000;
            System.out.println("FPS: "+ frames);
            frames = 0;
        }
        }
        stop();
    }

    private void tick(){

    }

    private void render(){
        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null){
            this.createBufferStrategy(3);
            return;
        }
        Graphics g = bs.getDrawGraphics();

        g.setColor(Color.black);
        g.fillRect(0, 0, WIDTH, HEIGHT);

        g.dispose();
        bs.show();
    }
public static void main(String[] args){

}
}

实际上有一些我不理解的东西,它是serialVersionUID。我不知道这些东西是什么。我刚从这个来源复制了它:https://www.youtube.com/watch?v=1gir2R7G9ws&index=4&list=PL3j6S0UUuVMRSD1_wCzr2-Yd8h48rUvhs&t=647s

它也是我用来构建这些代码(复制)的源。

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

你在main方法上什么也没写,所以程序什么都不做。你应该新建一个Game对象和一个Window对象,然后在main方法中调用window方法。