Exemplo n.º 1
0
        private void crearBarcos()
        {
            //Creación de los barcos
            //Porta aviones
            Barco portaAviones = new Barco(4, "Portaaviones XRT", "./../../img/spritesBarcos/Portaaviones/portaaviones");

            barcos.Add(portaAviones);

            //Submarinos
            Barco submarino1 = new Barco(3, "Submarino X1", "../../img/barco.jpg");
            Barco submarino2 = new Barco(3, "Submarino X2", "../../img/barco.jpg");

            barcos.Add(submarino1);
            barcos.Add(submarino2);

            //Destructores
            Barco destructor1 = new Barco(2, "Destructor R1", "../../img/barco.jpg");
            Barco destructor2 = new Barco(2, "Destructor R2", "../../img/barco.jpg");
            Barco destructor3 = new Barco(2, "Destructor R3", "../../img/barco.jpg");

            barcos.Add(destructor1);
            barcos.Add(destructor2);
            barcos.Add(destructor3);

            // Fragatas
            Barco fragata1 = new Barco(1, "Fragata T1", "../../img/barco.jpg");
            Barco fragata2 = new Barco(1, "Fragata T2", "../../img/barco.jpg");
            Barco fragata3 = new Barco(1, "Fragata T3", "../../img/barco.jpg");
            Barco fragata4 = new Barco(1, "Fragata T4", "../../img/barco.jpg");

            barcos.Add(fragata1);
            barcos.Add(fragata2);
            barcos.Add(fragata3);
            barcos.Add(fragata4);
        }
Exemplo n.º 2
0
        private void celda_Click(object sender, EventArgs e)
        {
            PictureBox pictures = sender as PictureBox;

            //Array que contiene datos sobre la celda (coordenadas X/Y, si esta ocupado, etc.)
            String[] tagPicture     = pictures.Tag.ToString().Split('#');
            Barco    barcoAEliminar = null;

            if (pictures == null)
            {
                return;
            }
            if (atacar)
            {
                //Si le das demasiado rápido
                if (timerDisparo.Enabled)
                {
                    return;
                }

                //Si ya hemos disparado en la celda
                if (tagPicture[3] == "Dado")
                {
                    sfx.SoundLocation = "Sound\\Effects\\yaDado.wav";
                    sfx.Play();
                    return;
                }
                ;

                this.Cursor = new Cursor("../../icons/hitmarker.ico");
                timerCur.Start();

                //Si hemos disparado al agua
                if (tagPicture[0] == "A")
                {
                    sfx.SoundLocation = "Sound\\Effects\\darAgua.wav";
                    sfx.Play();
                    timerDisparo.Interval = 1050;
                    timerDisparo.Start();
                    j1.Tiros--;
                    j1.Fallos++;
                    lbl_TotalFallos.Text = j1.Fallos.ToString();
                    lbl_TotalTiros.Text  = j1.Tiros.ToString();
                    pictures.Image       = Image.FromFile("./../../img/miss.gif");
                    pictureChange        = pictures;
                    timerExplosionAgua.Start();
                    pictures.Tag = tagPicture[0] + "#" + tagPicture[1] + "#" + tagPicture[2] + "#" + "Dado";
                    comprobarPartida();
                    return;
                }

                timerDisparo.Interval = 1400;
                timerDisparo.Start();

                //Si hemos llegado aqui, hemos disparado a un barco

                //Sonido de disparo
                sfx.SoundLocation = "Sound\\Effects\\explosion.wav";
                sfx.Play();

                int counTemp;
                barcos.ForEach(a =>
                {
                    if (a.Name == tagPicture[0])
                    {
                        a.Vidas--;
                        pictures.Tag   = tagPicture[0] + "#" + tagPicture[1] + "#" + tagPicture[2] + "#" + "Dado";
                        pictures.Image = Image.FromFile("./../../img/explosion.gif");


                        pictureChange = pictures;
                        if (a.Vidas != 0)
                        {
                            timerExplosion.Start();
                        }


                        j1.Aciertos++;
                        lbl_TotalAciertos.Text = j1.Aciertos.ToString();
                    }

                    if (a.Vidas == 0)
                    {
                        counTemp = a.Tamaño;
                        foreach (Control control in tableLayoutPanel1.Controls.Cast <Control>()
                                 .OrderBy(c => Int32.Parse(c.Name.Substring(10))))

                        {
                            PictureBox picture   = control as PictureBox;
                            String[] tagPicture2 = picture.Tag.ToString().Split('#');

                            if (picture == null)
                            {
                                return;
                            }
                            if (a.Name == tagPicture2[0])
                            {
                                if (a.Vertical)
                                {
                                    switch (a.Tamaño)
                                    {
                                    case 1:

                                        text             = "./../../img/spritesBarcos/Fragata/fragataD.png";
                                        picture.SizeMode = PictureBoxSizeMode.Zoom;
                                        picture.Image    = Image.FromFile(text);
                                        counTemp--;
                                        break;

                                    case 2:

                                        text             = "./../../img/spritesBarcos/Destructor/destructorD" + counTemp + ".png";
                                        picture.SizeMode = PictureBoxSizeMode.Zoom;
                                        picture.Image    = Image.FromFile(text);
                                        counTemp--;
                                        break;

                                    case 3:

                                        text             = "./../../img/spritesBarcos/Submarino/submarinoD" + counTemp + ".png";
                                        picture.SizeMode = PictureBoxSizeMode.Zoom;
                                        picture.Image    = Image.FromFile(text);
                                        counTemp--;
                                        break;

                                    case 4:

                                        text             = "./../../img/spritesBarcos/Portaaviones/portaavionesD" + counTemp + ".png";
                                        picture.SizeMode = PictureBoxSizeMode.StretchImage;
                                        picture.Image    = Image.FromFile(text);
                                        counTemp--;
                                        break;

                                    default:
                                        Image img        = Image.FromFile("../../img/barco.jpg");
                                        picture.SizeMode = PictureBoxSizeMode.Zoom;
                                        picture.Image    = img;
                                        break;
                                    }
                                    barcoAEliminar = a;
                                }
                                else
                                {
                                    switch (a.Tamaño)
                                    {
                                    case 1:

                                        Bitmap fragata   = new Bitmap("./../../img/spritesBarcos/Fragata/fragataD.png");
                                        picture.SizeMode = PictureBoxSizeMode.Zoom;
                                        fragata.RotateFlip(RotateFlipType.Rotate270FlipNone);
                                        picture.Image = fragata;
                                        counTemp--;
                                        break;

                                    case 2:

                                        Bitmap submarino = new Bitmap("./../../img/spritesBarcos/Destructor/destructorD" + counTemp + ".png");
                                        picture.SizeMode = PictureBoxSizeMode.Zoom;
                                        submarino.RotateFlip(RotateFlipType.Rotate270FlipNone);
                                        picture.Image = submarino;
                                        counTemp--;
                                        break;

                                    case 3:

                                        Bitmap destructor = new Bitmap("./../../img/spritesBarcos/Submarino/submarinoD" + counTemp + ".png");
                                        picture.SizeMode  = PictureBoxSizeMode.Zoom;
                                        destructor.RotateFlip(RotateFlipType.Rotate270FlipNone);
                                        picture.Image = destructor;
                                        counTemp--;
                                        break;

                                    case 4:

                                        Bitmap portaaviones = new Bitmap("./../../img/spritesBarcos/Portaaviones/portaavionesD" + counTemp + ".png");
                                        picture.SizeMode    = PictureBoxSizeMode.StretchImage;
                                        portaaviones.RotateFlip(RotateFlipType.Rotate270FlipNone);
                                        picture.Image = portaaviones;
                                        counTemp--;
                                        break;

                                    default:

                                        Image img        = Image.FromFile("../../img/barco.jpg");
                                        picture.SizeMode = PictureBoxSizeMode.Zoom;
                                        picture.Image    = img;
                                        break;
                                    }
                                    barcoAEliminar = a;
                                }
                            }
                        }
                    }
                });
                //this.Cursor = new Cursor("../../icons/mira.ico");
                barcos.Remove(barcoAEliminar);
                comprobarPartida();
            }
            else
            {
                if (lw_Barcos.SelectedItems.Count == 0)
                {
                    MessageBox.Show("No hay un barco seleccionado.");
                }
                else
                {
                    // Comprueba si la casilla es agua para poder situar un barco
                    if (tagPicture[0] == "A")
                    {
                        int tamaño = Convert.ToInt32(lw_Barcos.SelectedItems[0].SubItems[1].Text); // Selecciona el tamaño del barco de la lista
                        if (!isVertical)
                        {
                            // Comprueba si hay espacio horizontal suficiente, y si lo hay, elimina al barco de la lista (Falta añadir barco al tablero. De momento solo añade la primera parte)
                            asignarBarco(lw_Barcos.SelectedItems[0].Text, tamaño, Convert.ToInt32(tagPicture[1]), Convert.ToInt32(tagPicture[2]), int.Parse(pictures.Name.Substring(10)));
                        }
                        else if (isVertical)
                        {
                            // Comprueba si hay espacio vertical suficiente, y si lo hay, elimina al barco d ela lista (Falta añadir barco al tablero. De momento solo añade la primera parte)
                            asignarBarco(lw_Barcos.SelectedItems[0].Text, tamaño, Convert.ToInt32(tagPicture[1]), Convert.ToInt32(tagPicture[2]), int.Parse(pictures.Name.Substring(10)));
                        }
                    }
                    else
                    {
                        MessageBox.Show("El rango de casillas seleccionado ya está ocupado.");
                    }
                }

                // Comprueba si la lista se ha vaciado
                if (lw_Barcos.Items.Count == 0)
                {
                    btn_atacar.Enabled = true;
                    btn_rotar.Enabled  = false;
                }
            }
        }