Advertisement
Guest User

Game.java

a guest
Dec 1st, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.30 KB | None | 0 0
  1. package h.c.rpg;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.image.BufferStrategy;
  7. import java.awt.image.BufferedImage;
  8. import java.awt.image.DataBufferInt;
  9.  
  10. import javax.swing.JFrame;
  11.  
  12. import h.c.rpg.entity.mob.Player;
  13. import h.c.rpg.graphics.Screen;
  14. import h.c.rpg.input.Keyboard;
  15. import h.c.rpg.level.Level;
  16. import h.c.rpg.level.RandomLevel;
  17. import h.c.rpg.level.SpawnLevel;
  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 --- set to 300
  23.         public static int height = width / 16 * 9;      // 168.75 - height of screen --- based on 16:9 ratio
  24.         public static int scale = 3;    // scales up by 3
  25.         public static String title = "16-bit Warrior "; // sets title of window to a string value
  26.  
  27.         private Thread thread;  // thread(s) = sub-process
  28.         private JFrame frame;
  29.         private Keyboard key;
  30.         private Level level;
  31.         private Player player;
  32.         private boolean running = false; // helps keep program open see line 28 for more
  33.  
  34.         private Screen screen;
  35.  
  36.         private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);     // create an image
  37.         private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();    // accessing the previously created image
  38.  
  39.         public Game() {
  40.                 Dimension size = new Dimension(width * scale, height * scale);  // sets display screen by giving the screen dimensions
  41.                 setPreferredSize(size);                                                                                                 // sets dimensions size in line above to the size of game screen
  42.  
  43.                 screen = new Screen(width, height);     // sets screen integer to width and height
  44.                 frame = new JFrame();
  45.                 key = new Keyboard();
  46.                 level = Level.spawn;
  47.                 player = new Player(6 * 16, 4 * 16, key);
  48.                
  49.                 frame.addKeyListener(key);      // checks for keys and applies them and connects the keyboard class interger keyboard update
  50.         }
  51.  
  52.         public synchronized void start() {
  53.                 running = true;
  54.                 thread = new Thread(this, "Display");
  55.                 thread.start();
  56.         }
  57.  
  58.         public synchronized void stop() {
  59.                 running = false;
  60.                 try {
  61.                         thread.join();
  62.                 } catch (InterruptedException e) {
  63.                         e.printStackTrace();
  64.                 }
  65.         }
  66.  
  67.         public void run() {
  68.                 long lastTime = System.nanoTime();
  69.                 long timer = System.currentTimeMillis();
  70.                 final double ns = 1000000000.0 / 60.0; //60.0;
  71.                 double delta = 0;
  72.                 int frames = 0;
  73.                 int updates = 0;
  74.                 frame.requestFocus();
  75.  
  76.                 while (running) {
  77.                         long now = System.nanoTime();
  78.                         delta += (now - lastTime) / ns;
  79.                         lastTime = now;
  80.                         while (delta >= 1) {
  81.                                 update(); // Can be replaced by tick() like Minecraft
  82.                                 updates++;
  83.                                 delta--;
  84.                         }
  85.                         render();
  86.                         frames++;
  87.  
  88.                         if (System.currentTimeMillis() - timer > 1000) {
  89.                                 timer += 1000;
  90.                                 System.out.println("[" + updates + " ups, " + frames + " fps" + "]");
  91.                                 frame.setTitle(title + "[" + updates + " ups, " + frames + " fps" + "]");
  92.                                 updates = 0;
  93.                                 frames = 0;
  94.                         }
  95.                 }
  96.                 stop();
  97.         }
  98.        
  99.         public void update() {
  100.                 key.update();
  101.                 player.update();
  102.         }
  103.  
  104.         public void render() {
  105.                 BufferStrategy bs = getBufferStrategy();
  106.                 if (bs == null) {
  107.                         createBufferStrategy(3);
  108.                         return;
  109.                 }
  110.  
  111.                 screen.clear();                         // clears the graphics
  112.                 int xScroll = player.x - screen.width / 2;
  113.                 int yScroll = player.y - screen.height / 2;
  114.                 level.render(xScroll, yScroll, screen);         // renders level & moves with on x / y axis with input
  115.                 level.render(player.x, player.x, screen);
  116. //              player.render(screen);          // renders player with screen
  117.  
  118.                 for (int i = 0; i < pixels.length; i++) {
  119.                         pixels[i] = screen.pixels[i];
  120.                 }
  121.  
  122.                 Graphics g = bs.getDrawGraphics();
  123.                 g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  124.                 //g.setColor(Color.BLACK);              // original white       // black color for coordinates
  125.                 //g.setFont(new Font("Verdana", 0, 25));        // size 25 verdana font for coordinates
  126.                 //g.drawString("X: " + player.x + ", Y: " + player.y, 700, 22);         // original 350, 300    // string that tracks
  127.                 // player.x and player.y for coordinates and places it(the string) on the window at 700, 22 (top-right most corner)
  128.                 g.dispose();
  129.                 bs.show();
  130.         }
  131.  
  132.         public static void main(String[] args) {
  133.                 Game game = new Game();
  134.                 game.frame.setResizable(false); // Can not resize window (it will cause graphica errors)
  135.                 game.frame.setTitle(Game.title); // Sets window title to 'Rain' (can be changed)
  136.                 game.frame.add(game);
  137.                 game.frame.pack(); // Packs the game
  138.                 game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Exits the game when you click the red x
  139.                 game.frame.setLocationRelativeTo(null);
  140.                 game.frame.setVisible(true); // Sets visibility to true
  141.  
  142.                 game.start(); // Starts the game
  143.         }
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement