Advertisement
Guest User

Basecode for my LD entry

a guest
Dec 14th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Canvas;
  3. import java.awt.Graphics;
  4. import java.awt.event.KeyEvent;
  5. import java.awt.event.KeyListener;
  6. import java.awt.image.BufferStrategy;
  7. import java.awt.image.BufferedImage;
  8. import java.awt.image.DataBufferInt;
  9. import java.util.Random;
  10.  
  11. import javax.swing.JApplet;
  12.  
  13. public class Main extends JApplet implements Runnable {
  14.     private static final long serialVersionUID = 1L;
  15.     public static final int GWIDTH = 320;
  16.     public static final int GHEIGHT = 180;
  17.    
  18.     boolean running = false;
  19.     Thread thread;
  20.     Canvas canvas;
  21.     Input input;
  22.     Random rand = new Random();
  23.    
  24.     BufferedImage image = new BufferedImage(GWIDTH, GHEIGHT, BufferedImage.TYPE_INT_RGB);
  25.     int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
  26.    
  27.     public void initGame() {
  28.         input = new Input();
  29.         canvas.addKeyListener(input);
  30.     }
  31.    
  32.     public void update() {
  33.     }
  34.    
  35.     public void draw() {
  36.         BufferStrategy bs = canvas.getBufferStrategy();
  37.         if (bs == null) {
  38.             canvas.createBufferStrategy(3);
  39.             return;
  40.         }
  41.        
  42.         clear();
  43.         // Render stuff
  44.        
  45.         Graphics g = bs.getDrawGraphics();
  46.         g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  47.         g.dispose();
  48.         bs.show();
  49.     }
  50.    
  51.     public void clear() {
  52.         for (int i = 0; i < pixels.length; i++) {
  53.             pixels[i] = 0;
  54.         }
  55.     }
  56.    
  57.     public void run() {
  58.         long lastTime = System.currentTimeMillis();
  59.         final double ns = 1000.0 / 60.0;
  60.         double delta = 0;
  61.         long nowTime;
  62.         int frames = 0, updates = 0;
  63.         long timer = System.currentTimeMillis();
  64.         while (running) {
  65.             nowTime = System.currentTimeMillis();
  66.             delta += (nowTime - lastTime) / ns;
  67.             lastTime = nowTime;
  68.             while (delta >= 1) {
  69.                 update();
  70.                 updates++;
  71.                 delta--;
  72.             }
  73.             draw();
  74.             frames++;
  75.             if (System.currentTimeMillis() - timer > 1000) {
  76.                 timer = System.currentTimeMillis();
  77.                 System.out.println("FPS: " + frames + ", UPS: " + updates);
  78.                 frames = 0;
  79.                 updates = 0;
  80.             }
  81.         }
  82.     }
  83.    
  84.     /**
  85.      * Applet stuff
  86.      */
  87.     public void init() {
  88.         canvas = new Canvas();
  89.         setLayout(new BorderLayout());
  90.         add(canvas);
  91.         this.setSize(640, 360);
  92.     }
  93.    
  94.     public synchronized void start() {
  95.         running = true;
  96.         thread = new Thread(this);
  97.         initGame();
  98.         thread.start();
  99.     }
  100.    
  101.     public synchronized void stop() {
  102.         running = false;
  103.         try {
  104.             thread.join();
  105.         } catch(Exception ex) {
  106.             ex.printStackTrace();
  107.             System.exit(0);
  108.         }
  109.     }
  110.    
  111.     class Input implements KeyListener {
  112.         public boolean[] key = new boolean[65536];
  113.        
  114.         public void keyPressed(KeyEvent e) {
  115.             key[e.getKeyCode()] = true;
  116.             if (e.getKeyCode() >= 65536) {
  117.                 System.out.println("Caught a keypress with a keycode >=65536!");
  118.             }
  119.         }
  120.        
  121.         public void keyReleased(KeyEvent e) {
  122.             key[e.getKeyCode()] = false;
  123.             if (e.getKeyCode() >= 65536) {
  124.                 System.out.println("Caught a keypress with a keycode >=65536!");
  125.             }
  126.         }
  127.        
  128.         public void keyTyped(KeyEvent e) {}
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement