Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.thecherno.rain;
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.image.BufferStrategy;
- import java.awt.image.BufferedImage;
- import java.awt.image.DataBufferInt;
- import java.util.Random;
- import javax.swing.JFrame;
- import com.thecherno.rain.graphics.Screen;
- import com.thecherno.rain.input.Keyboard;
- import com.thecherno.rain.level.Level;
- import com.thecherno.rain.level.RandomLevel;
- public class Game extends Canvas implements Runnable {
- private static final long serialVersionUID = 1L;
- public static int width = 300; //width of screen
- public static int height = width / 16 * 9; //height of the screen based on 9/16 ratio
- public static int scale = 3; //scale
- public static String title = "Rain";
- private Thread thread; // basically a thread is .exe sub-program
- private JFrame frame;
- private Keyboard key;
- private Level level;
- private boolean running = false; // helps keep program open see line 28 for more
- private Screen screen;
- private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //create an image
- private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData(); // accessing the previously created image
- public Game() {
- Dimension size = new Dimension(width * scale, height * scale); // sets the display screen by giving the screen dimensions
- setPreferredSize(size); //sets the dimensions size in line above to the size of the game screen
- screen = new Screen(width, height); // sets screen (the integer )to width and heing
- frame = new JFrame();
- key = new Keyboard();
- level = new RandomLevel(64, 64);
- addKeyListener(key); // checks for keys and applies them and connects the keyboard class integer keyboard update
- }
- public synchronized void start () {
- 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
- thread = new Thread (this, "Display"); //creates a thread in this class
- thread.start(); // starts the newly created thread
- }
- public synchronized void stop () { //safely stops the thread
- running = false; //sets running false so the program will be able to close
- try {
- thread.join();
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void run() {
- long lastTime = System.nanoTime(); //creating a timer variable
- long timer = System.currentTimeMillis();
- final double ns = 1000000000.0 / 60.0; // converts nanoseconds to milliseconds
- double delta = 0;
- int frames = 0;
- int updates = 0;
- requestFocus();
- while (running) { //as long as running is equal to true the program will run this while loop see line 10 for more
- long now = System.nanoTime(); // everything in the next 4 lines will be just creating a timer
- delta += (now-lastTime) / ns;
- lastTime = now;
- while (delta >=1) {
- update(); //updates the screen at a certain FPS
- updates ++; // keeps track of how many times update has happened
- delta--;
- }
- render(); //puts the render, everything graphical into the backup but does not display right awa
- frames++; // keeps track of how many time the render has happened
- if (System.currentTimeMillis() - timer > 1000) { //adds 1 second every time while delta is complete
- timer += 1000;
- System.out.println(updates + "ups, " + frames + " fps"); // displays fps
- frame.setTitle(title + " | " + updates + "ups, " + frames + " fps" );
- updates = 0;
- frames = 0;
- }
- }
- stop();
- }
- int x = 0, y = 0;
- public void update() { // just creates the update void
- key.update();
- if (key.up) y++;
- if (key.down) y--;
- if (key.left) x++;
- if (key.right) x--;
- }
- public void render() { // creates the render void which holds all graphical information
- BufferStrategy bs = getBufferStrategy(); // renders backup screens to come
- if(bs == null) { // limits backup to hold a certain amount
- createBufferStrategy(3); // limits backup to hold 3 backup screens
- return; // returns back to the start of the program
- }
- screen.clear(); //clears everything on the screen right befor the next rendering happens
- level.render(x, y, screen);
- for (int i = 0; i < pixels.length; i++) { // just do it because
- pixels[i] = screen.pixels[i]; // sets the game class pixel arrays equal to the screen class pixel arrays
- }
- Graphics g = bs.getDrawGraphics();
- g.fillRect(0, 0, getWidth(), getHeight()); // builds a rectangle the size of the program window with the color as the previously set color
- g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
- g.dispose();
- bs.show();
- }
- public static void main (String[] args) { //starts the game // where the main programming is // execution for the game happens here
- Game game = new Game(); // allows all integers in the game class into this one section
- game.frame.setResizable(false); // restricts screen resizing
- game.frame.setTitle(Game.title); //REMEMBER THIS SETS THE TITLE NAME ON THE PROGRAM BOX
- game.frame.add(game); // adds all Game class stuff into this component
- game.frame.pack(); // resizes this component to the same size as public game
- game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //stops this component when you exit off of the program
- game.frame.setLocationRelativeTo(null); //sets the program window to run right in the middle of the screen
- game.frame.setVisible(true); //makes the program window visible on the screen
- game.start(); // starts the game
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement