Advertisement
LuqDude

Game.java

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