Advertisement
Guest User

Untitled

a guest
Aug 31st, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. import java.awt.Canvas;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Point;
  7. import java.awt.image.BufferStrategy;
  8. import java.awt.image.BufferedImage;
  9. import javax.swing.JFrame;
  10.  
  11. public class Game extends Canvas implements Runnable {
  12.    
  13.     public static final long serialVersionUID = 1L;
  14.     public static final short WH = 400, W = 400+6, H = 400+28;
  15.     public static Graphics g;
  16.     public static boolean pause = false;
  17.    
  18.     public static void main (String[] args) {
  19.        
  20.         Game game = new Game();
  21.         game.addKeyListener(new Input());
  22.         game.setFocusable(true);
  23.        
  24.         BufferedImage icon = new BufferedImage(32,32,BufferedImage.TYPE_INT_RGB);
  25.         Graphics2D g2d = icon.createGraphics();
  26.         g2d.setColor(Player.C1);
  27.         g2d.fillRect(0,0,icon.getWidth(),icon.getHeight()/2);
  28.         g2d.setColor(Player.C2);
  29.         g2d.fillRect(0,icon.getHeight()/2,icon.getWidth(),icon.getHeight()/2);
  30.        
  31.         JFrame frame = new JFrame("The Astronaut");
  32.         frame.add(game);
  33.         frame.pack();
  34.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35.         frame.setSize(W,H);
  36.         frame.setResizable(false);
  37.         frame.addKeyListener(new Input());
  38.         frame.setLocationRelativeTo(null);
  39.         frame.setIconImage(icon);
  40.         frame.setVisible(true);
  41.        
  42.         Level.load(Spawn.levelX,Spawn.levelY);
  43.         Spawn.respawn();
  44.         game.start();
  45.        
  46.     }
  47.    
  48.     public void start () {
  49.         setCursor(getToolkit().createCustomCursor(new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR),new Point(0,0),""));
  50.         new Thread(this).start();
  51.     }
  52.    
  53.     public void run () {
  54.        
  55.         long before = System.nanoTime(), now;
  56.         double unprocessed = 0;
  57.        
  58.         long counter = System.currentTimeMillis();
  59.         byte a = 0;
  60.        
  61.         while (true) {
  62.            
  63.             if (!pause) {
  64.                
  65.                 now = System.nanoTime();
  66.                 unprocessed += (now-before)*60/1_000_000_000D;
  67.                
  68.                 while (unprocessed >= 1) {
  69.                     update();
  70.                     render();
  71.                     unprocessed--;
  72.                     a++;
  73.                 }
  74.                
  75.                 before = now;
  76.                
  77.                 if (System.currentTimeMillis()-counter >= 1_000) {
  78.                     System.out.println("UPS/FPS: " + a);
  79.                     counter = System.currentTimeMillis();
  80.                     a = 0;
  81.                 }
  82.                
  83.             } else {
  84.                
  85.                 before = System.nanoTime();
  86.                 unprocessed = 0;
  87.                 a = 0;
  88.                 counter = System.currentTimeMillis();
  89.                
  90.                 Input.left = false;
  91.                 Input.right = false;
  92.                 Input.up = false;
  93.                 Input.down = false;
  94.                 Input.ctrl = false;
  95.                
  96.                 if (!Input.p){
  97.                     pause = false;
  98.                 }
  99.                
  100.             }
  101.            
  102.         }
  103.        
  104.     }
  105.    
  106.     public void update () {
  107.         if (Input.p || !hasFocus()){
  108.             pause = true;
  109.             Input.p = true;
  110.             return;
  111.         }
  112.         Level.update();
  113.     }
  114.    
  115.     public void render () {
  116.         BufferStrategy bs = getBufferStrategy();
  117.         if (bs == null) {
  118.             createBufferStrategy(3);
  119.             return;
  120.         }
  121.         g = bs.getDrawGraphics();
  122.         Level.render();
  123.         if (pause) {
  124.             g.setColor(new Color(0F,0F,0F,1/3F));
  125.             g.fillRect(0,0,Game.WH,Game.WH);
  126.             g.setFont(new Font("Calibri", Font.PLAIN, 20));
  127.             g.setColor(Color.WHITE);
  128.             g.drawString("Press P to continue",Game.WH/2,Game.WH/2);
  129.         }
  130.         g.dispose();
  131.         bs.show();
  132.     }
  133.    
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement