2016-10-17 6 views
0

私はゲームを始めましたが、何か問題があります。実際には何も表示されず、どちらもエラーが表示されません。それだけでそれを実行し、何もしない。ここでの主なゲームを始めよう

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){ 

} 
} 

ある

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 
     } 


    } 

実は、私は理解していないものがあり、それはserialVersionUID次のとおりです。ここで

は私のコードです。私はこのことが何であるか分からない。私はちょうどこのソースからそれをコピーしました: https://www.youtube.com/watch?v=1gir2R7G9ws&index=4&list=PL3j6S0UUuVMRSD1_wCzr2-Yd8h48rUvhs&t=647s

私はこれらのコードを(コピーされた)ビルドに使うソースです。

ありがとうございます。

+0

あなたの 'main'メソッドは空です。それはあなたがプログラムを始めるときに実行されるものです。そこに何もない場合、プログラムは起動するとすぐに終了します。 – resueman

+0

新しいゲーム();あなたがメインで必要なものすべてです。 – eldo

答えて

1

メインメソッドには何も書き込まないので、プログラムは何も行いません。新しいゲームオブジェクトとWindowオブジェクトを作成し、mainメソッドのwindowメソッドを呼び出す必要があります。

+0

回答に有効なメインを追加することを検討してください。 – c0der

+0

ニース、そのことは解決されましたが、今はもう2つの問題があります。 – Bog

+0

申し訳ありません。すぐに入力してください。だから最初の問題は、プライベートスレッドのスレッドを言う行に、私にプライベートワードのエラーを与えることです。 2番目の問題は、実行時に問題があるということです。public static void main(String args []){ \t \t \t new Game(); \t \t \t}}。新しいGame();を追加しましたが、public static void main(String args [])行にエラーが表示されます – Bog

関連する問題