Partage
  • Partager sur Facebook
  • Partager sur Twitter

Evolution d'un score

    20 mai 2012 à 16:26:27

    Bonjour à tous,

    Voilà un moment que je n'arrive pas à faire évoluer un score pour un jeu.

    La situation est la suivante. Lorsque je déplace un élément (en l'occurrence un pion), je souhaite incrémenter de 1 la variable "Score" définie au départ, et placée dans le JtextField "sco". Or quelque soit la modification que j'essaye d'apporter (même simplement un Score++), la variable reste figée à 0... Comment puis je la faire changer, si possible lors d'un déplacement ?

    Voici le code correspondant:

    Classe fenetre
    package avat;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.event.*;
    import java.util.Scanner;
     import javax.swing.JTextField;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
    public class Fenetre extends JFrame implements KeyListener {
    	
    	
    	private Panneau pan = new Panneau(8,4);
            private int Score=0;
            private JTextField jtf = new JTextField("Score");
            private JTextField sco = new JTextField(""+Score);
           
    	public Fenetre () {}
    	         public Fenetre (String pseudo, int numJoueur) {
    	                
                         
    	                 this.setTitle("Animation");
    	                 this.setSize(600, 600);
    	                 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	                 this.setBackground (Color.white);
    	                 this.setLocationRelativeTo(null);
    	                 this.setContentPane(pan);
    	                 this.setVisible(true);
    	                 pan.addKeyListener(this);
    	                 pan.setPseudo(pseudo);
    	                 pan.setNumJoueur(numJoueur);
                             pan.add(jtf); 
                             pan.add(sco); 
    	                 
    	         }
    	        
    			@Override
    			 public void keyTyped(KeyEvent arg0) {  
                             
                                 
    			    }
    
                            @Override
    			    public void keyReleased(KeyEvent arg0) {
                                    
                                    
    			    }
    
    			    
    			    
    				@Override
    				public void keyPressed(KeyEvent event) {
    					// TODO Auto-generated method stub
    					
    				 	//Les coordonnées de départ de notre rond
                                       
                            int xPos = pan.getxPos(), yPos = pan.getyPos();
    	                int source = event.getKeyCode();
    	             
    	     
    	                           if( source==KeyEvent.VK_UP && xPos==1 &&yPos==2 ||
    	                               source==KeyEvent.VK_UP && xPos==2 &&yPos==2 ||   
    	                               source==KeyEvent.VK_UP && xPos==6 &&yPos==2 ||    
    	                               source==KeyEvent.VK_UP && xPos==7 &&yPos==2 ||
    	                               source==KeyEvent.VK_UP && xPos==1 &&yPos==8 ||
    	                               source==KeyEvent.VK_UP && xPos==2 &&yPos==8 ||   
    	                               source==KeyEvent.VK_UP && xPos==6 &&yPos==8 ||    
    	                               source==KeyEvent.VK_UP && xPos==7 &&yPos==8 ||
    	                               source==KeyEvent.VK_UP && xPos==3 &&yPos==6 ||
    	                               source==KeyEvent.VK_UP && xPos==4 &&yPos==6 ||
    	                               source==KeyEvent.VK_UP && xPos==5 &&yPos==6        
    	                                   )   
    	                               
    	                           { 
    	                             yPos=yPos+1;
    	                           }
    	                           else if(source==KeyEvent.VK_DOWN && xPos==1 &&yPos==0 ||
    	                                   source==KeyEvent.VK_DOWN && xPos==2 &&yPos==0 ||   
    	                                   source==KeyEvent.VK_DOWN && xPos==6 &&yPos==0 ||    
    	                                   source==KeyEvent.VK_DOWN && xPos==7 &&yPos==0 ||
    	                                   source==KeyEvent.VK_DOWN && xPos==1 &&yPos==6 ||
    	                                   source==KeyEvent.VK_DOWN && xPos==2 &&yPos==6 ||   
    	                                   source==KeyEvent.VK_DOWN && xPos==6 &&yPos==6 ||    
    	                                   source==KeyEvent.VK_DOWN && xPos==7 &&yPos==6 ||
    	                                   source==KeyEvent.VK_DOWN && xPos==3 &&yPos==2 ||
    	                                   source==KeyEvent.VK_DOWN && xPos==5 &&yPos==2 ||
    	                                   source==KeyEvent.VK_DOWN && xPos==4 &&yPos==4)
    	                               
    	                           {
    	                              
    	                               yPos=yPos-1;
    	                       } 
    	                           
    	                           else if(source==KeyEvent.VK_RIGHT && xPos==0 &&yPos==1||
    	                                   source==KeyEvent.VK_RIGHT && xPos==0 &&yPos==7||
    	                                   source==KeyEvent.VK_RIGHT && xPos==5 &&yPos==1||
    	                                   source==KeyEvent.VK_RIGHT && xPos==5 &&yPos==7||
    	                                   source==KeyEvent.VK_RIGHT && xPos==2 &&yPos==3||
    	                                   source==KeyEvent.VK_RIGHT && xPos==2 &&yPos==4||
    	                                   source==KeyEvent.VK_RIGHT && xPos==2 &&yPos==5||
    	                                   source==KeyEvent.VK_RIGHT && xPos==4 &&yPos==3||
    	                                   source==KeyEvent.VK_RIGHT && xPos==4 &&yPos==4)
    	                                    
    	                           {
    	                             xPos=xPos-1;
    	                             }
    	                           
    	                            else if(source==KeyEvent.VK_LEFT && xPos==3 &&yPos==1||
    	                                    source==KeyEvent.VK_LEFT && xPos==8 &&yPos==1||
    	                                    source==KeyEvent.VK_LEFT && xPos==3 &&yPos==7||
    	                                    source==KeyEvent.VK_LEFT && xPos==8 &&yPos==7||
    	                                    source==KeyEvent.VK_LEFT && xPos==6 &&yPos==3||
    	                                    source==KeyEvent.VK_LEFT && xPos==6 &&yPos==4||
    	                                    source==KeyEvent.VK_LEFT && xPos==6 &&yPos==5||
    	                                    source==KeyEvent.VK_LEFT && xPos==4 &&yPos==3||
    	                                    source==KeyEvent.VK_LEFT && xPos==4 &&yPos==4)
    	                                   
    	                           {
    	                             xPos=xPos+1;
    	                           } 
    	                              
    	            
    	                           // On intègre ici un code supprimant l'emplacement n-1 lors du déplacement du pion:  
    	                         
    	                          
    	                           
    	                           
    	                             
    	                //Section permettant de bloquer le joueur dans le tableau            
    	              switch (event.getKeyCode()) {
    	                 
    	                  case 38:
    	                      if(yPos > 0)
    	                          yPos = yPos - 1;
    	                      break;
    	                  case 40:
    	                      if(yPos < 8)
    	                          yPos = yPos + 1;
    	                      break;
    	                  case 39:
    	                      if(xPos < 8)
    	                          xPos = xPos + 1;
    	                      break;
    	                  case 37:
    	                      if(xPos > 0)
    	                          xPos = xPos - 1;
    	                      break;
    	                 
    	                 
    	              }
    	            
    	                
    	                pan.setxPos(xPos);
    	                pan.setyPos(yPos);
                            pan.setScore(Score);
    	                pan.repaint();
                            
    	                
    	               
    	                
    					
    				}
    	     
     
    }
    


    Classe panneau

    package avat;
    import java.awt.Graphics;
    import java.awt.Color;
    import java.awt.BasicStroke;
    import java.awt.Font;
    import java.awt.Image;
    import java.awt.Graphics2D;
    import java.awt.GradientPaint;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import java.lang.Object;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    
     
    public class Panneau extends JPanel {
        
     
    	 private int i;
    	    private int j;
    	    private int xPos;
    	    private int yPos;
    	    private int Score=0;
    	    private String pseudo;
    	    private int numJoueur=0;
    	   
    	    public Panneau(int x, int y){
    	        xPos = x;
    	        yPos = y;
    	        
    	 
    	        this.setFocusable(true);
    	        this.requestFocusInWindow();
              
    	    }
    	
                
    	    @Override
                
    	    public void paintComponent(Graphics g ) {
    
    	    	
    	    
    	    	
    	        for (i = 0; i <= 8; i++) {
    	            for (j = 0; j <= 8; j++) {
    	               
    	                 
    	            if (i==1 && j==1 || i==2 && j==1 || i==6 && j==1 || i==7 && j==1 ||
    	                j==3 && i==3 || j==3 &&i==5 ||
    	                j==4 && i==3 || j==4 &&i==5 ||
    	                j==5 && i==3 || j==5 &&i==5 || j==5 && i==4 || 
    	                i==1 && j==7 || i==2 && j==7 || i==6 && j==7 || i==7 && j==7){
    	                
    	                 
    	                 g.setColor(Color.gray);
    	                 g.fillRect(80+40 * i,40+ 40 * j, 40, 40);
    	                
    	                 
    	                
    	            }
    	            
    	            	     				
    	        
    	            else{
    	                 g.setColor(Color.blue);
    	                 g.fillRect(80+40 * i,40+ 40 * j, 40, 40);
    	                 g.setColor(Color.black);
    	                 g.drawRect(80+40 * i,40+ 40 * j, 40, 40);
    	                 g.setColor(Color.black);
    	                 g.drawOval(95+40 * i,55+ 40 * j, 10, 10);
    	                 
    	                
    	            }
    	            }
    	        }
    	        // on intègre le joueur vert:
    	       
    	        
    	        Font font = new Font("Courier", Font.BOLD, 20);
                
                    g.setFont(font);
    	        g.setColor(Color.black);
    	        g.drawString(pseudo, 10, 20);
    	      
    	        
    	        switch (numJoueur)
    	        {   case 1:
                g.setColor(Color.black);
    	    g.drawString(pseudo, 10, 20); 
                g.setColor(Color.green);
                g.fillOval(80+xPos*40, 40+yPos * 40, 40, 40);
               
    	         break;
    	         
    	        case 2: g.setColor(Color.red);
                g.fillOval(80+xPos*40, 40+yPos * 40, 40, 40);
    	         break;
    	         
    	        case 3: g.setColor(Color.yellow);
                g.fillOval(80+xPos*40, 40+yPos * 40, 40, 40);
    	         break;
    	         
    	        case 4: g.setColor(Color.black);
                g.fillOval(80+xPos*40, 40+yPos * 40, 40, 40);
    	         break;
    	        
    	                        
    	        }
                  
              
    	        
    	     
    
    	    }
                
              
    	 
              
              
              
              
              
    	        public int getxPos() {
    	                return xPos;
    	        }
    	 
    	        public void setxPos(int posX) {
    	                this.xPos = posX;
    	        }
    	 
    	        public int getyPos() {
    	                return yPos;
    	        }
    	 
    	        public void setyPos(int posY) {
    	                this.yPos = posY;
    	        }
                    
                    public void setScore(int score) {
                    this.Score=score;
                    }
    	        
    	        public void setPseudo(String Pseudo) {
                    this.pseudo=Pseudo;
            }
    		
    	        public void setNumJoueur(int NumJoueur) {
                    this.numJoueur=NumJoueur;
            }
    		
    
    
    	       
    
    	    public void keyTyped(KeyEvent arg0) {  
    	    }
    
    	    public void keyReleased(KeyEvent arg0) {
    	    }
    	}
    


    Classe avat

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package avat;
    
    /**
     *
     * @author Utilisateur
     */
    import java.util.Scanner;
    public class Avat {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args){
    		
    		Scanner sc = new Scanner(System.in);
    		
    		int numJoueur= 0;
    		System.out.println("Bonjour. Entrez votre pseudo:");
    		String pseudo= sc.nextLine();
    		numJoueur++;
    		Fenetre fen = new Fenetre (pseudo,numJoueur);
    		
            
            
            
    }   
    }
    


    Merci par avance de vos réponses,

    Cordialement
    • Partager sur Facebook
    • Partager sur Twitter
    Anonyme
      20 mai 2012 à 19:49:53

      Tu peux rajouter, ligne 154
      if ((xPos != pan.getxPos()) || (yPos != pan.getyPos()) // c-à-d, si on a bougé
          score++; // Avec un s minuscule car c'est une variable
      sco.setText(""+score); // Il faut mettre ton JTextField à jour, ce qui manquait à ton code, je pense
      
      • Partager sur Facebook
      • Partager sur Twitter

      Evolution d'un score

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown