Bienvenidos

En este blog se publicaran proyectos en c++, les doy la bienvenida y espero sea de ayuda para ustedes este espacio, no olviden comentar!

sábado, 22 de octubre de 2011

Juego de Snake en C# por Angel Soto

Ahora presento una aplicación que simula el juego de la serpiente que come y crece utilizando gráficos y manipulación de pixeles. El método del juego es similar al popular juego de Snake para móviles. 
Espero que sea de su agrado.


AQUÍ LES MUESTRO LA VISUALIZACIÓN DE LA FORMA





La siguiente imagen muestra los componentes usados.























COMPONENTES
button1;
button2;
button3;
button4;
button5;
button6;
button7;
button8;
button9;
button12;
button11;
button10
groupBox1;
radioButton3;
radioButton2;
radioButton1;
panel1;
panel2;
panel3;
panel4;
panel5;
panel6;
timer1;
timer2;
timer3;
timer4;
timer8;
timer5;
timer6;
timer7;

label1;
label2;
label3;
label4;
label5;
textBox1;
comboBox1;
comboBox2;
numericUpDown1;

Ahora va el código completo con sencillas descripciones de lo que hace cada instrucción.
//CODIGO-------------------------------------------------------------------------------


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Threading;


namespace ProyectoGraficacion
{
    public partial class Form1 : Form
    {
        bool band = true;
        Random ram = new Random();
        int contxy; //ACUMULADOR DE LA POSICION DE LA VIBORA EN EL EJE X
        contletra = 0;
        int Xcount; //ACUMULADOR DE LA POSICION DE LA VIBORA EN EL EJE Y
        int y;
        bool band1;
        int dificultad = 300;
     
     //ARREGLOS PARA GUARDAR COORDENADAS
     //DE LA POSICION DE LOS 4 PIXELES
     //QUE REPRESENTAN LA COMIDA DE LA VIBORA 

        int[,] comida = new int[10,2];//PIXEL 1
        int[,] comida2 = new int[10, 2];//PIXEL 2
        int[,] comida3 = new int[10, 2];//PIXEL 3
        int[,] comida4 = new int[10, 2];//PIXEL 4
        
        int PUNTOS;  //CONTADOR DE PUNTOS CADA VEZ QUE COME LA VIVORA
        int vivoraX; //POSICION EN X 
        int vivoraY; //POSICION EN Y
        int CX;  //AUXILIAR GUARDA LA COORDENADA DE LA COMIDA EN EL EJE X
        int CY;  //AUXILIAR GUARDA LA COORDENADA DE LA COMIDA EN EL EJE X
        int cont_comida;  //CONTADOR DE LA COMIDA QUE TOMA LA VIBORA

        int retraso; //RETRASO DE LA VIBORA 2
        int crecer = 10;//LONGITUD DE LA VIVORA
        bool a = true;  //ALTERNADOR DE DIRECCION 
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //PONEMOS INVISIBLES LOS BOTONES QUE CONTROLAN LA VIBORA 
        
            button4.Visible = false;
            button5.Visible = false;
            button6.Visible = false;
            button7.Visible = false;
            button9.Enabled = false;
        }


 //BOTON QUE MUSTRA UN TEXTO CON EFECTOS DE GRADIENTE Y ZOOM       
        private void button2_Click(object sender, EventArgs e)
        {
            int tam = int.Parse(numericUpDown1.Value.ToString());
            panel6.Visible = false;
            panel5.Visible = false;
            panel2.Visible = false;
            this.Refresh();
            timer1.Enabled = false;
            timer2.Enabled = false;
            timer3.Enabled = false;
            
            Graphics g = this.CreateGraphics();
            string cad = textBox1.Text;
            LinearGradientBrush colorizer = new LinearGradientBrush(new Point(20, 20), new Point(450, 20), Color.White, Color.DarkRed);
            for (int i = 0; i <= tam; i++)
            {
                //this.Refresh();
                //g.DrawString(cad, new Font("Arial", 20 + i), colorizer, 10, 10);
                g.DrawString(cad, new Font("Arial", 20 + i), colorizer, 10 + 20+i, 10 + 20+i);
                contxy = 10 + 20 + i;
                contletra = 20 + i;
            }
            LinearGradientBrush colorizer2 = new LinearGradientBrush(new  Point(20, 20), new Point(450, 20),  Color.Black, Color.Gray);
            g.DrawString(cad, new Font("Arial", contletra), colorizer2, contxy, contxy);
        }


        

//-----------------------------------------------------------------------//
//-------------------CODIGO DEL MANEJO DE LA VIBORA----------------------//
//-----------------------------------------------------------------------//

//INICIALIZA TODOS LOS COMPONENTES PARA QUE SEAN VISIBLES
 private void button7_Click(object sender, EventArgs e)
        {


            if (comboBox1.SelectedIndex == 0)
                dificultad = 80;//MILISEGUNDOS QUE TARDARA EN AVANZAR UN
                                //PIXEL (VELOCIDAD)
            else if (comboBox1.SelectedIndex == 1)
                dificultad = 50;
            else if (comboBox1.SelectedIndex == 2)
                dificultad = 0;

            button4.Enabled = false;
            button5.Enabled = true;
            button6.Enabled = true;

            panel2.Visible = true;
            timer5.Enabled = false;
            timer6.Enabled = false;
            timer7.Enabled = false;
            timer4.Enabled = true;
        }
//-----------------------------------------------------------------------//
private void button3_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            timer8.Enabled = false;

            timer4.Enabled = false;
            timer5.Enabled = false;
            timer6.Enabled = false;
            timer7.Enabled = false;

            this.Refresh();
            PUNTOS = 0;//PUNTOS A CERO
            cont_comida = 1;//EMPIEZA CONTADOR EN 1
            label4.Text = PUNTOS.ToString();
            button4.Visible = true;
            button5.Visible = true;
            button6.Visible = true;
            button7.Visible = true;

            button4.Enabled = true;
            button5.Enabled = true;
            button6.Enabled = true;
            button7.Enabled = true;
            
            band1 = true;
            panel6.Visible = true;
            panel5.Visible = true;
            panel2.Visible = true;
            
//DIBUJA UN FONDO PARA EL JUEGO
            Point[] ptGraph = { new Point(0, 0), new Point( (this.Size.Width - 214)  /2, 100), new Point(this.Size.Width - 214, 0),new Point(this.Size.Width - 214-100, this.Size.Height/2),
                                new Point(this.Size.Width - 214, this.Size.Height), new Point( (this.Size.Width - 214)/2 , this.Size.Height-100),new Point(0, this.Size.Height),
                                new Point(100, (this.Size.Height)/2)};
       
            PathGradientBrush pgb = new PathGradientBrush(ptGraph);
            Graphics g1 = CreateGraphics();
            g1.FillRectangle(pgb, 0, 0, this.Size.Width - 214, this.Size.Height); 
          
//BIRMAP DONDE SE DIBUJARAN LOS PIXELES EN UNA POSICION RANDOM
            Bitmap bmp = new Bitmap(panel2.Size.Width, panel2.Size.Height);
            for (int i = 0; i < 10; i++)
            { 
                CX=ram.Next(panel2.Size.Width - 1);
                CY=ram.Next(panel2.Size.Height - 1);
                bmp.SetPixel(CX, CY, Color.Black);
                bmp.SetPixel(CX+1, CY, Color.Black);
                bmp.SetPixel(CX, CY+1, Color.Black);
                bmp.SetPixel(CX+1, CY+1, Color.Black);

//SE ALMACENAN LAS POSICIONES EN 4 ARREGLOS
                //----------------------------
                comida[i, 0] = CX;
                comida[i, 1] = CY;
                //----------------------------
                comida2[i, 0] = CX + 1;
                comida2[i, 1] = CY;
                //----------------------------
                comida3[i, 0] = CX;
                comida3[i, 1] = CY + 1;
                //----------------------------
                comida4[i, 0] = CX + 1;
                comida4[i, 1] = CY + 1;
            }
            Graphics g = panel2.CreateGraphics();
            g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height);
            timer4.Enabled = true;
         
        }
//-----------------------------------------------------------------------//
//Este timer se activa con presionar el button7  


        private void timer4_Tick(object sender, EventArgs e)
        {
            
            if (a == true)//LIMPIA EL PANEL
            {
                panel2.Refresh();
                b = true;
                c = true;
                d = true;
                a = false;
            }

            if (band1 == true)//si es el primero en presionarse
            {
                Xcount = panel2.Width/2;
                y = panel2.Height/2; //Inicializa el punto de inicio en el centro
                band1 = false;
            }
            retraso = (Xcount -crecer);
            
            //se dibuja el bitmap al tamaño del panel
            Bitmap bmp = new Bitmap(panel2.Size.Width, panel2.Size.Height);

            //Se dibujan los cuatro pixeles que forman las 10 comidas o puntos
            for (int i = 0; i < 10; i++)
            {
                bmp.SetPixel(comida[i, 0], comida[i, 1], Color.Black);
                bmp.SetPixel(comida2[i, 0], comida2[i, 1], Color.Black);
                bmp.SetPixel(comida3[i, 0], comida3[i, 1], Color.Black);
                bmp.SetPixel(comida4[i, 0], comida4[i, 1], Color.Black);
            }

            //se movera mientras no pase el limite derecho
            //de lo contrario saldra un mensaje de GAME OVER
            if (Xcount < panel2.Width)
            {
                try
                {       
                    //Dibuja la vibora con ancho de 4 pixeles
                    //color amarillo y verde alternados  
                  
                    bmp.SetPixel(Xcount++, y, Color.Green);
                    vivoraX = Xcount;
                    vivoraY = y;
                    bmp.SetPixel(Xcount, y + 1, Color.Yellow);
                    bmp.SetPixel(Xcount, y + 2, Color.Green);
                    bmp.SetPixel(Xcount, y + 3, Color.Yellow);
                    
                    Thread.Sleep(dificultad);//nos da la velocidad de la vivora
                }
             
//termina el juego al salir del limite
                catch (ArgumentOutOfRangeException)
                {
                    MessageBox.Show("PERDISTE", "GAME OVER", MessageBoxButtons.RetryCancel, MessageBoxIcon.Stop);
                    timer4.Enabled = false;
                    button4.Visible = false;
                    button5.Visible = false;
                    button6.Visible = false;
                    button7.Visible = false;
                }
                try
                { 
                 //La segunfa vibora borra la anterior evitando dejar rastro
                        bmp.SetPixel(retraso++, y, Color.PaleGreen);
                        bmp.SetPixel(retraso, y + 1, Color.PaleGreen);
                        bmp.SetPixel(retraso, y + 2, Color.PaleGreen);
                        bmp.SetPixel(retraso, y + 3, Color.PaleGreen);
                }
                catch (ArgumentOutOfRangeException) { }
            }

//VALIDA SI LA VIBORA SE CRUZO POR ALGUNA DE LAS POSICIONES
//DONDE ESTA LA COMIDA (POSICIONES ALMACENADAS EN UN ARREGLO)

            for (int i = 0; i < comida.GetLength(0); i++)
            {
                if ((( comida[i, 0] == vivoraX && comida[i, 1] == vivoraY) || (comida[i, 0] == vivoraX && comida[i, 1] == vivoraY + 1) || (comida[i, 0] == vivoraX && comida[i, 1] == vivoraY + 2)) ||
                    ((comida2[i, 0] == vivoraX && comida2[i, 1] == vivoraY) || (comida2[i, 0] == vivoraX  && comida2[i, 1] == vivoraY + 1) || (comida2[i, 0] == vivoraX && comida2[i, 1] == vivoraY + 2)) ||
                    ((comida3[i, 0] == vivoraX && comida3[i, 1] == vivoraY) || (comida2[i, 0] == vivoraX && comida2[i, 1] == vivoraY + 1) || (comida2[i, 0] == vivoraX && comida2[i, 1] == vivoraY + 2)) ||
                    ((comida4[i, 0] == vivoraX && comida4[i, 1] == vivoraY) || (comida2[i, 0] == vivoraX && comida2[i, 1] == vivoraY + 1) || (comida2[i, 0] == vivoraX && comida2[i, 1] == vivoraY + 2)))
                {
                    PUNTOS += 10;//SI ENTRA DA 10 PUNTOS
                    crecer+=4;//CRECE 4 PIXELES

                    //MANDA LOS PIXELES COMIDOS A UNA POSICION INVISIBLE PARA EL 
                    //USUARIO(0,0)

                    //SON 4 PARA CREAR UN "PIXELOTE"
                    comida[i, 0] = 0;
                    comida[i, 1] = 0;

                    comida2[i, 0] = 0;
                    comida2[i, 1] = 0;

                    comida3[i, 0] = 0;
                    comida3[i, 1] = 0;

                    comida4[i, 0] = 0;
                    comida4[i, 1] = 0;

                    cont_comida++;//ACUMULA LAS VECES QUE HA COMIDO LA VIBORA
                    label4.Text = PUNTOS.ToString();//MUESTRA EL PUNTAJE

                    
                }

            }
//SI SE LLEGA A 120 PUNTOS O COME TODA LA COMIDA GANAS
            if (PUNTOS >= 120 || cont_comida >=10)
            {
                this.Refresh();
                timer4.Enabled = false;
                panel2.Visible = false;
                panel5.Visible = false;
                panel6.Visible = false;

//ACTIVA AL TIMER 8 QUE MUESTRA UN MENSAJE DE "GANASTE"
                timer8.Enabled = true;
           
              
            }
        
            Graphics g = panel2.CreateGraphics();
            g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height);

        }   
//-----------------------------------------------------------------------//

//SE SIGUE EL MISMO PROCEDIMIENTO PARA EL RESTO DE LOS BOTONES Y TIMERS
        private void button5_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
                dificultad = 80;
            else if (comboBox1.SelectedIndex == 1)
                dificultad = 50;
            else if (comboBox1.SelectedIndex == 2)
                dificultad = 0;

            button6.Enabled = false;
            button4.Enabled = true;
            button7.Enabled = true;

            panel2.Visible = true;
            timer5.Enabled = false;
            timer6.Enabled = false;
            timer4.Enabled = false;
            timer7.Enabled = true;
        }

//---------------------------------------------------------------------------//
//Este timer se activa con presionar el button6
        int retrasoy;
        bool b = true;
        private void timer5_Tick(object sender, EventArgs e)
        {
            if (b == true)
            {
                panel2.Refresh();
                a = true;
                c = true;
                d = true;
                b = false;
            }
            if (band1 == true)
            {

                Xcount = panel2.Width / 2;
                y = panel2.Height / 2;
                band1 = false;        
            }

            retrasoy = (y - crecer);
            Bitmap bmp = new Bitmap(panel2.Size.Width, panel2.Size.Height);
            for (int i = 0; i < 10; i++)
            {
                bmp.SetPixel(comida[i, 0], comida[i, 1], Color.Black);
                bmp.SetPixel(comida2[i, 0], comida2[i, 1], Color.Black);
                bmp.SetPixel(comida3[i, 0], comida3[i, 1], Color.Black);
                bmp.SetPixel(comida4[i, 0], comida4[i, 1], Color.Black);
            }
            if (y < panel2.Height)
            {
                try
                {
                    bmp.SetPixel(Xcount, y++, Color.Green);
                    vivoraX = Xcount;
                    vivoraY = y;
                    bmp.SetPixel(Xcount + 1, y, Color.Yellow);
                    bmp.SetPixel(Xcount + 2, y, Color.Green);
                    bmp.SetPixel(Xcount + 3, y, Color.Yellow);
                    Thread.Sleep(dificultad);
                }
                catch (ArgumentOutOfRangeException)
                {
                    MessageBox.Show("PERDISTE", "GAME OVER", MessageBoxButtons.RetryCancel, MessageBoxIcon.Stop);
                    timer5.Enabled = false;
                    button4.Visible = false;
                    button5.Visible = false;
                    button6.Visible = false;
                    button7.Visible = false;
                }
                try
                {
                    bmp.SetPixel(Xcount,     retrasoy++, Color.PaleGreen);
                    bmp.SetPixel(Xcount + 1, retrasoy, Color.PaleGreen);
                    bmp.SetPixel(Xcount + 2, retrasoy, Color.PaleGreen);
                    bmp.SetPixel(Xcount + 3, retrasoy, Color.PaleGreen);

                }
                catch (ArgumentOutOfRangeException)
                {

                }
            }
            for (int i = 0; i < comida.GetLength(0); i++)
            {
                if((( comida[i, 0] == vivoraX && comida[i, 1] == vivoraY) || (comida[i, 0] == vivoraX+1 && comida[i, 1] == vivoraY) || (comida[i, 0] == vivoraX+2 && comida[i, 1] == vivoraY)) ||
                    ((comida2[i, 0] == vivoraX && comida2[i, 1] == vivoraY) || (comida2[i, 0] == vivoraX+1  && comida2[i, 1] == vivoraY) || (comida2[i, 0] == vivoraX+2 && comida2[i, 1] == vivoraY)) ||
                    ((comida3[i, 0] == vivoraX && comida3[i, 1] == vivoraY) || (comida2[i, 0] == vivoraX+1 && comida2[i, 1] == vivoraY) || (comida2[i, 0] == vivoraX+2 && comida2[i, 1] == vivoraY)) ||
                    ((comida4[i, 0] == vivoraX && comida4[i, 1] == vivoraY) || (comida2[i, 0] == vivoraX+1 && comida2[i, 1] == vivoraY) || (comida2[i, 0] == vivoraX+2 && comida2[i, 1] == vivoraY)))
                {
                    PUNTOS += 10;
                    crecer+=4;
                    comida[i, 0] = 0;
                    comida[i, 1] = 0;

                    comida2[i, 0] = 0;
                    comida2[i, 1] = 0;

                    comida3[i, 0] = 0;
                    comida3[i, 1] = 0;

                    comida4[i, 0] = 0;
                    comida4[i, 1] = 0;
                    cont_comida++;
                    label4.Text = PUNTOS.ToString();
                    // break;
                }

            }
            if (PUNTOS >= 120 || cont_comida==10)
            {
                this.Refresh();
                timer5.Enabled = false;
                panel2.Visible = false;
                panel5.Visible = false;
                panel6.Visible = false;

                timer8.Enabled = true;
            }
            Graphics g = panel2.CreateGraphics();
            g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height);
            
        }
//---------------------------------------------------------------------------//
        private void button6_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
                dificultad = 80;
            else if (comboBox1.SelectedIndex == 1)
                dificultad = 50;
            else if (comboBox1.SelectedIndex == 2)
                dificultad = 1;

            button5.Enabled = false;
            button4.Enabled = true;
            button7.Enabled = true;

            panel2.Visible = true;
            timer4.Enabled = false;
            timer6.Enabled = false;
            timer7.Enabled = false;
            timer5.Enabled = true;
        }
//Este timer se activa con presionar el button4 

        bool c = true;
        private void timer6_Tick(object sender, EventArgs e)
        {
            if (c == true)
            {
                panel2.Refresh();
                a = true;
                b = true;
                d = true;
                c = false;
            }
            if (band1 == true)
            {
                Xcount = panel2.Width / 2;
                y = panel2.Height / 2;
                band1 = false;
            }
            retraso = Xcount + crecer;
            Bitmap bmp = new Bitmap(panel2.Size.Width, panel2.Size.Height);
            for (int i = 0; i < 10; i++)
            {
                bmp.SetPixel(comida[i, 0], comida[i, 1], Color.Black);
                bmp.SetPixel(comida2[i, 0], comida2[i, 1], Color.Black);
                bmp.SetPixel(comida3[i, 0], comida3[i, 1], Color.Black);
                bmp.SetPixel(comida4[i, 0], comida4[i, 1], Color.Black);
            }
            if (Xcount >= 0)
            {
                try
                {
                    bmp.SetPixel(Xcount--, y, Color.Green);
                    vivoraX = Xcount;
                    vivoraY = y;
                    bmp.SetPixel(Xcount, y + 1, Color.Yellow);
                    bmp.SetPixel(Xcount, y + 2, Color.Green);
                    bmp.SetPixel(Xcount, y + 3, Color.Yellow);
                    Thread.Sleep(dificultad);
                }
                catch (ArgumentOutOfRangeException)
                {
                    MessageBox.Show("PERDISTE", "GAME OVER", MessageBoxButtons.RetryCancel, MessageBoxIcon.Stop);
                    timer6.Enabled = false;
                    button4.Visible = false;
                    button5.Visible = false;
                    button6.Visible = false;
                    button7.Visible = false;
                }
                try
                {
                    bmp.SetPixel(retraso--, y, Color.PaleGreen);
                    bmp.SetPixel(retraso, y + 1, Color.PaleGreen);
                    bmp.SetPixel(retraso, y + 2, Color.PaleGreen);
                    bmp.SetPixel(retraso, y + 3, Color.PaleGreen);

                }
                catch (ArgumentOutOfRangeException)
                {

                }
            }
            for (int i = 0; i < comida.GetLength(0); i++)
            {
                if (((comida[i, 0] == vivoraX && comida[i, 1] == vivoraY) || (comida[i, 0] == vivoraX && comida[i, 1] == vivoraY + 1) || (comida[i, 0] == vivoraX && comida[i, 1] == vivoraY + 2)) ||
                    ((comida2[i, 0] == vivoraX && comida2[i, 1] == vivoraY) || (comida2[i, 0] == vivoraX && comida2[i, 1] == vivoraY + 1) || (comida2[i, 0] == vivoraX && comida2[i, 1] == vivoraY + 2)) ||
                    ((comida3[i, 0] == vivoraX && comida3[i, 1] == vivoraY) || (comida2[i, 0] == vivoraX && comida2[i, 1] == vivoraY + 1) || (comida2[i, 0] == vivoraX && comida2[i, 1] == vivoraY + 2)) ||
                    ((comida4[i, 0] == vivoraX && comida4[i, 1] == vivoraY) || (comida2[i, 0] == vivoraX && comida2[i, 1] == vivoraY + 1) || (comida2[i, 0] == vivoraX && comida2[i, 1] == vivoraY + 2)))
                {
                    PUNTOS += 10;
                    crecer+=4;
                    comida[i, 0] = 0;
                    comida[i, 1] = 0;

                    comida2[i, 0] = 0;
                    comida2[i, 1] = 0;

                    comida3[i, 0] = 0;
                    comida3[i, 1] = 0;

                    comida4[i, 0] = 0;
                    comida4[i, 1] = 0;
                    cont_comida++;
                    label4.Text = PUNTOS.ToString();
                    // break;
                }

            }
            if (PUNTOS >= 120 || cont_comida==10)
            {
                this.Refresh();
                timer6.Enabled = false;
                panel2.Visible = false;
                panel5.Visible = false;
                panel6.Visible = false;
                timer8.Enabled = true;
                //Graphics g1 = this.CreateGraphics();

                //LinearGradientBrush colorizer = new LinearGradientBrush(new Point(20, 20), new Point(450, 20), Color.White, Color.DarkRed);
                //g1.DrawString("GANASTE", new Font("CASTELLAR", 20), colorizer, 10, 10);

                //LinearGradientBrush colorizer2 = new LinearGradientBrush(new Point(20, 20), new Point(450, 20), Color.Black, Color.Gray);
                //g1.DrawString("GANASTE", new Font("CASTELLAR", 75), colorizer2, 100, 100);
            }
            Graphics g = panel2.CreateGraphics();
            g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
                dificultad = 80;
            else if (comboBox1.SelectedIndex == 1)
                dificultad = 50;
            else if (comboBox1.SelectedIndex == 2)
                dificultad = 1;

            button7.Enabled = false;
            button5.Enabled = true;
            button6.Enabled = true;

            panel2.Visible = true;
            timer5.Enabled = false;
            timer4.Enabled = false;
            timer7.Enabled = false;
            timer6.Enabled = true;
        }
//Este timer se activa con presionar el button5

        bool d = true;
        private void timer7_Tick(object sender, EventArgs e)
        {
            if (d == true)
            {
                panel2.Refresh();
                a = true;
                b = true;
                c = true;
                d = false;

            }
            if (band1 == true)
            {
                Xcount = panel2.Width / 2;
                y = panel2.Height / 2;
                band1 = false;
            }
            retrasoy = y + crecer;
            Bitmap bmp = new Bitmap(panel2.Size.Width, panel2.Size.Height);
            for (int i = 0; i < 10; i++)
            {
                bmp.SetPixel(comida[i, 0], comida[i, 1], Color.Black);
                bmp.SetPixel(comida2[i, 0], comida2[i, 1], Color.Black);
                bmp.SetPixel(comida3[i, 0], comida3[i, 1], Color.Black);
                bmp.SetPixel(comida4[i, 0], comida4[i, 1], Color.Black);
            }
            if (y >= 0)
            {
                try
                {
                    bmp.SetPixel(Xcount, y--, Color.Green);
                    vivoraX = Xcount;
                    vivoraY = y;
                    bmp.SetPixel(Xcount + 1, y, Color.Yellow);
                    bmp.SetPixel(Xcount + 2, y, Color.Green);
                    bmp.SetPixel(Xcount + 3, y, Color.Yellow);
                    Thread.Sleep(dificultad);
                }
                catch (ArgumentOutOfRangeException)
                {
                    MessageBox.Show("PERDISTE", "GAME OVER", MessageBoxButtons.RetryCancel, MessageBoxIcon.Stop);
                    timer7.Enabled = false;
                    button4.Visible = false;
                    button5.Visible = false;
                    button6.Visible = false;
                    button7.Visible = false;
                }
                try
                {
                    bmp.SetPixel(Xcount, retrasoy--, Color.PaleGreen);
                    bmp.SetPixel(Xcount+1, retrasoy , Color.PaleGreen);
                    bmp.SetPixel(Xcount+2, retrasoy, Color.PaleGreen);
                    bmp.SetPixel(Xcount+3, retrasoy, Color.PaleGreen);

                }
                catch (ArgumentOutOfRangeException)
                {

                }
            }
            for (int i = 0; i < comida.GetLength(0); i++)
            {
                if (((comida[i, 0] == vivoraX && comida[i, 1] == vivoraY) || (comida[i, 0] == vivoraX && comida[i, 1] == vivoraY) || (comida[i, 0] == vivoraX && comida[i, 1] == vivoraY)) ||
                    ((comida2[i, 0] == vivoraX && comida2[i, 1] == vivoraY) || (comida2[i, 0] == vivoraX+1 && comida2[i, 1] == vivoraY) || (comida2[i, 0] == vivoraX+2 && comida2[i, 1] == vivoraY )) ||
                    ((comida3[i, 0] == vivoraX && comida3[i, 1] == vivoraY) || (comida2[i, 0] == vivoraX+1 && comida2[i, 1] == vivoraY) || (comida2[i, 0] == vivoraX+2 && comida2[i, 1] == vivoraY)) ||
                    ((comida4[i, 0] == vivoraX && comida4[i, 1] == vivoraY) || (comida2[i, 0] == vivoraX+1 && comida2[i, 1] == vivoraY) || (comida2[i, 0] == vivoraX+2 && comida2[i, 1] == vivoraY)))
                {
                    PUNTOS += 10;
                    crecer+=4;
                    comida[i, 0] = 0;
                    comida[i, 1] = 0;

                    comida2[i, 0] = 0;
                    comida2[i, 1] = 0;

                    comida3[i, 0] = 0;
                    comida3[i, 1] = 0;

                    comida4[i, 0] = 0;
                    comida4[i, 1] = 0;

                    cont_comida++;
                    label4.Text = PUNTOS.ToString();
                    // break;
                }

            }
            if (PUNTOS >= 120 || cont_comida==10)
            {
                this.Refresh();
                timer7.Enabled = false;
                panel2.Visible = false;
                panel5.Visible = false;
                panel6.Visible = false;
                timer8.Enabled = true;
                //Graphics g1 = this.CreateGraphics();

                //LinearGradientBrush colorizer = new LinearGradientBrush(new Point(20, 20), new Point(450, 20), Color.White, Color.DarkRed);
                //g1.DrawString("GANASTE", new Font("CASTELLAR", 20), colorizer, 10, 10);

                //LinearGradientBrush colorizer2 = new LinearGradientBrush(new Point(20, 20), new Point(450, 20), Color.Black, Color.Gray);
                //g1.DrawString("GANASTE", new Font("CASTELLAR", 75), colorizer2, 100, 100);
            }
            Graphics g = panel2.CreateGraphics();
            g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height);
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {

        }
    
//------------------------------------------------------------------///
//BOTON DE AYUDA E INSTRUCCIONES DEL JUEGO
  
        private void button8_Click(object sender, EventArgs e)
        {
            MessageBox.Show("1)Mueve la vibora con los botones que aparecen en la parte inferior\n2)Los botones aparecen cuando oprimas el boton JUGAR VIBORA \n3)Cuando la vibora coma se daran 20 puntos pero si solo comes la mitad seran 10 puntos \n4)Ganaras el juego si comes todo o acumulas 120 puntos \n5)Cuidado con no tocar los bordes de la pantalla o perderas \n6)Por defaul se inicia en nivel PRINCIPIANTE", "AYUDA",MessageBoxButtons.OK,MessageBoxIcon.Question);
        }
//----------------------------------------------------------------------------
//MENSAJE QUE APARECE CUANDO GANAS EL JUEGO

        private void timer8_Tick(object sender, EventArgs e)
        {
            Graphics g1 = this.CreateGraphics();

            LinearGradientBrush colorizer = new LinearGradientBrush(new Point(20, 20), new Point(450, 20), Color.White, Color.DarkRed);
            g1.DrawString("GANASTE", new Font("CASTELLAR", 20), colorizer, 10, 10);

            LinearGradientBrush colorizer2 = new LinearGradientBrush(new Point(20, 20), new Point(450, 20), Color.Black, Color.Gray);
            g1.DrawString("GANASTE", new Font("CASTELLAR", 75), colorizer2, 100, 100);
        }
