Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. package firstGame;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Color;
  5. import java.awt.Graphics;
  6. import java.awt.image.BufferStrategy;
  7.  
  8. public class Game extends Canvas implements Runnable {
  9.  
  10.     private static final long serialVersionUID = -4449141158043388716L;
  11.    
  12.     private static final int WIDTH = 700, HEIGHT = WIDTH / 12 * 9;
  13.    
  14.     private Thread thread;
  15.     private boolean running = false;
  16.    
  17.     public Game() {
  18.         new Window(WIDTH, HEIGHT, "Jump", this);
  19.     }
  20.    
  21.     public synchronized void start() {
  22.         thread = new Thread(this);
  23.         thread.start();
  24.         running = true;
  25.     }
  26.    
  27.     public synchronized void stop() {
  28.         try {
  29.             thread.join();
  30.             running = false;
  31.         }catch(Exception e) {
  32.             e.printStackTrace();
  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.    
  69.     private void render() {
  70.         BufferStrategy bs = this.getBufferStrategy();
  71.         if(bs == null) {
  72.             this.createBufferStrategy(3);
  73.             return;
  74.         }
  75.         Graphics g = bs.getDrawGraphics();
  76.        
  77.         g.setColor(Color.black);
  78.         g.fillRect(0, 0, WIDTH, HEIGHT);
  79.        
  80.         g.dispose();
  81.         bs.show();
  82.     }
  83.  
  84.     public static void main(String[] args) {
  85.         new Game();
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement