Advertisement
ponysquad

game.java

Aug 14th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. package aarons.project.game;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.awt.image.BufferStrategy;
  8. import java.awt.image.BufferedImage;
  9. import java.awt.image.DataBufferInt;
  10.  
  11. import javax.swing.JFrame;
  12.  
  13. import aarons.project.game.graphics.Screen;
  14. import aarons.project.game.input.Keyboard;
  15.  
  16. public class Game extends Canvas implements Runnable{
  17. private static final long serialVersionUID = 1L;
  18.  
  19. public static int width = 300;
  20. public static int height = width / 16 * 9;
  21. public static int scale = 3;
  22. public static String title = "Rain";
  23.  
  24.  
  25. private Thread thread;
  26. private JFrame frame;
  27. private Keyboard key;
  28. private boolean running = false;
  29.  
  30. private Screen screen;
  31.  
  32. private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  33. private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
  34.  
  35. public Game() {
  36. Dimension size = new Dimension(width * scale, height * scale);
  37. setPreferredSize(size);
  38.  
  39. screen = new Screen(width, height);
  40. frame = new JFrame();
  41. key = new Keyboard();
  42.  
  43. addKeyListener(key);
  44. }
  45.  
  46. public synchronized void start () {
  47. running = true;
  48. thread = new Thread(this, "Display");
  49. thread.start();
  50. }
  51.  
  52. public synchronized void stop () {
  53. running = false;
  54. try {
  55. thread.join();
  56. } catch (InterruptedException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60.  
  61. public void run() {
  62. long lastTime = System.nanoTime();
  63. long timer = System.currentTimeMillis();
  64. final double ns = 1000000000.0 / 60.0;
  65. double delta = 0;
  66. int frames = 0;
  67. int updates = 0;
  68. requestFocus();
  69. while (running) {
  70. long now = System.nanoTime();
  71. delta += (now-lastTime) / ns;
  72. lastTime = now;
  73. while (delta >= 1) {
  74. update();
  75. updates++;
  76. delta--;
  77. }
  78. render();
  79. frames++;
  80.  
  81. if (System.currentTimeMillis() - timer > 1000) {
  82. timer +=1000;
  83. System.out.println(updates + "ups, " + frames + "fps");
  84. frame.setTitle(title + " | " + updates + "ups, " + frames + "fps");
  85. updates = 0;
  86. frames = 0;
  87. }
  88. }
  89. stop();
  90. }
  91.  
  92. int x = 0, y = 0;
  93.  
  94. public void update() {
  95. key.update();
  96. if (key.up) y--;
  97. if (key.down) y++;
  98. if (key.left) x--;
  99. if(key.right) x++;
  100. }
  101.  
  102. public void render() {
  103. BufferStrategy bs = getBufferStrategy();
  104. if (bs == null) {
  105. createBufferStrategy(3);
  106. return;
  107. }
  108.  
  109. screen.clear();
  110. screen.render(x, y);
  111.  
  112. for (int i = 0; i < pixels.length; i++) {
  113. pixels[i] = screen.pixels[i];
  114. }
  115.  
  116. Graphics g = bs.getDrawGraphics();
  117. g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  118. g.dispose();
  119. bs.show();
  120. }
  121.  
  122. public static void main (String[] args) {
  123. Game game = new Game();
  124. game.frame.setResizable(false);
  125. game.frame.setTitle(Game.title);
  126. game.frame.add(game);
  127. game.frame.pack();
  128. game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  129. game.frame.setLocationRelativeTo(null);
  130. game.frame.setVisible(true);
  131.  
  132. game.start();
  133. }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement