Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package myFirstGame;
- 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 javax.swing.JFrame;
- public class Game extends Canvas implements Runnable {
- private static final long serialVersionUID = 1L;
- JFrame frame;
- public static Player player;
- InputHandler IH;
- public final static int WIDTH = 720;
- public final int HEIGHT = WIDTH / 16 * 9;
- public final Dimension gameSize = new Dimension(WIDTH, HEIGHT);
- public final String title = "myFirstGame";
- BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
- static boolean gameRunning;
- @Override
- public void run() {
- while (gameRunning) {
- tick();
- render();
- try {
- Thread.sleep(20);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public synchronized void start() {
- gameRunning = true;
- new Thread(this).start();
- }
- public synchronized void stop() {
- gameRunning = false;
- System.exit(0);
- }
- public Game() {
- frame = new JFrame();
- this.setMinimumSize(gameSize);
- this.setPreferredSize(gameSize);
- this.setMaximumSize(gameSize);
- frame.add(this, BorderLayout.CENTER);
- frame.pack();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- frame.setResizable(false);
- frame.setTitle(title);
- frame.setLocationRelativeTo(null);
- this.addKeyListener(IH);
- IH = new InputHandler(this);
- player = new Player(10, 405 - 20);
- }
- public void tick() {
- player.tick(this);
- }
- public void render() {
- BufferStrategy bs = getBufferStrategy();
- if (bs == null) {
- createBufferStrategy(3);
- return;
- }
- Graphics g = bs.getDrawGraphics();
- g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
- player.render(g);
- g.dispose();
- bs.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement