Advertisement
Guest User

Java2D_Game

a guest
Nov 11th, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. package j2dgame.game;
  2.  
  3. import j2dgame.gfx.Colors;
  4. import j2dgame.gfx.Screen;
  5. import j2dgame.gfx.SpriteSheet;
  6.  
  7. import java.awt.BorderLayout;
  8. import java.awt.Canvas;
  9. import java.awt.Dimension;
  10. import java.awt.Graphics;
  11. import java.awt.image.BufferStrategy;
  12. import java.awt.image.BufferedImage;
  13. import java.awt.image.DataBufferInt;
  14.  
  15. import javax.swing.JFrame;
  16.  
  17. public class Game extends Canvas implements Runnable {
  18.  
  19. private static final long serialVersionUID = 1L;
  20.  
  21. public static final int WIDTH = 160;
  22. public static final int HEIGHT = WIDTH / 12 * 9;
  23. public static final int SCALE = 5;
  24. public static final String NAME = "Game";
  25.  
  26. private JFrame frame;
  27.  
  28. public boolean running = false;
  29. public int tickCount = 0;
  30.  
  31. private BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
  32. BufferedImage.TYPE_INT_RGB);
  33. private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer())
  34. .getData();
  35. private int[] colors = new int[6 * 6 * 6];
  36.  
  37. private Screen screen;
  38. public InputHandler input;
  39.  
  40. public Game() {
  41. setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
  42. setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
  43. setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
  44.  
  45. frame = new JFrame(NAME);
  46.  
  47. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  48. frame.setLayout(new BorderLayout());
  49.  
  50. frame.add(this, BorderLayout.CENTER);
  51. frame.pack();
  52.  
  53. frame.setResizable(false);
  54. frame.setLocationRelativeTo(null);
  55. frame.setVisible(true);
  56. }
  57.  
  58. public void init() {
  59. int index = 0;
  60. for (int r = 0; r < 6; r++) {
  61. for (int g = 0; g < 6; g++) {
  62. for (int b = 0; b < 6; b++) {
  63. int rr = (r * 255 / 5);
  64. int gg = (g * 255 / 5);
  65. int bb = (b * 255 / 5);
  66.  
  67. colors[index++] = rr << 16 | gg << 8 | bb;
  68. }
  69. }
  70. }
  71.  
  72. screen = new Screen(WIDTH, HEIGHT, new SpriteSheet("/sprite_sheet.png"));
  73. input = new InputHandler(this);
  74. }
  75.  
  76. public synchronized void start() {
  77. running = true;
  78. new Thread(this).start();
  79.  
  80. }
  81.  
  82. public synchronized void stop() {
  83. running = false;
  84.  
  85. }
  86.  
  87. public void run() {
  88. long lastTime = System.nanoTime();
  89. double nsPerTick = 1000000000D / 60D;
  90.  
  91. int ticks = 0;
  92. int frames = 0;
  93.  
  94. long lastTimer = System.currentTimeMillis();
  95. double delta = 0;
  96.  
  97. init();
  98.  
  99. while (running) {
  100. long now = System.nanoTime();
  101. delta += (now - lastTime) / nsPerTick;
  102. lastTime = now;
  103. boolean shouldRender = true;
  104.  
  105. while (delta >= 1) {
  106. ticks++;
  107. tick();
  108. delta -= 1;
  109. shouldRender = true;
  110. }
  111. try {
  112. Thread.sleep(2);
  113. } catch (InterruptedException e) {
  114. e.printStackTrace();
  115. }
  116. if (shouldRender) {
  117. frames++;
  118. render();
  119. }
  120.  
  121. if (System.currentTimeMillis() - lastTimer >= 1000) {
  122. lastTimer += 1000;
  123. System.out.println(ticks + " ticks, " + frames + " frames");
  124. frames = 0;
  125. ticks = 0;
  126. }
  127. }
  128.  
  129. }
  130.  
  131. public void tick() {
  132. tickCount++;
  133. if (input.up.isPressed()) {
  134. screen.yOffset--;
  135. }
  136. if (input.down.isPressed()) {
  137. screen.yOffset++;
  138. }
  139. if (input.left.isPressed()) {
  140. screen.xOffset--;
  141. }
  142. if (input.right.isPressed()) {
  143. screen.xOffset++;
  144. }
  145.  
  146. }
  147.  
  148. public void render() {
  149. BufferStrategy bs = getBufferStrategy();
  150. if (bs == null) {
  151. createBufferStrategy(3);
  152. return;
  153. }
  154.  
  155. for (int y = 0; y < 32; y++) {
  156. for (int x = 0; x < 32; x++) {
  157. screen.render(x << 3, y << 3, 0, Colors.get(555, 500, 050, 005));
  158. }
  159. }
  160. for (int y = 0; y < screen.height; y++) {
  161. for (int x = 0; x < screen.width; x++) {
  162. int colorCode = screen.pixels[x + y * screen.width];
  163. if (colorCode < 255)
  164. pixels[x + y * WIDTH] = colors[colorCode];
  165. }
  166. }
  167. Graphics g = bs.getDrawGraphics();
  168. g.drawRect(0, 0, getWidth(), getHeight());
  169. g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  170. g.dispose();
  171. bs.show();
  172.  
  173. }
  174.  
  175. public static void main(String[] args) {
  176. new Game().start();
  177. }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement