Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ottdev.game;
- import java.awt.BorderLayout;
- 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 java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.JFrame;
- import com.ottdev.game.gfx.Screen;
- import com.ottdev.game.gfx.SpriteSheet;
- public class Game extends Canvas implements Runnable {
- public static final int HEIGHT = 240;
- public static final int WIDTH = HEIGHT * 16 / 9;
- private Thread thread;
- private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
- private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
- private boolean running = false;
- private int tickCount;
- private Screen screen;
- public void start() {
- if (running)
- return;
- running = true;
- new Thread(this).start();
- }
- public void stop() {
- running = false;
- }
- private void init() {
- try {
- screen = new Screen(WIDTH, HEIGHT, new SpriteSheet(ImageIO.read(Game.class.getResourceAsStream("/icons.png"))));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void run() {
- long lastTime = System.nanoTime();
- double unprocessed = 0;
- double nsPerTick = 1000000000.0 / 60;
- int frames = 0;
- int ticks = 0;
- long lastTimer1 = System.currentTimeMillis();
- init();
- while (running) {
- long now = System.nanoTime();
- unprocessed += (now - lastTime) / nsPerTick;
- lastTime = now;
- boolean shouldRender = true;
- while (unprocessed >= 1) {
- ticks++;
- tick();
- unprocessed -= 1;
- shouldRender = true;
- }
- try {
- Thread.sleep(2);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- if (shouldRender) {
- frames++;
- render();
- }
- if (System.currentTimeMillis() - lastTimer1 > 1000) {
- lastTimer1 += 1000;
- System.out.println(ticks + " ticks, " + frames + " fps");
- frames = 0;
- ticks = 0;
- }
- }
- }
- public void tick() {
- tickCount++;
- screen.xScroll++;
- screen.yScroll++;
- }
- public void render() {
- BufferStrategy bs = getBufferStrategy();
- if (bs == null) {
- createBufferStrategy(3);
- return;
- }
- screen.render(pixels, 0, WIDTH);
- Graphics g = bs.getDrawGraphics();
- g.fillRect(0, 0, getWidth(), getHeight());
- int ww = WIDTH * 3;
- int hh = WIDTH * 3;
- int xo = (getHeight()-ww)/2;
- int yo = (getWidth()-hh)/2;
- g.drawImage(image, xo, yo, ww, hh, null);
- g.dispose();
- bs.show();
- }
- public static void main(String[] args) {
- Game game = new Game();
- game.setMinimumSize(new Dimension(WIDTH * 2, HEIGHT * 2));
- game.setMaximumSize(new Dimension(WIDTH * 2, HEIGHT * 2));
- game.setPreferredSize(new Dimension(WIDTH * 2, HEIGHT * 2));
- JFrame frame = new JFrame();
- frame.setLayout(new BorderLayout());
- frame.add(game, BorderLayout.CENTER);
- frame.pack();
- frame.setLocationRelativeTo(null);
- frame.setTitle("The Game With No Name: Indev!");
- frame.setResizable(false);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- game.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement