Visualizzazione dei risultati da 1 a 6 su 6

Discussione: Problema gestione IA

  1. #1

    Problema gestione IA

    Ciao a tutti

    Durante la creazione del gioco è sorto un altro problema, quello del fuoco nemico e quindi della gestione dell' IA...qualcuno può darmi una mano???
    Io avevo pensato una cosa di questo tipo:

    codice:
    private JLabel fuocon;
    
        private class Game extends Thread
        {
            @Override
            public void run()
            {
                Energia.setValue(100);
                boolean control=true;
                boolean attacco=false;
                while (control)
                {
                    for (int c = 0; c < h; c++)
                    {
                        
    
                        if (nemico[c].getLocation().x <= 0)
                        {
                            nemico[c].setLocation(800, nemico[c].getLocation().y);
                        }
    
                        nemico[c].setLocation(nemico[c].getLocation().x - 1 , nemico[c].getLocation().y);
    
                       FuocoNemico(nemico[c]);
                       
    
                    }
                    try
                    {
                        sleep(10);//velocità aerei nemici
                    }
                    catch (InterruptedException ex) {  }
    
                    /*if(Winner())
                    {
                        win.setVisible(true);
                        control=false;
                    }*/
    
    
               }
             }
    
    
        }
    
    
    
    
    
        private void FuocoNemico(JLabel nem)
        {
            boolean attacco=false;
            int vof = (int) ((1-0+1)*Math.random());
            switch (vof)
            {
                case 0:attacco=true;
                break;
                case 1:attacco=false;
                break;
            }
            if (attacco)
            {
                fuocon = new JLabel(new ImageIcon(getClass().getResource("/game/graphics/images/fuoco nemico.png")));
                fuocon.setLocation(nem.getLocation().x-40, nem.getLocation().y);
                fuocon.setSize(60, 100);
                jPanel1.add(fuocon);
                fuocon.setVisible(true);
                Point loc = fuocon.getLocation();
                for (int i = 0; i < 400; i++)
                {
                  fuocon.setLocation(fuocon.getLocation().x - 2, fuocon.getLocation().y);
                }
            }
    
        }
    il fuoco parte però non va verso sinistra come dovrebbe fare in :

    codice:
     
    
    for (int i = 0; i < 400; i++)
    {
        fuocon.setLocation(fuocon.getLocation().x - 2, fuocon.getLocation().y);
    }
    ma rimane vicino all' aereo per tutto il suo proseguiemnto...

    L'errore forse sta nel passare le coordinate dellaereo, dato che quest'ultimo essendo in movimento le fà variare continuamente ? Come potrei risolvere nel caso questo problema?
    Oppure non c'entrano niente le coordinate e il problema è da tutt'altra parte?

    Comunque se avete un consiglio/idea per una migliore gestione dell IA ditemi sono tutto orecchie

  2. #2
    Qualcuno può darmi una risposta possibilmente prima che inizino gli esami??

  3. #3
    Utente di HTML.it L'avatar di bako
    Registrato dal
    Feb 2004
    Messaggi
    1,797
    codice:
    if (attacco)
            {
                fuocon = new JLabel(new ImageIcon(getClass().getResource("/game/graphics/images/fuoco nemico.png")));
                fuocon.setLocation(nem.getLocation().x-40, nem.getLocation().y);
                fuocon.setSize(60, 100);
                jPanel1.add(fuocon);
                fuocon.setVisible(true);
                Point loc = fuocon.getLocation();
                for (int i = 0; i < 400; i++)
                {
                  fuocon.setLocation(fuocon.getLocation().x - 2, fuocon.getLocation().y);
                }
            }
    mi sa che entra sempre in attacco e quindi fa
    fuocon.setLocation(nem.getLocation().x-40, nem.getLocation().y);

    prova a mettere un attacco = false a fine del for (int i = 0; i < 400; i++)

  4. #4
    Grazie Bako proverò a seguire il tuo consiglio

  5. #5
    Utente di HTML.it L'avatar di bako
    Registrato dal
    Feb 2004
    Messaggi
    1,797
    Originariamente inviato da Jack&Amaretto
    Grazie Bako proverò a seguire il tuo consiglio
    riguardando non funziona.

  6. #6
    Alla fine ho trovato questa soluzione che sembra funzionare :

    codice:
    private class Game extends Thread
        {
            @Override
            public void run()
            {
                Energia.setValue(100);
                boolean control=true;
                boolean z;
         
                while (control)
                {
                   for (int c = 0; c < h; c++)
                   {
                       z=Colpitoame();
                       
                       if((nemico[c].isVisible())&&(z==false))
                       {
                         fuocon[c].setVisible(true);
                       }
    
                       if (nemico[c].getLocation().x <= 0)
                       {
                          nemico[c].setLocation(800, nemico[c].getLocation().y);
                          fuocon[c].setLocation(nemico[c].getLocation().x, nemico[c].getLocation().y+40);
    
                          if((nemico[c].isVisible())&&(z==false))
                          {
                              fuocon[c].setVisible(true);
                          }
    
                       }
    
                      if (fuocon[c].getLocation().x <= 0)
                      {
                          fuocon[c].setLocation(nemico[c].getLocation().x, nemico[c].getLocation().y+40);
                      }
    
                     nemico[c].setLocation(nemico[c].getLocation().x - 1 , nemico[c].getLocation().y);
      
                     
                     if (z==false)
                     {
                       fuocon[c].setLocation(fuocon[c].getLocation().x - 4 , fuocon[c].getLocation().y);
                     }
                     
                     
    
    
    
    
                  }
    
                  try
                   {
                     sleep(10);//velocità aerei nemici
                   }
                  catch (InterruptedException ex) {  }
               }
    
             }
    
        }
    codice:
    private JLabel[] nemico;
        private JLabel[] fuocon;
        public int h;
    
        private void Avversari()
        {
            int x,y;
    
            h = (int) ((6-2)*Math.random()+1);
            this.nemico = new JLabel[h];
            this.fuocon = new JLabel[h];
    
            for (int i = 0; i < h; i++)
            {
                this.nemico[i] = new JLabel(new ImageIcon(getClass().getResource("/game/graphics/images/nemico.png")));
                this.nemico[i].setVisible(true);
    
                this.fuocon[i] = new JLabel(new ImageIcon(getClass().getResource("/game/graphics/images/fuoco nemico.png")));
                this.fuocon[i].setVisible(false);
    
    
    
                this.nemico[i].setSize(60, 75);
                this.fuocon[i].setSize(40, 10);
    
                y = (int) ((430-0+1)*Math.random());
                this.nemico[i].setLocation(700 , y);
                this.fuocon[i].setLocation(nemico[i].getLocation().x  , nemico[i].getLocation().y +40);
    
                this.jPanel1.add(this.nemico[i]);
                this.jPanel1.add(this.fuocon[i]);
    
            }
    
        }
    Voi che ne pensate ?
    (ho modificato la procedura avversari aggiungendo al suo interno anche il fuoco nemico che è un label come i nemici, poi in Game lo faccio avanzare insieme ai nemici)

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.