Advertisement
fsvuuw

Untitled

Apr 19th, 2014
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. package com.thecherno.rain;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Color;
  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 java.util.Random;
  11.  
  12. import javax.swing.JFrame;
  13.  
  14. import com.thecherno.rain.graphics.Screen;
  15. import com.thecherno.rain.input.Keyboard;
  16. import com.thecherno.rain.level.Level;
  17. import com.thecherno.rain.level.RandomLevel;
  18.  
  19. public class Game extends Canvas implements Runnable {
  20. private static final long serialVersionUID = 1L;
  21.  
  22. public static int width = 300; //width of screen
  23. public static int height = width / 16 * 9; //height of the screen based on 9/16 ratio
  24. public static int scale = 3; //scale
  25. public static String title = "Rain";
  26.  
  27. private Thread thread; // basically a thread is .exe sub-program
  28. private JFrame frame;
  29. private Keyboard key;
  30. private Level level;
  31. private boolean running = false; // helps keep program open see line 28 for more
  32.  
  33. private Screen screen;
  34.  
  35. private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //create an image
  36. private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData(); // accessing the previously created image
  37.  
  38. public Game() {
  39. Dimension size = new Dimension(width * scale, height * scale); // sets the display screen by giving the screen dimensions
  40. setPreferredSize(size); //sets the dimensions size in line above to the size of the game screen
  41.  
  42. screen = new Screen(width, height); // sets screen (the integer )to width and heing
  43. frame = new JFrame();
  44. key = new Keyboard();
  45. level = new RandomLevel(64, 64);
  46.  
  47.  
  48.  
  49. addKeyListener(key); // checks for keys and applies them and connects the keyboard class integer keyboard update
  50. }
  51.  
  52. public synchronized void start () {
  53. running = true ; // sets running equal to true so a while will run and keep java from closing when the program is done running all exucutions
  54. thread = new Thread (this, "Display"); //creates a thread in this class
  55. thread.start(); // starts the newly created thread
  56. }
  57.  
  58. public synchronized void stop () { //safely stops the thread
  59. running = false; //sets running false so the program will be able to close
  60. try {
  61. thread.join();
  62. } catch(InterruptedException e) {
  63. e.printStackTrace();
  64. }
  65. }
  66. public void run() {
  67. long lastTime = System.nanoTime(); //creating a timer variable
  68. long timer = System.currentTimeMillis();
  69. final double ns = 1000000000.0 / 60.0; // converts nanoseconds to milliseconds
  70. double delta = 0;
  71. int frames = 0;
  72. int updates = 0;
  73. requestFocus();
  74. while (running) { //as long as running is equal to true the program will run this while loop see line 10 for more
  75. long now = System.nanoTime(); // everything in the next 4 lines will be just creating a timer
  76. delta += (now-lastTime) / ns;
  77. lastTime = now;
  78. while (delta >=1) {
  79. update(); //updates the screen at a certain FPS
  80. updates ++; // keeps track of how many times update has happened
  81. delta--;
  82. }
  83. render(); //puts the render, everything graphical into the backup but does not display right awa
  84. frames++; // keeps track of how many time the render has happened
  85.  
  86. if (System.currentTimeMillis() - timer > 1000) { //adds 1 second every time while delta is complete
  87. timer += 1000;
  88. System.out.println(updates + "ups, " + frames + " fps"); // displays fps
  89. frame.setTitle(title + " | " + updates + "ups, " + frames + " fps" );
  90. updates = 0;
  91. frames = 0;
  92. }
  93.  
  94. }
  95. stop();
  96. }
  97.  
  98. int x = 0, y = 0;
  99.  
  100. public void update() { // just creates the update void
  101. key.update();
  102. if (key.up) y++;
  103. if (key.down) y--;
  104. if (key.left) x++;
  105. if (key.right) x--;
  106.  
  107. }
  108.  
  109. public void render() { // creates the render void which holds all graphical information
  110. BufferStrategy bs = getBufferStrategy(); // renders backup screens to come
  111. if(bs == null) { // limits backup to hold a certain amount
  112. createBufferStrategy(3); // limits backup to hold 3 backup screens
  113. return; // returns back to the start of the program
  114. }
  115. screen.clear(); //clears everything on the screen right befor the next rendering happens
  116. level.render(x, y, screen);
  117.  
  118. for (int i = 0; i < pixels.length; i++) { // just do it because
  119. pixels[i] = screen.pixels[i]; // sets the game class pixel arrays equal to the screen class pixel arrays
  120. }
  121. Graphics g = bs.getDrawGraphics();
  122. g.fillRect(0, 0, getWidth(), getHeight()); // builds a rectangle the size of the program window with the color as the previously set color
  123. g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  124. g.dispose();
  125. bs.show();
  126.  
  127. }
  128.  
  129. public static void main (String[] args) { //starts the game // where the main programming is // execution for the game happens here
  130. Game game = new Game(); // allows all integers in the game class into this one section
  131. game.frame.setResizable(false); // restricts screen resizing
  132. game.frame.setTitle(Game.title); //REMEMBER THIS SETS THE TITLE NAME ON THE PROGRAM BOX
  133. game.frame.add(game); // adds all Game class stuff into this component
  134. game.frame.pack(); // resizes this component to the same size as public game
  135. game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //stops this component when you exit off of the program
  136. game.frame.setLocationRelativeTo(null); //sets the program window to run right in the middle of the screen
  137. game.frame.setVisible(true); //makes the program window visible on the screen
  138.  
  139. game.start(); // starts the game
  140. }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement