Advertisement
Guest User

Game class

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