Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tk.sketchistgames.Automa;
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.image.BufferStrategy;
- import java.awt.image.BufferedImage;
- import java.awt.image.DataBufferInt;
- import javax.swing.JFrame;
- import tk.sketchistgames.graphics.Screen;
- import tk.sketchistgames.input.Keyboard;
- import tk.sketchistgames.level.Level;
- import tk.sketchistgames.level.RandomLevel;
- public class Automa extends Canvas implements Runnable {
- private static final long serialVersionUID = 2664234447792702880L;
- public static int width = 300;
- public static int height = width / 16 * 9; // 168.5
- public static int scale = 3;
- private Thread GameThread;
- private JFrame frame = new JFrame();
- private Keyboard key;
- private Level level;
- private boolean running = false;
- private Screen screen;
- private BufferedImage image = new BufferedImage(width, height,
- BufferedImage.TYPE_INT_RGB);
- private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
- public Automa() {
- Dimension size = new Dimension(width * scale, height * scale);
- setPreferredSize(size);
- screen = new Screen(width, height);
- frame = new JFrame();
- key = new Keyboard();
- level = new RandomLevel(32, 32);
- addKeyListener(key);
- }
- public synchronized void start() {
- running = true;
- GameThread = new Thread(this, "Display");
- GameThread.start();
- }
- public synchronized void stop() {
- running = false;
- try {
- GameThread.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void run() {
- long lastTime = System.nanoTime();
- long timer = System.currentTimeMillis();
- final double ns = 1000000000.0 / 60.0;
- double delta = 0;
- int frames = 0;
- int updates = 0;
- requestFocus();
- while (running) {
- long now = System.nanoTime();
- delta += (now - lastTime) / ns;
- lastTime = now;
- while(delta >= 1){
- update();
- updates++;
- delta--;
- }
- this.render();
- frames++;
- if(System.currentTimeMillis() - timer > 1000){
- timer += 1000;
- frame.setTitle("Automa | " + "Chunk Updates: " + updates + ", " + "FPS: " + frames);
- updates = 0;
- frames = 0;
- }
- }
- stop();
- }
- int x=0,y=0;
- public void update() {
- key.update();
- if(key.up) y++;
- if(key.down) y--;
- if(key.left) x++;
- if(key.right) x--;
- }
- public void render() {
- BufferStrategy bs = getBufferStrategy();
- if (bs == null) {
- createBufferStrategy(3);
- return;
- }
- screen.clear();
- level.render(x, y, screen);
- for(int i = 0; i < pixels.length; i++){
- pixels[i] = screen.pixels[i];
- }
- Graphics g = bs.getDrawGraphics();
- /*
- * Graphics go here before g.dispose
- */
- g.setColor(Color.WHITE);
- g.fillRect(0, 0, getWidth(), getHeight());
- g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
- g.dispose();
- bs.show();
- }
- public static void main(String[] args) {
- Automa game = new Automa();
- game.frame.setResizable(false);
- game.frame.setTitle("Automa"); //TODO COME UP WITH BETTER NAME
- game.frame.add(game);
- game.frame.pack();
- game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- game.frame.setLocationRelativeTo(null);
- game.frame.setVisible(true);
- game.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement