Advertisement
Guest User

Game.java

a guest
Sep 8th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. package main;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.Font;
  7. import java.awt.Graphics;
  8. import java.awt.event.KeyEvent;
  9. import java.awt.image.BufferStrategy;
  10. import java.awt.image.BufferedImage;
  11. import java.awt.image.DataBufferInt;
  12. import java.util.Random;
  13.  
  14. import javax.swing.JFrame;
  15.  
  16. import main.Entity.mob.Player;
  17. import main.Level;
  18. import main.RandomLevel;
  19.  
  20.  
  21. public class Game extends Canvas implements Runnable {
  22. private static final long serialVersionUID = 1L;
  23.  
  24. private static int width = 300;
  25. private static int height = width / 16 * 9;
  26. private static int scale = 3;
  27. public static String title = "Drimian";
  28.  
  29. private Thread thread;
  30. private JFrame frame;
  31. private Keyboard key;
  32. private Level level;
  33. private Player player;
  34. private boolean running = false;
  35.  
  36. private Screen screen;
  37.  
  38. private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  39. private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
  40.  
  41. private Keyboard input;
  42.  
  43. public Game() {
  44. Dimension size = new Dimension(width * scale, height * scale);
  45. setPreferredSize(size);
  46.  
  47. screen = new Screen(width, height);
  48. frame = new JFrame();
  49. key = new Keyboard();
  50. addKeyListener(key);
  51.  
  52. Mouse mouse = new Mouse();
  53. addMouseListener(mouse);
  54. addMouseMotionListener(mouse);
  55. level = new SpawnLevel("/SpawnLevel.png");
  56.  
  57. //TileCoord playerSpawn = new TileCoord (25, 56);
  58. //player = new Player(playerSpawn.x(), playerSpawn.y(), key);
  59. player = new Player(392, 1695, key);
  60. //player = new Player(392, 895, key);
  61. player.init(level);
  62.  
  63. }
  64.  
  65. public static int getWindowWidth() {
  66. return width * scale;
  67. }
  68.  
  69. public static int getWindowHeight() {
  70. return height * scale;
  71. }
  72.  
  73. public synchronized void start() {
  74. running = true;
  75. thread = new Thread(this, "Display");
  76. thread.start();
  77.  
  78. }
  79.  
  80. public synchronized void stop() {
  81. running = false;
  82. try {
  83. thread.join();
  84. } catch (InterruptedException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88.  
  89. public void run() {
  90. long lastTime = System.nanoTime();
  91. long timer = System.currentTimeMillis();
  92. final double ns = 1000000000.0 / 60.0;
  93. double delta = 0;
  94. int frames = 0;
  95. int updates = 0;
  96. requestFocus();
  97. while (running) {
  98. long now = System.nanoTime();
  99. delta += (now - lastTime) / ns;
  100. lastTime = now;
  101. while (delta > 1) {
  102. update();
  103. updates ++;
  104. delta--;
  105. }
  106. render();
  107. frames++;
  108.  
  109. if (System.currentTimeMillis() - timer > 1000) {
  110. timer += 1000;
  111. System.out.println(updates + "ups, " + frames + "fps");
  112. frame.setTitle(title + " | " + updates + "ups, " + frames + "fps");
  113. updates = 0;
  114. frames = 0;
  115. }
  116. }
  117. stop();
  118. }
  119.  
  120. public void update() {
  121. key.update();
  122. player.update();
  123. level.update();
  124. }
  125.  
  126. public void render() {
  127. BufferStrategy bs = getBufferStrategy();
  128. if (bs == null) {
  129. createBufferStrategy(3);
  130. return;
  131. }
  132.  
  133. screen.clear();
  134. int xScroll = player.x - screen.width / 2;
  135. int yScroll = player.y - screen.height / 2;
  136. level.render(xScroll, yScroll, screen);
  137. player.render(screen);
  138. for (int i = 0; i < pixels.length; i++) {
  139. pixels[i] = screen.pixels[i];
  140. }
  141.  
  142. Graphics g = bs.getDrawGraphics();
  143. g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  144. g.setColor(Color.BLACK);
  145. g.setFont(new Font("Georgia", 0, 50));
  146. //g.fillRect(Mouse.getX() - 32, Mouse.getY() - 32, 64, 64);
  147. //if (Mouse.getButton() != -1) g.drawString("Button: " + Mouse.getButton(), 80, 80);
  148. //if (input.inventory) g.drawString("E", 80, 80);
  149. g.dispose();
  150. bs.show();
  151. }
  152.  
  153. public static void main(String[] args) {
  154. Game game = new Game();
  155. game.frame.setResizable(true);
  156. game.frame.setTitle(Game.title);
  157. game.frame.add(game);
  158. game.frame.pack();
  159. game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  160. game.frame.setLocationRelativeTo(null);
  161. game.frame.setVisible(true);
  162.  
  163. game.start();
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement