Advertisement
Guest User

Untitled

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