Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package wingsor;
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.image.*;
- import java.io.*;
- import javax.imageio.*;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- public class Game extends Canvas implements KeyListener {
- public static final int WIDTH = 200;
- public static final int HEIGHT = 300;
- public static final int SCROLL = 4;
- public static final int GRAVITE = 2;
- private JFrame frame;
- private Wingsy wingsy;
- private BufferedImage background;
- private BufferedImage background2;
- private int posBackground;
- private int posBackground2;
- private Tuyau t1;
- private Tuyau t2;
- private Tuyau t3;
- private int score;
- private boolean perdu;
- public Game() {
- frame = new JFrame();
- wingsy = new Wingsy();
- try {
- background = ImageIO.read(new File("res/background.png"));
- background2 = ImageIO.read(new File("res/background.png"));
- } catch (IOException e) {
- e.printStackTrace();
- }
- posBackground = 0;
- posBackground2 = Game.WIDTH;
- t1 = new Tuyau(Game.WIDTH );
- t2 = new Tuyau(Game.WIDTH + Tuyau.ECART );
- t3 = new Tuyau(Game.WIDTH + Tuyau.ECART * 2);
- perdu = false;
- // Configuration JFrame
- frame.setSize (new Dimension(Game.WIDTH, Game.HEIGHT));
- frame.setTitle ("Wingsor" );
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
- frame.setVisible (true );
- frame.add (this );
- // Ajout de l'événement clavier
- setFocusable (true);
- addKeyListener(this);
- }
- public void paint(Graphics g) {
- // Fond du jeu
- if (posBackground2 < 1) {
- posBackground = 0;
- posBackground2 = Game.WIDTH;
- }
- g.drawImage(background, posBackground, 0, Game.WIDTH, Game.HEIGHT-20, null);
- g.drawImage(background, posBackground2, 0, Game.WIDTH, Game.HEIGHT-20, null);
- // Si pas perdu
- if (!perdu) {
- posBackground -= Game.SCROLL;
- posBackground2-= Game.SCROLL;
- }
- // Wingsy
- wingsy.paint(g);
- // Affichage des tuyaux
- t1.paint(g);
- t2.paint(g);
- t3.paint(g);
- // Affichage du score
- /*g.setColor(Color.BLACK);
- g.drawString("Score"+score, Game.WIDTH/3, 30);*/
- }
- public void collision() {
- if (wingsy.getHitbox().intersects(t1.getHitboxTop()))
- System.out.println("lol");
- if (wingsy.getHitbox().intersects(t1.getHitboxBot()))
- System.out.println("lol");
- if (wingsy.getHitbox().intersects(t2.getHitboxTop()))
- System.out.println("lol");
- if (wingsy.getHitbox().intersects(t2.getHitboxBot()))
- System.out.println("lol");
- }
- public void perdu() {
- perdu = true;
- }
- public void run() {
- int run = 0;
- while (true) {
- if (!perdu && run >= 10) {
- t1.move();
- t2.move();
- t3.move();
- wingsy.move();
- // Score + 1
- t1.score(this, wingsy);
- t2.score(this, wingsy);
- t3.score(this, wingsy);
- if (wingsy.POSX == t1.getPosX() || wingsy.POSX == t2.getPosX() || wingsy.POSX == t3.getPosX())
- score++;
- t1.collision(wingsy, this); // Verifie s'il y a collision
- t2.collision(wingsy, this);
- // Si l'oiseau touche le sol
- if (wingsy.getPosY() >= Game.HEIGHT - wingsy.HEIGHT * 2)
- perdu();
- }
- repaint();
- frame.validate();
- try {
- Thread.sleep(50);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- run++;
- }
- }
- public int getScore() { return score; }
- public JFrame getFrame() { return frame; }
- public void setScore(int score) { this.score+=score; }
- public void keyTyped(KeyEvent e) {
- }
- public void keyPressed(KeyEvent e) {
- wingsy.sauter();
- }
- public void keyReleased(KeyEvent e) {
- }
- public static void main(String[] a) {
- Game game = new Game();
- game.run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment