Advertisement
Guest User

Game class

a guest
Mar 31st, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.26 KB | None | 0 0
  1. package myFirstGame;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Canvas;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.awt.image.BufferStrategy;
  9. import java.awt.image.BufferedImage;
  10.  
  11. import javax.swing.ImageIcon;
  12. import javax.swing.JFrame;
  13.  
  14. public class Game extends Canvas implements Runnable {
  15.     private static final long serialVersionUID = 1L;
  16.  
  17.     JFrame frame;
  18.  
  19.     public static Player player;
  20.  
  21.     InputHandler IH;
  22.  
  23.     public final static int WIDTH = 720;
  24.     public final int HEIGHT = WIDTH / 16 * 9;
  25.     public final Dimension gameSize = new Dimension(WIDTH, HEIGHT);
  26.     public final String title = "^^";
  27.    
  28.     public int screenWidth = getWidth();
  29.     public int screenHeight = getHeight();
  30.  
  31.     BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  32.    
  33.     Image bg;
  34.  
  35.     static boolean gameRunning;
  36.  
  37.     @Override
  38.     public void run() {
  39.  
  40.         while (gameRunning) {
  41.  
  42.             tick();
  43.             render();
  44.  
  45.             try {
  46.                 Thread.sleep(20);
  47.  
  48.             } catch (Exception e) {
  49.                 e.printStackTrace();
  50.             }
  51.  
  52.         }
  53.  
  54.     }
  55.  
  56.     public synchronized void start() {
  57.  
  58.         Game.player.loaded = true;
  59.         gameRunning = true;
  60.         new Thread(this).start();
  61.  
  62.     }
  63.  
  64.     public synchronized void stop() {
  65.  
  66.         gameRunning = false;
  67.         System.exit(0);
  68.     }
  69.  
  70.     public Game() {
  71.  
  72.         frame = new JFrame();
  73.  
  74.         this.setMinimumSize(gameSize);
  75.         this.setPreferredSize(gameSize);
  76.         this.setMaximumSize(gameSize);
  77.  
  78.         frame.add(this, BorderLayout.CENTER);
  79.         frame.pack();
  80.  
  81.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  82.  
  83.         frame.setVisible(true);
  84.         frame.setResizable(false);
  85.         frame.setTitle(title);
  86.         frame.setLocationRelativeTo(null);
  87.  
  88.         this.addKeyListener(IH);
  89.         IH = new InputHandler(this);
  90.  
  91.         player = new Player(10, 405 - 40);
  92.        
  93.         bg = new ImageIcon("C:\\Users\\Bruker\\Pictures\\bg.png").getImage();
  94.  
  95.     }
  96.  
  97.     public void tick() {
  98.  
  99.         player.tick(this);
  100.  
  101.     }
  102.  
  103.     public void render() {
  104.         BufferStrategy bs = getBufferStrategy();
  105.         if (bs == null) {
  106.             createBufferStrategy(3);
  107.             return;
  108.         }
  109.         Graphics g = bs.getDrawGraphics();
  110.        
  111.         g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  112.  
  113.         g.drawImage(bg, 0, 0, getWidth(), getHeight(), null);
  114.        
  115.         player.render(g);
  116.        
  117.        
  118.        
  119.         g.dispose();
  120.         bs.show();
  121.  
  122.     }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement