elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: (TUTORIAL) Aprende a emular Sentinel Dongle By Yapis


  Mostrar Mensajes
Páginas: [1]
1  Programación / Java / Me Falta Un Menu Para juego en: 22 Julio 2010, 06:48 am
Bueno al fin termine el juego de batalla naval
pero  ahora necesito hcer un menu de la dificultad
tiene que ser una ventana para q elija la dificultad (facil medio dificil)

bueno el juego ya lo tengo pero no se hacer ese menu
les agradeceria mucho si me ayudaran
ese es el juego
esta en facil
al elegir supuestamente medio se disminuyen los disparos...
Código
  1. package combate;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. import java.io.IOException;
  6.  
  7. import javax.swing.JFrame;
  8.  
  9. import javax.swing.JOptionPane;
  10. public class combate {
  11.  
  12. public static void main(String[] args) throws IOException {
  13.  
  14.  
  15.        int puntero = 12;
  16.        JFrame f = new JFrame();
  17.  
  18.        PanelCombate panel = new PanelCombate();
  19.        f.add(panel);
  20.  
  21.  
  22.  
  23.        f.addWindowListener(new WindowAdapter() {
  24.  
  25.            public void windowClosing(WindowEvent we) {
  26.                System.exit(0);
  27.            }
  28.        });
  29.  
  30.        f.setTitle("Combate Naval ");
  31.        f.setCursor(puntero);
  32.  
  33.        f.setSize(450, 450);
  34.        f.show();
  35.  
  36.    }
  37. }
  38. class PanelCombate extends Panel {
  39.  
  40.    Casilla tablero[][];
  41.    boolean naves [][];
  42.    boolean maquina[][];
  43.    int pos, disparos, fallos,aciertos = 0;
  44.  
  45.    PanelCombate() {
  46.        int f, c;
  47.        setLayout(new GridLayout(11, 11));
  48.        tablero = new Casilla[11][11];
  49.        naves = new boolean[11][11];
  50.        pos = seleccion();
  51.  
  52.        for (f = 0; f < 11; f++) {
  53.            for (c = 0; c < 11; c++) {
  54.                naves[f][c] = false;
  55.                tablero[f][c] = new Casilla("");
  56.                add(tablero[f][c]);
  57.                tablero[f][c].addActionListener(new AccionBoton(this, c, f));
  58.            }
  59.        }
  60.        for (int i = 1; i <= 3; i++) {
  61.            int tipo = (int) (Math.random() * 2);
  62.            f = (int) (Math.random() * 11);
  63.            c = (int) (Math.random() * 11);
  64.            switch (i) {
  65.                case 1:
  66.                    try{
  67.                    if (tipo > 2) {
  68.                        if (f == 11) {
  69.                            naves[f - 1][c] = true;
  70.                        } else {
  71.                            naves[f + 1][c] = true;
  72.                        }
  73.                    } else {
  74.                        if (c == 11) {
  75.                            naves[f][c - 1] = true;
  76.                        } else {
  77.                            naves[f][c + 1] = true;
  78.                        }
  79.                    }}
  80.                    catch(ArrayIndexOutOfBoundsException q){
  81.  
  82.                    }
  83.                    break;
  84.                case 2:
  85.                    try{
  86.                    if (tipo > 2) {
  87.                        if (f == 9) {
  88.                            naves[f - 1][c] = true;
  89.                            naves[f - 2][c] = true;
  90.                            naves[f - 3][c] = true;
  91.                        } else {
  92.                            naves[f + 1][c] = true;
  93.                            naves[f + 2][c] = true;
  94.                            naves[f + 3][c] = true;
  95.                        }
  96.                    } else {
  97.                        if (c == 9) {
  98.                            naves[f][c - 1] = true;
  99.                            naves[f][c - 2] = true;
  100.                            naves[f][c - 3] = true;
  101.                        } else {
  102.                            naves[f][c + 1] = true;
  103.                            naves[f][c + 2] = true;
  104.                            naves[f][c + 3] = true;
  105.                        }
  106.                    }
  107.                    break;
  108.                    }
  109.                    catch(ArrayIndexOutOfBoundsException w){
  110.  
  111.                    }
  112.                case 3:
  113.                    try{
  114.                    if (f == 9) {
  115.                        naves[f - 1][c] = true;
  116.                        naves[f - 1][c + 1] = true;
  117.                    } else {
  118.                        naves[f + 1][c] = true;
  119.                        naves[f + 1][c - 1] = true;
  120.                    }
  121.                    break;
  122.            }
  123.                    catch(ArrayIndexOutOfBoundsException q){
  124.  
  125.            }
  126.            }
  127.  
  128.            naves[f][c] = true;
  129.        }
  130.    }
  131.    private int seleccion() {
  132.  
  133.        int a = 9;
  134.        return a;
  135.    }
  136.  
  137.    class Casilla extends Button {
  138.  
  139.        boolean aciertos;
  140.  
  141.        Casilla(String t) {
  142.            super.setLabel("");
  143.            setBackground(new Color(250, 210, 250));
  144.            aciertos = false;
  145.        }
  146.    }
  147.  
  148.    class AccionBoton implements ActionListener {
  149.  
  150.        PanelCombate pc;
  151.        int vert, hori, cant = 0;
  152.  
  153.        AccionBoton(PanelCombate p, int v, int h) {
  154.            pc = p;
  155.            vert = v;
  156.            hori = h;
  157.        }
  158.  
  159.        public void actionPerformed(ActionEvent ae) {
  160.            boolean salida = false;
  161.  
  162.            if (!pc.tablero[hori][vert].aciertos) {
  163.                pc.tablero[hori][vert].aciertos = true;
  164.                pc.tablero[hori][vert].setBackground(new Color(0));
  165.                pc.disparos++;
  166.                if (pc.naves[hori][vert]) {
  167.                    pc.tablero[vert][hori].setLabel("");
  168.                    for (int x = 0; x < 11; x++) {
  169.                       for (int y = 0; y < 11; y++) {
  170.                            if (pc.naves[x][y]) {
  171.                                if (x == hori && y == vert) {
  172.                                    pc.tablero[x][y].setBackground(new Color(0,0,210));
  173.                                    pc.tablero[x][y].setLabel("");
  174.                                    pc.aciertos++;
  175.                                }
  176.                            }
  177.                        }
  178.                    }
  179.  
  180.                }
  181. pc.fallos = disparos - aciertos;
  182.                if (pc.aciertos >=9){
  183.                    JOptionPane.showMessageDialog(pc.getParent(), "Ganaste");
  184.                    System.exit(0);
  185.                }
  186.                if ((pc.disparos >= 6 && pc.aciertos == 0) || (pc.disparos >= 24)) {
  187.  
  188.                    JOptionPane.showMessageDialog(pc.getParent(), "Se te han acabado los disparos");
  189.                         JOptionPane.showMessageDialog(pc.getParent(), "Obtuviste "+aciertos+" aciertos y " +fallos+ " fallas " + " de un total de " +disparos+ " disparos ");
  190.                    if(JOptionPane.showConfirmDialog(pc.getParent(), "Desea Reiniciar el juego","Aviso", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION){
  191.                          if(salida != true){
  192.                         System.exit(0);
  193.                        }else{
  194.  
  195.  
  196.                        }
  197.                    }
  198.                }
  199.  
  200.  
  201.            }
  202.        }
  203.    }
  204. }
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines