Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    Problema gioco che sto creando

    Ragazzi ciao a tutti, sto creando un gioco sullo stiele di space invader... il mio problema sta nelal gestione dell'evento fuoco, ovvero quando l'aereo nel mio caso dovrebbe sparar un colpo.
    Ho creato un mouse pressed event che richiama una procedura fuoco...questa procedura praticamente dovrebbe fare questo: far avanzare il missile attraverso un ciclo nelal direzione giusta fin a quando nn incontra un aereo nemico ed allora fermarsi.... a me invece il fuoco continua a continuare ripartendo da sotto del frame , oppure si blocca quasi sul limite superiore del frame che contiene l'area di gioco scomparendo quando incontra un aereo, ma ricomparendo direttamente lì quando si ripreme sul mouse per sparare, invece da partire dall aereo come dovrebbe essere....
    questo è il codice:

  2. #2
    codice:
    package gioco.file;
    
    import gioco.file.*;
    import java.awt.Dimension;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.Toolkit;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    
    /**
     *
     * @author Giancarlo Mennillo Classe V S
     */
    
    public class Game2 extends javax.swing.JFrame {
    
    
          private class Aerei extends Thread
          {
            @Override
            public void run()
            {
                Energia.setValue(100);
    
                boolean control=true;
                while (control)
                {
                    for (int j = 0; j < 7; j++)
                        {
                            if (nemici[j].getLocation().y >= 800)
                            {
                                nemici[j].setLocation(nemici[j].getLocation().x, -10);
                            }
    
                            nemici[j].setLocation(nemici[j].getLocation().x, nemici[j].getLocation().y + 1);
                            Impattofuocomio(nemici[j]);
                                                  
                            if(Winner())
                            {
                                win.setVisible(true);
                                control=false;
                            }
                         }
            
                        try
                        {
                            sleep(12);//velocità con cui scendono gli aerei
                        }
                        catch (InterruptedException ex) { }
                }
             }
           }
    
        private class Fuoco extends Thread
        {
    
            @Override
            // in questa classe gestiamo anke il fatto di lanciare il missile
            public void run()
            {
                fuoco.setLocation(fuoco.getLocation().x - 800, fuoco.getLocation().y);
               while(colpito(nemici,fuoco.getLocation())==false)
               {
                  for (int i = 0; i < 800; i++)
                  {
                    fuoco.setVisible(true);
    
                    
                      /*if fuoco.getLocation().y >= 800)
                      {
                      fuoco].setLocation(fuoco.getLocation().x, -10);
                      }*/
                 
    
                    fuoco.setLocation(fuoco.getLocation().x, fuoco.getLocation().y +1);
    
                    }
                    
                    
                    try
                    {
                        sleep(12);//velocità fuoco
                    }
                        
                   catch (InterruptedException ex) { }
              }
         
                fuoco.setVisible(false);
                addMouseListener(new Mouse());
            }
        }
    
    
    
        public boolean colpito(JLabel pNemico[], Point fuocox)
        {
            boolean colpitos=false;
            for (int i = 0; i < 7; i++)
            {
                Point nemix = pNemico[i].getLocation();
                Dimension fuocoD= this.fuoco.getSize();
                Dimension nemicoD= pNemico[i].getSize();
                Rectangle fuocoR= new Rectangle(fuocox, fuocoD);
                Rectangle nemicoR= new Rectangle(nemix, nemicoD);
                if (fuocoR.intersects(nemicoR) ==true)
                {
                    colpitos = true;
                }
           }
            return colpitos;
        }
    
    
        private class Mouse extends MouseAdapter
        {
            @Override
            public void mouseMoved(MouseEvent e)
            {
                int x = e.getPoint().x;
                aereo.setLocation(x - aereo.getSize().height / 2,aereo.getLocation().y);
                fuoco.setLocation(x - fuoco.getSize().height / 2,fuoco.getLocation().y);
            }
    
            @Override
            public void mouseClicked(MouseEvent e)
            {
                Fuoco f = new Fuoco();
                f.start();
            }
        }
    
        public JLabel[] nemici;
    
        public boolean impatto(JLabel pNemico)
        {
            
            Dimension fuocoD= this.fuoco.getSize();
            Dimension nemicoD= pNemico.getSize();
            Point locFuoco= this.fuoco.getLocation();
            Point locNemico= pNemico.getLocation();
            Rectangle fuocoR= new Rectangle(locFuoco, fuocoD);
            Rectangle nemicoR= new Rectangle(locNemico, nemicoD);
            return fuocoR.intersects(nemicoR);
        }
    
        private void Impattofuocomio(JLabel pNemico)
        {
            if(this.impatto(pNemico))
            {
                pNemico.setIcon(new ImageIcon(getClass().getResource("/gioco/file/images/scoppio1.gif")));
                pNemico.setVisible(false);
                String k= lbScoreV.getText();
                int x=Integer.parseInt(k);
                x=x+10;
                k=Integer.toString(x);
                lbScoreV.setText(k);
    
    
            }
            
        }
    
        private boolean Winner()
        {
            int cont=0;
           
                for (int j = 0; j < 7; j++)
                {
                    if(!this.nemici[j].isVisible())
                    {
                        cont++;                    
                    }
                    
                }
         
            return cont==7;
        }
    
        private void initLabel()
        {
    
           this.nemici = new JLabel[7];
           for (int i = 0; i < 7; i++)
           {
                this.nemici[i] = new JLabel(new ImageIcon(getClass().getResource("/gioco/file/images/nemico.png")));
                this.nemici[i].setVisible(true);
                this.nemici[i].setSize(85, 55);
                this.nemici[i].setLocation((i * 79), -80);
                this.jPanel1.add(this.nemici[i]);
           }
    
        }

  3. #3
    Lo vorrei all'esame quindi se avete qualche consiglio da darmi o qulche miglioramento che potrei fare, oltre al modo in cui risolvere il problema di cui vi ho parlato, sono ben disposto ad ascoltare i vostri suggerimenti....anche se ora come ora il problema maggiore è quello del fuoco che nn funziona...

  4. #4
    posta tutti i file, essendo in un package.

    cmq penso che dovresti usare un mouseClicked e non pressed
    I miei esempi in Java qui: http://cdpjavaexamples.altervista.org/index.html

  5. #5
    Si alla fine l'ho cambiato in un mouseClicked come hai potuto vedere dal codice che ho postato... cmq nn è tutto il codice continua:
    codice:
    private void initLabel()
        {
    
           this.nemici = new JLabel[7];
           for (int i = 0; i < 7; i++)
           {
                this.nemici[i] = new JLabel(new ImageIcon(getClass().getResource("/gioco/file/images/nemico.png")));
                this.nemici[i].setVisible(true);
                this.nemici[i].setSize(85, 55);
                this.nemici[i].setLocation((i * 79), -80);
                this.jPanel1.add(this.nemici[i]);
           }
    
        }
    
        public void energya()
        {
    
          int a= Energia.getValue();
          if (a==0)
          {
             String y= lbLifeV.getText();
             int x=Integer.parseInt(y);
             x=x-1;
             y=Integer.toString(x);
             lbLifeV.setText(y);
          }
        }
    
    
    
        public Game2()
        {
            
            initComponents();
            this.win.setVisible(false);
            this.fuoco.setVisible(false);
            initLabel();
            this.addMouseMotionListener(new Mouse());
            this.addMouseListener(new Mouse());
            new Aerei().start();
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jPanel1 = new javax.swing.JPanel();
            aereo = new javax.swing.JLabel();
            fuocherello = new javax.swing.JLabel();
            win = new javax.swing.JLabel();
            fuoco = new javax.swing.JLabel();
            lbScore = new javax.swing.JLabel();
            lbScoreV = new javax.swing.JLabel();
            lbLife = new javax.swing.JLabel();
            lbLifeV = new javax.swing.JLabel();
            Energia = new javax.swing.JProgressBar();
            lbenergy = new javax.swing.JLabel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Aereo battle- by Giancarlo Mennillo");
            setResizable(false);
    
            jPanel1.setBackground(new java.awt.Color(0, 0, 204));
            jPanel1.setMinimumSize(new java.awt.Dimension(894, 745));
            jPanel1.setPreferredSize(new java.awt.Dimension(894, 745));
    
            aereo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gioco/file/images/aereo.png"))); // NOI18N
    
            win.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gioco/file/images/win.png"))); // NOI18N
    
            fuoco.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gioco/file/images/fuoco.png"))); // NOI18N
    
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(fuocherello)
                    .addGap(845, 845, 845))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(263, 263, 263)
                    .addComponent(fuoco)
                    .addContainerGap(559, Short.MAX_VALUE))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(247, 247, 247)
                    .addComponent(aereo)
                    .addContainerGap(541, Short.MAX_VALUE))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(120, 120, 120)
                    .addComponent(win)
                    .addContainerGap(435, Short.MAX_VALUE))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(win)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 254, Short.MAX_VALUE)
                    .addComponent(fuoco)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(aereo)
                    .addGap(23, 23, 23)
                    .addComponent(fuocherello))
            );
    
            lbScore.setFont(new java.awt.Font("Tahoma", 0, 24));
            lbScore.setText("Score:");
    
            lbScoreV.setFont(new java.awt.Font("Tahoma", 0, 24));
            lbScoreV.setText("0");
    
            lbLife.setFont(new java.awt.Font("Tahoma", 0, 24));
            lbLife.setText("Lives:");
    
            lbLifeV.setFont(new java.awt.Font("Tahoma", 0, 24));
            lbLifeV.setText("3");
    
            Energia.setForeground(new java.awt.Color(255, 0, 51));
    
            lbenergy.setFont(new java.awt.Font("Tahoma", 0, 24));
            lbenergy.setText("Energy:");
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addGap(26, 26, 26)
                    .addComponent(lbScore)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(lbScoreV)
                    .addGap(119, 119, 119)
                    .addComponent(lbLife)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(lbLifeV)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 67, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(lbenergy)
                            .addGap(63, 63, 63))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(Energia, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())))
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 562, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(lbenergy)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(Energia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbScore, javax.swing.GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE)
                            .addComponent(lbScoreV, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lbLife, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lbLifeV, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 590, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
    
         public static void main(String args[])
         {
               java.awt.EventQueue.invokeLater(new Runnable()
               {
                public void run()
                {
                    
                    Game2 a=new Game2();
                    Dimension screen= Toolkit.getDefaultToolkit().getScreenSize();
                    int x=(screen.width-a.getSize().width)/2;
                    int y=(screen.height-a.getSize().height)/2;
                    a.setLocation(x, y);
                    a.setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JProgressBar Energia;
        private javax.swing.JLabel aereo;
        private javax.swing.JLabel fuocherello;
        private javax.swing.JLabel fuoco;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JLabel lbLife;
        public static javax.swing.JLabel lbLifeV;
        private javax.swing.JLabel lbScore;
        public static javax.swing.JLabel lbScoreV;
        private javax.swing.JLabel lbenergy;
        private javax.swing.JLabel win;
        // End of variables declaration                   
    }
    gli altri file del package altro non sono ke le immagini .png che ho usato per creare gli aerei, il fuoco, gli aerei nemici, e dei palloncini che compaiono quando si vince....in realtà è un solo file java però ho creato un package nel caso avessi avuto bisogno di aggiungere dei file....

  6. #6

    nuovo problema: gestione IA

    ciao a tutti!!!!
    sono riuscito comunque a risolvere il mio problema...in realtà è stato un errore mio più che altro poichè non mi sono accorto che nel ciclo in cui doveva avanzare il fuoco avevo messo come valore cui doveva arrivare la i la larghezza e non l'altezza e qnd essendo quest'ultima più piccola il fuoco si bloccava a metà schermo, oppure avanzava più del dovuto ripartendo dalla base 2/3 volte perchè il valore che avevo dato era sballato...comunque ora ho deciso di realizzare il gioco in orizzontale come accadeva in space invader e non più in verticale ( infatti ho fatto subito perchè dovevo solo scambiare i valori base e altezza nelle giuste parti di codice) ora però è sorto un altro problema, quello del fuoco nemico e quindi della gestione dell' IA...qualcuno può darmi una mano??? avrei bisogno di urgente aiuto dato che lo devo finire per l'esame...

    secondo voi dovrei metterlo in una nuova discussione dato che riguarda un altro tipo di problema?

  7. #7
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,327

    Re: nuovo problema: gestione IA

    Originariamente inviato da Jack&Amaretto
    secondo voi dovrei metterlo in una nuova discussione dato che riguarda un altro tipo di problema?
    Sì.

    E, aggiungo, puoi chiedere aiuto su del codice che tu hai realizzato (quindi, problematiche mirate), non chiedere la realizzazione di codice (o parte di esso) completo (come indicato nel regolamento interno a queste sezioni).


    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

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.