Advertisement
Guest User

Game.class

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