//----------------------------------------------------------------------------
//BOTON DE PAUSA DEL JUEGO
        bool enc = true;
        private void button10_Click(object sender, EventArgs e)
        {
            if (enc == true)
            {
                timer4.Stop();
                timer5.Stop();
                timer6.Stop();
                timer7.Stop();
                enc = false;
            }
            else if (enc == false)
            {
                MessageBox.Show("PRESIONA LOS BOTONES PARA CONTINUAR");
                enc = true;
            }
        }
  }
}
//-----------------------------END---------------------------------------//

ESTA APLICACIÓN ES PROYECTO DE LA UNIDAD 1 DE GRAFICACIÓN
ESTO ES TODO EL POST POR AHORA
DEJEN SUS COMENTARIOS O CRITICAS.


GRACIAS!!!!!

6 comentarios:

  1. holaaa hey que cool esta tu juego de casualidad no sabes hacer el juego del stop en c#???
    El juego de stop que consiste en que el servidor da una pista para que los clientes completen con nombre de personas, paises, ciudades, objetos, animales, los clientes deben completar la información en el menor tiempo posible y el servidor valida las respuestas y anuncia cual es el ganador de acuerdo al tiempo en que responden,
    ejemplo el servidor anuncia: inicia con A y termina con A, los clientes responden:

    Pais: Argentina
    Animal: Aguila
    Nombre: Amanda
    Fruta: Almendra
    Objeto: Agenda
    etc,etc

    Saludos.

    cualkier ayuda aka gracias lau9.11@hotmail.com

    ResponderEliminar
  2. Dice Richard k suban el
    Proyecto para Descargar ....

    ResponderEliminar
  3. Este comentario ha sido eliminado por el autor.

    ResponderEliminar
  4. el juego de stop esta en proceso, gracias por la idea. y sobre subir el proyecto no creo que sea posible.

    ResponderEliminar
  5. por favor sube el proyecto para descargarlo por que lo he tratado de hacer con estos datos pero no me sale por favor

    ResponderEliminar
  6. Cuanto tiempo te demoraste haciendo este juego?

    ResponderEliminar