Crenox

Java 2D Game Development Code

Jun 22nd, 2013
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. package ca.vanzeben.game;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Canvas;
  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. import javax.swing.JFrame;
  11.  
  12. public class MainGame extends Canvas implements Runnable
  13. {
  14. private static final long serialVersionUID = 1L;
  15.  
  16. public static final int WIDTH = 160;
  17. public static final int HEIGHT = WIDTH / 12 * 9;
  18. public static final int SCALE = 3;
  19. public static final String NAME = "Game";
  20.  
  21. private JFrame frame;
  22.  
  23. public boolean running = false;
  24. public int tickCount = 0;
  25.  
  26. private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  27. private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
  28.  
  29. public MainGame()
  30. {
  31. setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
  32. setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
  33. setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
  34.  
  35. frame = new JFrame(NAME);
  36.  
  37. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38. frame.setLayout(new BorderLayout());
  39.  
  40. // This is adding the canvas to the frame.
  41. frame.add(this, BorderLayout.CENTER);
  42. // Sets the frame to the preferred size.
  43. frame.pack();
  44.  
  45. frame.setResizable(false);
  46. frame.setLocationRelativeTo(null);
  47. frame.setVisible(true);
  48. }
  49.  
  50.  
  51. public synchronized void start()
  52. {
  53. running = true;
  54. new Thread(this).start();
  55. }
  56.  
  57. public synchronized void stop()
  58. {
  59. running = false;
  60. }
  61.  
  62. public void run()
  63. {
  64. long lastTime = System.nanoTime();
  65. double nsPerTick = 1000000000D / 60D;
  66.  
  67. int ticks = 0;
  68. int frames = 0;
  69.  
  70. long lastTimer = System.currentTimeMillis();
  71. double delta = 0;
  72.  
  73. while(running)
  74. {
  75. long now = System.nanoTime();
  76. delta += (now - lastTime) / nsPerTick;
  77. lastTime = now;
  78. boolean shouldRender = true;
  79.  
  80. while (delta >= 1)
  81. {
  82. ticks++;
  83. tick();
  84. delta -= 1;
  85. shouldRender = true;
  86. }
  87.  
  88. try
  89. {
  90. Thread.sleep(2);
  91. } catch (InterruptedException e)
  92. {
  93. e.printStackTrace();
  94. }
  95.  
  96. if (shouldRender)
  97. {
  98. frames++;
  99. render();
  100. }
  101.  
  102. if(System.currentTimeMillis() - lastTimer >= 1000)
  103. {
  104. lastTimer += 1000;
  105. System.out.println(ticks + " ticks, " + frames + " frames.");
  106. frames = 0;
  107. ticks = 0;
  108. }
  109. }
  110. }
  111.  
  112. // Updates the logics of the game.
  113. public void tick()
  114. {
  115. tickCount++;
  116.  
  117. for(int i = 0; i < pixels.length; i++)
  118. {
  119. pixels[i] = i + tickCount;
  120. }
  121. }
  122.  
  123. // Prints out the logic that the tick does.
  124. public void render()
  125. {
  126. BufferStrategy bs = getBufferStrategy();
  127. if(bs == null)
  128. {
  129. createBufferStrategy(3);
  130. return;
  131. }
  132.  
  133. Graphics g = bs.getDrawGraphics();
  134.  
  135. g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  136.  
  137. g.dispose();
  138. bs.show();
  139. }
  140.  
  141. public static void main(String args[])
  142. {
  143. new MainGame().start();
  144. }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment