Starkdop

Untitled

Mar 1st, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.02 KB | None | 0 0
  1. package wingsor;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.awt.image.*;
  6. import java.io.*;
  7.  
  8. import javax.imageio.*;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JPanel;
  12.  
  13. public class Game extends Canvas implements KeyListener {
  14.  
  15.     public static final int WIDTH   = 200;
  16.     public static final int HEIGHT  = 300;
  17.     public static final int SCROLL  = 4;
  18.     public static final int GRAVITE = 2;
  19.    
  20.     private JFrame        frame;
  21.     private Wingsy        wingsy;
  22.     private BufferedImage background;
  23.     private BufferedImage background2;
  24.     private int           posBackground;
  25.     private int           posBackground2;
  26.     private Tuyau         t1;
  27.     private Tuyau         t2;
  28.     private Tuyau         t3;
  29.     private int           score;
  30.     private boolean       perdu;
  31.    
  32.     public Game() {
  33.         frame  = new JFrame();
  34.         wingsy = new Wingsy();
  35.        
  36.         try {
  37.             background  = ImageIO.read(new File("res/background.png"));
  38.             background2 = ImageIO.read(new File("res/background.png"));
  39.         } catch (IOException e) {
  40.             e.printStackTrace();
  41.         }
  42.        
  43.         posBackground  = 0;
  44.         posBackground2 = Game.WIDTH;
  45.        
  46.         t1 = new Tuyau(Game.WIDTH                  );
  47.         t2 = new Tuyau(Game.WIDTH + Tuyau.ECART    );
  48.         t3 = new Tuyau(Game.WIDTH + Tuyau.ECART * 2);
  49.        
  50.         perdu = false;
  51.        
  52.         // Configuration JFrame
  53.         frame.setSize                 (new Dimension(Game.WIDTH, Game.HEIGHT));
  54.         frame.setTitle                ("Wingsor"                             );
  55.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE                  );
  56.         frame.setVisible              (true                                  );
  57.         frame.add                     (this                                  );
  58.        
  59.         // Ajout de l'événement clavier
  60.         setFocusable  (true);
  61.         addKeyListener(this);
  62.        
  63.     }
  64.  
  65.     public void paint(Graphics g) {
  66.         // Fond du jeu
  67.         if (posBackground2 < 1) {
  68.             posBackground  = 0;
  69.             posBackground2 = Game.WIDTH;
  70.         }
  71.        
  72.         g.drawImage(background, posBackground, 0, Game.WIDTH, Game.HEIGHT-20, null);
  73.         g.drawImage(background, posBackground2, 0, Game.WIDTH, Game.HEIGHT-20, null);
  74.        
  75.         // Si pas perdu
  76.         if (!perdu) {
  77.             posBackground -= Game.SCROLL;
  78.             posBackground2-= Game.SCROLL;
  79.         }
  80.        
  81.         // Wingsy
  82.         wingsy.paint(g);
  83.        
  84.         // Affichage des tuyaux
  85.         t1.paint(g);
  86.         t2.paint(g);
  87.         t3.paint(g);
  88.        
  89.         // Affichage du score
  90.         /*g.setColor(Color.BLACK);
  91.         g.drawString("Score"+score, Game.WIDTH/3, 30);*/
  92.     }
  93.    
  94.     public void collision() {
  95.         if (wingsy.getHitbox().intersects(t1.getHitboxTop()))
  96.             System.out.println("lol");
  97.         if (wingsy.getHitbox().intersects(t1.getHitboxBot()))
  98.             System.out.println("lol");
  99.         if (wingsy.getHitbox().intersects(t2.getHitboxTop()))
  100.             System.out.println("lol");
  101.         if (wingsy.getHitbox().intersects(t2.getHitboxBot()))
  102.             System.out.println("lol");
  103.     }
  104.    
  105.     public void perdu() {
  106.         perdu = true;
  107.     }
  108.    
  109.     public void run() {
  110.         int run = 0;
  111.        
  112.         while (true) {
  113.             if (!perdu && run >= 10) {
  114.                 t1.move();
  115.                 t2.move();
  116.                 t3.move();
  117.                 wingsy.move();
  118.                
  119.                 // Score + 1
  120.                 t1.score(this, wingsy);
  121.                 t2.score(this, wingsy);
  122.                 t3.score(this, wingsy);
  123.                
  124.                 if (wingsy.POSX == t1.getPosX() || wingsy.POSX == t2.getPosX() || wingsy.POSX == t3.getPosX())
  125.                     score++;
  126.                
  127.                 t1.collision(wingsy, this); // Verifie s'il y a collision
  128.                 t2.collision(wingsy, this);
  129.                
  130.                 // Si l'oiseau touche le sol
  131.                 if (wingsy.getPosY() >= Game.HEIGHT - wingsy.HEIGHT * 2)
  132.                     perdu();
  133.             }
  134.            
  135.             repaint();
  136.             frame.validate();
  137.            
  138.             try {
  139.                 Thread.sleep(50);
  140.             } catch (InterruptedException e) {
  141.                 e.printStackTrace();
  142.             }
  143.            
  144.             run++;
  145.         }
  146.     }
  147.    
  148.     public int    getScore() { return score; }
  149.     public JFrame getFrame() { return frame; }
  150.    
  151.     public void setScore(int score) { this.score+=score; }
  152.    
  153.     public void keyTyped(KeyEvent e) {
  154.        
  155.     }
  156.  
  157.     public void keyPressed(KeyEvent e) {
  158.         wingsy.sauter();
  159.     }
  160.  
  161.     public void keyReleased(KeyEvent e) {
  162.        
  163.     }
  164.    
  165.     public static void main(String[] a) {
  166.         Game game = new Game();
  167.         game.run();
  168.     }
  169.    
  170. }
Advertisement
Add Comment
Please, Sign In to add comment