Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mathmaurer.jeu;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import javax.swing.ImageIcon;
- import javax.swing.JPanel;
- @SuppressWarnings("serial")
- public class Scene extends JPanel{
- // Image de fond imgFond fait 740 sur 360
- private ImageIcon icoFond;
- private Image imgFond1;
- private Image imgFond2;
- // image panneauDépart
- private ImageIcon icoDepart;
- private Image imgDepart;
- // Image personnage
- private ImageIcon icoMario;
- private Image imgMario;
- //
- private int xFond1; // abscisse coin supérieur haut
- private int xFond2;
- private int dx; // deplace l'écran verticalement
- private int xPos; // permet de laisser fi
- /* CONSTRUCTEUR */
- public Scene(){
- super();
- this.xFond1 = -50; // déborde de chaque coté
- this.xFond2 = 690; // -50 + 740
- this.dx = 0;
- this.xPos = -1;
- // Image de fond
- icoFond = new ImageIcon(getClass().getResource("/images/fondEcran.png"));
- this.imgFond1 = this.icoFond.getImage();
- this.imgFond2 = this.icoFond.getImage();
- icoMario = new ImageIcon(getClass().getResource("/images/persoNormal.png"));
- this.imgMario = this.icoMario.getImage();
- // Image départ
- this.icoDepart = new ImageIcon(getClass().getResource("/images/panneauDepart.png"));
- this.imgDepart = this.icoDepart.getImage();
- // Infos par rapport à la fenetre
- this.setFocusable(true); //
- this.requestFocusInWindow(); // pour être sûr de récuperer le focus
- this.addKeyListener(new Clavier()); // ecoute les evenements au clavier de la scéne
- Thread chronoEcran = new Thread(new Chrono()); //
- chronoEcran.start();
- }
- /* GETTERS */
- public int getDx() {return dx;}
- public int getxPos() {return xPos;}
- /* SETTERS */
- public void setDx(int dx) {this.dx = dx;}
- public void setxPos(int xPos) {this.xPos = xPos;}
- public void setxFond1(int xFond1) {this.xFond1 = xFond1;}
- public void setxFond2(int xFond2) {this.xFond2 = xFond2;}
- /* METHODES */
- public void deplacementFond(){
- if(this.xPos >= 0){ // si xPos sup à 0 alors on peut bouger et si xpos égal -1 on pourra pas aller vers la gauche (bloquer)
- this.xPos = this.xPos + this.dx;
- this.xFond1 = this.xFond1 - this.dx; // la position va être mise a jour avec la pos de dx en moins
- this.xFond2 = this.xFond2 - this.dx;
- }
- // pour mettre l'image de fond à la suite
- if(this.xFond1 == -740){ this.xFond1 = 740; }
- else if(this.xFond2 == -740){ this.xFond2 = 740; }
- else if(this.xFond1 == 740){ this.xFond1 = -740; }
- else if(this.xFond2 == 740){ this.xFond2 = -740; }
- }
- public void paintComponent(Graphics g){ // insére les images
- super.paintComponent(g); // méthode héridité donc super méthode
- Graphics g2 = (Graphics2D)g;
- this.deplacementFond();
- // Image de fond
- g2.drawImage(this.imgFond1, this.xFond1, 0, null); // Dessin de l'image de fond
- g2.drawImage(this.imgFond2, this.xFond2, 0, null);
- g2.drawImage(this.imgMario, 150, 280, null); // provisoire
- // Image de départ
- g2.drawImage(this.imgDepart, 35 - this.xPos, 285, null);
- }
- }
- // Clavier.java
- package com.mathmaurer.jeu;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- public class Clavier implements KeyListener{
- @Override
- public void keyPressed(KeyEvent e) { // appuye sur la touche
- if(e.getKeyCode() == KeyEvent.VK_RIGHT){
- // Annule le décalage de 1 créé par deplacementFond
- if(Main.scene.getxPos() == -1){
- Main.scene.setxPos(0); // Réinitialisation de setxPOS
- Main.scene.setxFond1(-50); // Réinitialisation de xFond 1
- Main.scene.setxFond1(690); // Réinitialisation de xFond 2
- }
- Main.scene.setDx(1);
- } else if(e.getKeyCode() == KeyEvent.VK_LEFT){
- Main.scene.setDx(-1);
- }
- }
- @Override
- public void keyReleased(KeyEvent e) {Main.scene.setDx(0);} // relache la touche
- @Override
- public void keyTyped(KeyEvent e) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement