Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.anyspieceofinter.unnamedgameproject;
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Dimension;
- import javax.swing.JFrame;
- import java.awt.image.BufferedImage;
- import java.awt.image.DataBufferInt;
- import java.awt.image.BufferStrategy;
- import net.anyspieceofinter.unnamedgameproject.input.Keyboard;
- import net.anyspieceofinter.unnamedgameproject.graphics.Screen;
- public class Game extends Canvas implements Runnable {
- public static int aspectratioWidth = 7;
- public static int aspectratioHeight = 5;
- public static int scale = 2;
- public static int width = 280;
- public static int height = width / aspectratioWidth * aspectratioHeight;
- //0 - 29,99 | 30,000 pixels
- public static int bufferLayers = 3;
- private JFrame frame;
- private Keyboard key;
- 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 Game() {
- Dimension size = new Dimension(width*scale, height*scale);
- setPreferredSize(size);
- screen = new Screen(width, height);
- frame = new JFrame();
- key = new Keyboard();
- addKeyListener(key);
- }
- private Thread thread;
- public synchronized void start() {
- running = true;
- thread = new Thread(this, "Game");
- thread.start();
- }
- public synchronized void stop() {
- running = false;
- try {
- thread.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void run() {
- long lastTime = System.nanoTime();
- long timer = System.currentTimeMillis();
- final double nanoSeconds = 1000000000.0 / 30.0;
- double delta = 0;
- int updates = 0;
- int frames = 0;
- while(running) {
- long now = System.nanoTime();
- delta += (now - lastTime) / nanoSeconds;
- lastTime = now;
- while (delta >= 1) {
- update();
- updates++;
- delta--;
- }
- render();
- frames++;
- if(System.currentTimeMillis() - timer > 1000) {
- timer += 1000;
- frame.setTitle("unname game project [ups: " + updates + ", fps: " + frames + "]");
- updates = 0;
- frames = 0;
- }
- }
- stop();
- }
- public void update() {
- key.update();
- }
- public void render() {
- BufferStrategy buffer = getBufferStrategy();
- if(buffer == null) {
- createBufferStrategy(Game.bufferLayers);
- return;
- }
- screen.clear();
- screen.render();
- for(int i = 0; i < pixels.length; i++) {
- pixels[i] = screen.pixels[i];
- }
- Graphics g = buffer.getDrawGraphics();
- g.setColor(Color.BLACK);
- g.fillRect(0, 0, getWidth(), getHeight());
- g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
- g.dispose();
- buffer.show();
- }
- public static void main(String[] ags) {
- Game game = new Game();
- game.frame.setResizable(false);
- game.frame.setTitle("unnamed game project");
- 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