Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //imports here
- public class Game extends Canvas implements Runnable {
- public static JFrame frame;
- public static Screen screen;
- public static Level level;
- public static KeyHandel key;
- public static Entity player;
- public static final String gameTitle = "TITLE";
- public static final String gameThreadName = "Game Thread";
- public static final int windowWidth = 800;
- public static final int windowHeight = windowWidth / 16 * 9;
- public static final int windowScale = 2;
- public static final int imageWidth = windowWidth * 4;
- public static final int imageHeight = windowHeight * 4;
- public static final int TILE_SIZE = 128;
- public static final int BIT_TILE_SIZE_MASK = 7; // 2^7 = 128
- private BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
- private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
- private boolean running = false;
- public Game() {
- frame = new JFrame();
- key = new KeyHandel();
- screen = new Screen(imageWidth, imageHeight);
- level = new Map_One("/maps/map_one.png");
- player = new EntityPlayer(Sprite.player, key).setX(700).setY(500);
- level.addEntity(player);
- init();
- }
- private void init() {
- Dimension size = new Dimension(windowWidth * windowScale, windowHeight * windowScale);
- setPreferredSize(size);
- addKeyListener(key);
- }
- public static void main(String[] args) {
- Game game = new Game();
- game.frame.setResizable(false);
- game.frame.setTitle(gameTitle);
- game.frame.add(game);
- game.frame.pack();
- game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- game.frame.setLocationRelativeTo(null);
- game.frame.setVisible(true);
- game.requestFocus();
- game.start();
- }
- private void start() {
- running = true;
- new Thread(this, gameThreadName).start();
- }
- private void stop() {
- running = false;
- }
- @Override
- public void run() {
- long lastTime = System.nanoTime();
- long timer = System.currentTimeMillis();
- final double ns = 1_000_000_000.0 / 60.0;
- double delta = 0;
- int frames = 0;
- int updates = 0;
- while (running) {
- long now = System.nanoTime();
- delta += (now - lastTime) / ns;
- lastTime = now;
- while (delta >= 1) {
- tick();
- updates++;
- delta--;
- }
- render();
- frames++;
- if (System.currentTimeMillis() - timer >= 1000) {
- timer += 1000;
- frame.setTitle(gameTitle + " | " + updates + " ups, " + frames + " fps");
- frames = 0;
- updates = 0;
- }
- }
- stop();
- }
- private void render() {
- BufferStrategy bs = getBufferStrategy();
- if (bs == null) {
- createBufferStrategy(3);
- return;
- }
- int xOffset = player.getX() - screen.getWidth() / 2;
- int yOffset = player.getY() - screen.getHeight() / 2;
- level.render(xOffset, yOffset, screen); // renders the level
- for (int i = 0; i < pixels.length; i++) {
- pixels[i] = screen.getPixelAt(i); // saves the pixels of the screen in the image pixels array
- }
- Graphics g = bs.getDrawGraphics();
- g.drawImage(image, 0, 0, getWidth(), getHeight(), null); // stretchs the image to the screen (the actual image is bigger than the window so it fits more tiles on the screen)
- g.dispose();
- bs.show();
- }
- private void tick() {
- key.update();
- level.update(); //updates the level ( updates entities positions)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement