Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. import java.awt.Canvas;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Point;
  7. import java.awt.image.BufferStrategy;
  8. import java.awt.image.BufferedImage;
  9. import javax.swing.JFrame;
  10.  
  11. public class Game extends Canvas implements Runnable {
  12.  
  13. public static final long serialVersionUID = 1L;
  14. public static final short WH = 400, W = 400+6, H = 400+28;
  15. public static final byte UF = 60;
  16. public static Graphics g;
  17. public static boolean pause = false;
  18.  
  19. public static void main (String[] args) {
  20.  
  21. Game game = new Game();
  22. game.addKeyListener(new Input());
  23. game.setFocusable(true);
  24.  
  25. BufferedImage icon = new BufferedImage(32,32,BufferedImage.TYPE_INT_RGB);
  26. Graphics2D g2d = icon.createGraphics();
  27. g2d.setColor(Player.C1);
  28. g2d.fillRect(0,0,icon.getWidth(),icon.getHeight()/2);
  29. g2d.setColor(Player.C2);
  30. g2d.fillRect(0,icon.getHeight()/2,icon.getWidth(),icon.getHeight()/2);
  31.  
  32. JFrame frame = new JFrame("The Astronaut");
  33. frame.add(game);
  34. frame.pack();
  35. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  36. frame.setSize(W,H);
  37. frame.setResizable(false);
  38. frame.addKeyListener(new Input());
  39. frame.setLocationRelativeTo(null);
  40. frame.setIconImage(icon);
  41. frame.setVisible(true);
  42.  
  43. Level.load(Spawn.levelX,Spawn.levelY);
  44. Spawn.respawn();
  45. game.start();
  46.  
  47. }
  48.  
  49. public void start () {
  50. setCursor(getToolkit().createCustomCursor(new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR),new Point(0,0),""));
  51. new Thread(this).start();
  52. }
  53.  
  54. public void run () {
  55.  
  56. long before = System.nanoTime(), now;
  57. double unprocessed = 0;
  58.  
  59. long counter = System.currentTimeMillis();
  60. byte a = 0;
  61.  
  62. while (true) {
  63.  
  64. if (!pause) {
  65.  
  66. now = System.nanoTime();
  67. unprocessed += (now-before)*UF/1_000_000_000D;
  68.  
  69. while (unprocessed >= 1) {
  70. update();
  71. render();
  72. unprocessed--;
  73. a++;
  74. }
  75.  
  76. before = now;
  77.  
  78. if (System.currentTimeMillis()-counter >= 1_000) {
  79. System.out.println("UF: " + a);
  80. counter = System.currentTimeMillis();
  81. a = 0;
  82. }
  83.  
  84. } else {
  85.  
  86. before = System.nanoTime();
  87. unprocessed = 0;
  88. a = 0;
  89. counter = System.currentTimeMillis();
  90.  
  91. Input.left = false;
  92. Input.right = false;
  93. Input.up = false;
  94. Input.down = false;
  95. Input.ctrl = false;
  96.  
  97. if (!Input.p){
  98. pause = false;
  99. }
  100.  
  101. }
  102.  
  103. }
  104.  
  105. }
  106.  
  107. public void update () {
  108. if (Input.p || !hasFocus()){
  109. pause = true;
  110. Input.p = true;
  111. return;
  112. }
  113. Level.update();
  114. }
  115.  
  116. public void render () {
  117. BufferStrategy bs = getBufferStrategy();
  118. if (bs == null) {
  119. createBufferStrategy(2);
  120. return;
  121. }
  122. g = bs.getDrawGraphics();
  123. Level.render();
  124. if (pause) {
  125. g.setColor(new Color(0F,0F,0F,1/3F));
  126. g.fillRect(0,0,Game.WH,Game.WH);
  127. g.setFont(new Font("Calibri", Font.PLAIN, 20));
  128. g.setColor(Color.WHITE);
  129. g.drawString("Press P to continue",Game.WH/2,Game.WH/2);
  130. }
  131. g.dispose();
  132. bs.show();
  133. }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement