klasscho

Game class

May 26th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | None | 0 0
  1. package com.packag;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.event.KeyListener;
  7. import java.awt.image.BufferedImage;
  8.  
  9. public class Game extends JPanel implements KeyListener, Runnable {
  10.  
  11.     public static final int WIDTH = 400;
  12.     public static final int HEIGHT = 630;
  13.     public static final Font main = new Font("Bebas Neue Regular", Font.PLAIN, 28  );
  14.     private Thread game;
  15.     private boolean runnig;
  16.     private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB );
  17.     private GameBoard board;
  18.  
  19.     private long starTime;
  20.     private long elapsed;
  21.     private boolean set;
  22.  
  23.     public Game(){
  24.        setFocusable(true);
  25.        setPreferredSize(new Dimension(WIDTH, HEIGHT - GameBoard.BOARD_HEIGHT));
  26.        addKeyListener(this);
  27.  
  28.        board = new GameBoard(WIDTH/2 - GameBoard.BOARD_WIDTH/2, HEIGHT);
  29.     }
  30.  
  31.     private void update(){
  32.         board.update();
  33.         Keyboard.update();
  34.  
  35.     }
  36.  
  37.     private void render(){
  38.         Graphics2D g = (Graphics2D) image.getGraphics();
  39.         g.setColor(Color.white);
  40.         g.fillRect(0, 0, WIDTH, HEIGHT);
  41.         //render board
  42.         board.render(g);
  43.         g.dispose();
  44.  
  45.         Graphics2D  g2d = (Graphics2D) getGraphics();
  46.         g2d.drawImage(createImage(WIDTH, HEIGHT), 0, 0, null);
  47.         g2d.dispose();
  48.  
  49.  
  50.     }
  51.  
  52.     @Override
  53.     public void run() {
  54.         boolean shouldRender = false;
  55.         int fps = 0, updates = 0;
  56.         long fpsTimer = System.currentTimeMillis();
  57.         double nsPerUpdate = 1000000000.0/60;
  58.  
  59.         //last update time in nanoseconds
  60.         double then = System.nanoTime();
  61.         double unprocessed = 0;
  62.  
  63.         while (runnig){
  64.             double now = System.nanoTime();
  65.             unprocessed += (now - then)/nsPerUpdate;
  66.             then = now;
  67.         }
  68.  
  69.         //update queue
  70.         while (unprocessed >= 1){
  71.             updates++;
  72.             update();
  73.             unprocessed--;
  74.             shouldRender = true;
  75.         }
  76.         //render
  77.  
  78.         if (shouldRender) {
  79.             fps++;
  80.             render();
  81.             shouldRender = false;
  82.         }
  83.         else{
  84.             try {
  85.                 Thread.sleep(1);
  86.             }catch (Exception e){
  87.                 e.printStackTrace();
  88.             }
  89.         }
  90.         //fps timer
  91.         if (System.currentTimeMillis() - fpsTimer > 1000){
  92.             System.out.printf("%d fps %d updates", fps, updates);
  93.             System.out.println();
  94.             fps = 0;
  95.             updates = 0;
  96.             fpsTimer += 1000;
  97.         }
  98.  
  99.  
  100.     }
  101.  
  102.     public synchronized void start (){
  103.         if(runnig) return;
  104.         runnig = true;
  105.         game = new Thread(this, "game");
  106.         game.start();
  107.     }
  108.  
  109.     public synchronized void stop(){
  110.         if(!runnig) return;
  111.         runnig = false;
  112.         System.exit(0);
  113.     }
  114.     @Override
  115.     public void keyTyped(KeyEvent e) {
  116.  
  117.     }
  118.  
  119.     @Override
  120.     public void keyPressed(KeyEvent e) {
  121.         Keyboard.keyPressed(e);
  122.     }
  123.  
  124.     @Override
  125.     public void keyReleased(KeyEvent e) {
  126.         Keyboard.keyReleased(e);
  127.     }
  128.  
  129. }
Add Comment
Please, Sign In to add comment