Advertisement
Guest User

Untitled

a guest
May 11th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. //imports here
  2.  
  3. public class Game extends Canvas implements Runnable {
  4.  
  5. public static JFrame frame;
  6. public static Screen screen;
  7. public static Level level;
  8. public static KeyHandel key;
  9. public static Entity player;
  10.  
  11. public static final String gameTitle = "TITLE";
  12. public static final String gameThreadName = "Game Thread";
  13.  
  14. public static final int windowWidth = 800;
  15. public static final int windowHeight = windowWidth / 16 * 9;
  16. public static final int windowScale = 2;
  17. public static final int imageWidth = windowWidth * 4;
  18. public static final int imageHeight = windowHeight * 4;
  19.  
  20. public static final int TILE_SIZE = 128;
  21. public static final int BIT_TILE_SIZE_MASK = 7; // 2^7 = 128
  22.  
  23. private BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
  24.  
  25. private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
  26.  
  27. private boolean running = false;
  28.  
  29. public Game() {
  30. frame = new JFrame();
  31. key = new KeyHandel();
  32. screen = new Screen(imageWidth, imageHeight);
  33. level = new Map_One("/maps/map_one.png");
  34. player = new EntityPlayer(Sprite.player, key).setX(700).setY(500);
  35. level.addEntity(player);
  36.  
  37. init();
  38. }
  39.  
  40. private void init() {
  41. Dimension size = new Dimension(windowWidth * windowScale, windowHeight * windowScale);
  42. setPreferredSize(size);
  43. addKeyListener(key);
  44. }
  45.  
  46. public static void main(String[] args) {
  47. Game game = new Game();
  48. game.frame.setResizable(false);
  49. game.frame.setTitle(gameTitle);
  50.  
  51. game.frame.add(game);
  52. game.frame.pack();
  53. game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54. game.frame.setLocationRelativeTo(null);
  55. game.frame.setVisible(true);
  56. game.requestFocus();
  57.  
  58. game.start();
  59. }
  60.  
  61. private void start() {
  62. running = true;
  63. new Thread(this, gameThreadName).start();
  64. }
  65.  
  66. private void stop() {
  67. running = false;
  68. }
  69.  
  70. @Override
  71. public void run() {
  72. long lastTime = System.nanoTime();
  73. long timer = System.currentTimeMillis();
  74. final double ns = 1_000_000_000.0 / 60.0;
  75. double delta = 0;
  76. int frames = 0;
  77. int updates = 0;
  78.  
  79. while (running) {
  80. long now = System.nanoTime();
  81. delta += (now - lastTime) / ns;
  82. lastTime = now;
  83. while (delta >= 1) {
  84. tick();
  85. updates++;
  86. delta--;
  87. }
  88.  
  89. render();
  90. frames++;
  91.  
  92. if (System.currentTimeMillis() - timer >= 1000) {
  93. timer += 1000;
  94. frame.setTitle(gameTitle + " | " + updates + " ups, " + frames + " fps");
  95. frames = 0;
  96. updates = 0;
  97. }
  98. }
  99. stop();
  100. }
  101.  
  102. private void render() {
  103. BufferStrategy bs = getBufferStrategy();
  104. if (bs == null) {
  105. createBufferStrategy(3);
  106. return;
  107. }
  108.  
  109. int xOffset = player.getX() - screen.getWidth() / 2;
  110. int yOffset = player.getY() - screen.getHeight() / 2;
  111.  
  112. level.render(xOffset, yOffset, screen); // renders the level
  113.  
  114. for (int i = 0; i < pixels.length; i++) {
  115. pixels[i] = screen.getPixelAt(i); // saves the pixels of the screen in the image pixels array
  116. }
  117.  
  118. Graphics g = bs.getDrawGraphics();
  119.  
  120. 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)
  121.  
  122. g.dispose();
  123. bs.show();
  124. }
  125.  
  126. private void tick() {
  127.  
  128. key.update();
  129. level.update(); //updates the level ( updates entities positions)
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement