Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package h.c.rpg;
- import java.awt.Canvas;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.image.BufferStrategy;
- import java.awt.image.BufferedImage;
- import java.awt.image.DataBufferInt;
- import javax.swing.JFrame;
- import h.c.rpg.entity.mob.Player;
- import h.c.rpg.graphics.Screen;
- import h.c.rpg.input.Keyboard;
- import h.c.rpg.level.Level;
- import h.c.rpg.level.RandomLevel;
- import h.c.rpg.level.SpawnLevel;
- public class Game extends Canvas implements Runnable {
- private static final long serialVersionUID = 1L;
- public static int width = 300; // width of screen --- set to 300
- public static int height = width / 16 * 9; // 168.75 - height of screen --- based on 16:9 ratio
- public static int scale = 3; // scales up by 3
- public static String title = "16-bit Warrior "; // sets title of window to a string value
- private Thread thread; // thread(s) = sub-process
- private JFrame frame;
- private Keyboard key;
- private Level level;
- private Player player;
- 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 display screen by giving the screen dimensions
- setPreferredSize(size); // sets dimensions size in line above to the size of game screen
- screen = new Screen(width, height); // sets screen integer to width and height
- frame = new JFrame();
- key = new Keyboard();
- level = Level.spawn;
- player = new Player(6 * 16, 4 * 16, key);
- frame.addKeyListener(key); // checks for keys and applies them and connects the keyboard class interger keyboard update
- }
- public synchronized void start() {
- running = true;
- thread = new Thread(this, "Display");
- thread.start();
- }
- public synchronized void stop() {
- running = false;
- try {
- thread.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void run() {
- long lastTime = System.nanoTime();
- long timer = System.currentTimeMillis();
- final double ns = 1000000000.0 / 60.0; //60.0;
- double delta = 0;
- int frames = 0;
- int updates = 0;
- frame.requestFocus();
- while (running) {
- long now = System.nanoTime();
- delta += (now - lastTime) / ns;
- lastTime = now;
- while (delta >= 1) {
- update(); // Can be replaced by tick() like Minecraft
- updates++;
- delta--;
- }
- render();
- frames++;
- if (System.currentTimeMillis() - timer > 1000) {
- timer += 1000;
- System.out.println("[" + updates + " ups, " + frames + " fps" + "]");
- frame.setTitle(title + "[" + updates + " ups, " + frames + " fps" + "]");
- updates = 0;
- frames = 0;
- }
- }
- stop();
- }
- public void update() {
- key.update();
- player.update();
- }
- public void render() {
- BufferStrategy bs = getBufferStrategy();
- if (bs == null) {
- createBufferStrategy(3);
- return;
- }
- screen.clear(); // clears the graphics
- int xScroll = player.x - screen.width / 2;
- int yScroll = player.y - screen.height / 2;
- level.render(xScroll, yScroll, screen); // renders level & moves with on x / y axis with input
- level.render(player.x, player.x, screen);
- // player.render(screen); // renders player with screen
- for (int i = 0; i < pixels.length; i++) {
- pixels[i] = screen.pixels[i];
- }
- Graphics g = bs.getDrawGraphics();
- g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
- //g.setColor(Color.BLACK); // original white // black color for coordinates
- //g.setFont(new Font("Verdana", 0, 25)); // size 25 verdana font for coordinates
- //g.drawString("X: " + player.x + ", Y: " + player.y, 700, 22); // original 350, 300 // string that tracks
- // player.x and player.y for coordinates and places it(the string) on the window at 700, 22 (top-right most corner)
- g.dispose();
- bs.show();
- }
- public static void main(String[] args) {
- Game game = new Game();
- game.frame.setResizable(false); // Can not resize window (it will cause graphica errors)
- game.frame.setTitle(Game.title); // Sets window title to 'Rain' (can be changed)
- game.frame.add(game);
- game.frame.pack(); // Packs the game
- game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Exits the game when you click the red x
- game.frame.setLocationRelativeTo(null);
- game.frame.setVisible(true); // Sets visibility to true
- game.start(); // Starts the game
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement