Guest User

Game.java

a guest
Mar 21st, 2013
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. package com.doamax.game.graphics;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.image.BufferStrategy;
  7. import java.awt.image.BufferedImage;
  8. import java.awt.image.DataBufferInt;
  9.  
  10. import javax.swing.JFrame;
  11.  
  12. import com.doamax.game.graphics.Screen;
  13. import com.doamax.game.input.Keyboard;
  14. import com.doamax.game.level.Level;
  15. import com.doamax.game.level.RandomLevel;
  16.  
  17. public class Game extends Canvas implements Runnable {
  18.  
  19.     private static final long serialVersionUID = 1L;
  20.     public static int width = 300;
  21.     public static int height = width / 16 * 9;
  22.     public static int scale = 3;
  23.     public static String title = "Game";
  24.    
  25.     public Thread thread;
  26.     private JFrame frame;
  27.     private Keyboard key;
  28.     private Level level;
  29.     private boolean running = false;
  30.     private Screen screen;
  31.    
  32.     private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  33.     private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
  34.    
  35.     public Game(){
  36.         Dimension size = new Dimension(width * scale, height * scale);
  37.         setPreferredSize(size);
  38.        
  39.         screen = new Screen(width, height);
  40.         frame = new JFrame();
  41.         key = new Keyboard();
  42.         addKeyListener(key);
  43.         level = new RandomLevel(64,64);
  44.        
  45.     }
  46.    
  47.     public synchronized void start(){
  48.         running = true;
  49.         thread = new Thread(this, "Display");
  50.         thread.start();
  51.     }
  52.    
  53.     public synchronized void stop(){
  54.         running = false;
  55.         try{
  56.             thread.join();
  57.         }catch (InterruptedException e){
  58.             e.printStackTrace();
  59.         }
  60.     }
  61.  
  62.     public void run() {
  63.         long lastTime = System.nanoTime();
  64.         long timer = System.currentTimeMillis();
  65.         final double ns = 1000000000.0 / 60.0;
  66.         double delta = 0;
  67.         int frames = 0;
  68.         int updates = 0;
  69.         requestFocus();
  70.         while(running == true){
  71.             long now = System.nanoTime();
  72.             delta += (now - lastTime) / ns;
  73.             lastTime = now;
  74.             while (delta >= 1){
  75.                 update();
  76.                 updates++;
  77.                 delta--;
  78.             }
  79.             render();
  80.             frames++;
  81.            
  82.             if (System.currentTimeMillis() - timer > 1000 ){
  83.                 timer += 1000;
  84.                 frame.setTitle(title + "  |  " + "UPS " + updates + "  |  " + "FPS: " + frames);
  85.                 updates = 0;
  86.                 frames = 0;
  87.             }
  88.         }
  89.         stop();
  90.     }
  91.    
  92.     int x = 0, y = 0;
  93.    
  94.     public void update() {
  95.         key.update();
  96.         if(key.up) y--;
  97.         if(key.down) y++;
  98.         if(key.left) x--;
  99.         if(key.right) x++;
  100.     }
  101.     public void render() {
  102.         BufferStrategy bs = getBufferStrategy();
  103.         if (bs == null){
  104.             createBufferStrategy(3);
  105.             return;
  106.         }
  107.        
  108.         screen.clear();
  109.         level.render(x, y, screen);
  110.        
  111.         for(int i = 0; i < pixels.length; i++){
  112.             pixels[i] = screen.pixels[i];
  113.         }
  114.        
  115.         Graphics g = bs.getDrawGraphics();
  116.         g.fillRect(0, 0, getWidth(), getHeight());
  117.         g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  118.         g.dispose();
  119.         bs.show();
  120.     }
  121.    
  122.     public static void main(String[]args){
  123.         Game game = new Game();
  124.         game.frame.setResizable(false);
  125.         game.frame.setTitle(Game.title);
  126.         game.frame.add(game);
  127.         game.frame.pack();
  128.         game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  129.         game.frame.setLocationRelativeTo(null);
  130.         game.frame.setVisible(true);
  131.        
  132.         game.start();
  133.        
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment