Advertisement
Guest User

Game.java

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