Advertisement
Guest User

asd

a guest
Mar 24th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. package com.game.main;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Graphics;
  5. import java.awt.image.BufferStrategy;
  6.  
  7.  
  8. public class Game extends Canvas implements Runnable {
  9.    
  10.     private static final long serialVersionUID = -4594556074679255172L;
  11.    
  12.     public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;
  13.     private boolean running = false;
  14.    
  15.     private Thread thread;
  16.    
  17.     public Game(){
  18.         new Window(WIDTH, HEIGHT, "Game!", this);
  19.     }
  20.    
  21.     public synchronized void start() {
  22.         thread = new Thread(this);
  23.         thread.start();
  24.         running = true;
  25.     }
  26.     public synchronized void stop() {
  27.         try{
  28.             thread.join();
  29.             running = false;
  30.         }catch(Exception e){
  31.             e.printStackTrace();
  32.            
  33.         }
  34.     }
  35.    
  36.    
  37.     public void run() {
  38.         long lastTime = System.nanoTime();
  39.         double amountOfTicks = 60.0;
  40.         double ns = 1000000000 / amountOfTicks;
  41.         double delta = 0;
  42.         long timer = System.currentTimeMillis();
  43.         int frames = 0;
  44.         while(running) {
  45.                     long now = System.nanoTime();
  46.                     delta += (now - lastTime) / ns;
  47.                     lastTime = now;
  48.                     while(delta >=1){
  49.                         tick();
  50.                         delta--;
  51.                     }
  52.                     if(running)
  53.                         render();
  54.                     frames++;
  55.                            
  56.                     if(System.currentTimeMillis() - timer > 1000){
  57.                         timer += 1000;
  58.                         System.out.println("FPS: "+ frames);
  59.                         frames = 0;
  60.                     }
  61.                 }
  62.                 stop();
  63.         }
  64.    
  65.     private void tick() {
  66.        
  67.     }
  68.     private void render() {
  69.         BufferStrategy bs = this.getBufferStrategy();
  70.         if(bs == null) {
  71.             this.createBufferStrategy(3);
  72.             return;
  73.         }
  74.        
  75.         Graphics g = bs.getDrawGraphics();
  76.        
  77.         g.dispose();
  78.         bs.show();
  79.     }
  80.    
  81.     public static void main(String args[]){
  82.         new Game();
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement