Ver Mensaje Individual
  #2 (permalink)  
Antiguo 25/01/2016, 18:37
Avatar de OPMUANRK
OPMUANRK
 
Fecha de Ingreso: diciembre-2014
Mensajes: 52
Antigüedad: 9 años, 4 meses
Puntos: 1
Exclamación Respuesta: Cuerpo de la viborita en letras (Juego) - C#

Y por último la clase form1

Código C#:
Ver original
  1. namespace Viborita
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         int Record;
  6.         int RecordFinal;
  7.         //Cuerpo de la víbora
  8.         private List<Circulos>Viborita = new List<Circulos>();
  9.         //Formar comida
  10.         private Circulos comida = new Circulos();
  11.        
  12.         public Form1()
  13.         {
  14.             InitializeComponent();
  15.             //Cargar las configuraciones predeterminadas
  16.             new Config();
  17.             //Establecer la velocidad y empezar el temporizador
  18.             Tiempo.Interval = 1000 / Config.Velocidad;
  19.             Tiempo.Tick += Actualizar;
  20.             Tiempo.Start();
  21.             //Cuadro de diálogo para escribir tu nombre (Hacer referecia a Visual Basic para que funcione)
  22.             string nombre = Microsoft.VisualBasic.Interaction.InputBox("Escribe tu nombre", "Bienvenido", "");
  23.             lbNombre.Text = nombre;
  24.             //Empezar el juego
  25.             EmpezarJuego();
  26.         }
  27.  
  28.         private void EmpezarJuego()
  29.         {
  30.             lbFinJuego.Visible = false;
  31.             //Cargar las configuraciones predeterminadas
  32.             lbrecord.Text = RecordFinal.ToString();
  33.             new Config();
  34.  
  35.             //Esto crea a la víbora y la pone en el juego
  36.             Viborita.Clear();
  37.             Circulos cabeza = new Circulos();
  38.             cabeza.X = 10;
  39.             cabeza.Y = 5;
  40.             Viborita.Add(cabeza);
  41.  
  42.             lbPuntos.Text = Config.Puntuacion.ToString();
  43.  
  44.             GenerarComida();
  45.         }
  46.         //Genera la comida aleatoriamente en la pantalla
  47.         private void GenerarComida()
  48.         {
  49.             //Divide la altura y anchura de nuestra área de juego por la de la configuración
  50.             //para tener una variable qué usar y en dónde ponerla sin salir del área
  51.             int PosicionXmax = pbArea.Size.Width / Config.Ancho;
  52.             int PosicionYmax = pbArea.Size.Height / Config.Alto;
  53.  
  54.             //Esto crea un nuevo objeto aleatorio y un objeto de comida
  55.             //y lo coloca en un lugar del área de juego entre cero y el máximo del área de juego en X y Y
  56.             Random aleatorio = new Random();
  57.             comida = new Circulos();
  58.             comida.X = aleatorio.Next(0, PosicionXmax);
  59.             comida.Y = aleatorio.Next(0, PosicionYmax);
  60.         }
  61.  
  62.         private void Actualizar (object sender, EventArgs e)
  63.         {
  64.             //Ver si el juego terminó
  65.             if (Config.FinJuego==true)
  66.             {
  67.                 //Verificar si la tecla Enter es presionada
  68.                 if (Entrada.KeyPressed(Keys.Enter))
  69.                 {
  70.                     EmpezarJuego();
  71.                     Viborita.Clear();
  72.                     string nom = lbNombre.Text;
  73.                     string nombre = Microsoft.VisualBasic.Interaction.InputBox("Escribe tu nombre", "Bienvenido",""+nom+"");
  74.                     lbNombre.Text = nombre;
  75.                     nom = nombre;
  76.                     EmpezarJuego();
  77.                 }
  78.                
  79.             }
  80.             else
  81.             {
  82.                 if (Entrada.KeyPressed(Keys.Right) && Config.direccion != Direccion.Izq)
  83.                     Config.direccion = Direccion.Der;
  84.                 else if (Entrada.KeyPressed(Keys.Left) && Config.direccion != Direccion.Der)
  85.                     Config.direccion = Direccion.Izq;
  86.                 else if (Entrada.KeyPressed(Keys.Up) && Config.direccion != Direccion.Abajo)
  87.                     Config.direccion = Direccion.Arriba;
  88.                 else if (Entrada.KeyPressed(Keys.Down) && Config.direccion != Direccion.Arriba)
  89.                     Config.direccion = Direccion.Abajo;
  90.  
  91.                 Moverse();
  92.             }
  93.                 pbArea.Invalidate();
  94.         }
  95.  
  96.         private void Tiempo_Tick(object sender, EventArgs e)
  97.         {
  98.             throw new NotImplementedException();
  99.         }
  100.  
  101.         private void label1_Click(object sender, EventArgs e)
  102.         {
  103.  
  104.         }
  105.  
  106.         private void lbPuntos_Click(object sender, EventArgs e)
  107.         {
  108.  
  109.         }
  110.  
  111.         private void Form1_Load(object sender, EventArgs e)
  112.         {
  113.  
  114.         }
  115.  
  116.         private void pbArea_Paint(object sender, PaintEventArgs e)
  117.         {
  118.             Graphics area = e.Graphics;
  119.  
  120.             if(!Config.FinJuego)
  121.             {
  122.                 //Establecer el color de la víbora
  123.                 Brush colorVib;
  124.  
  125.                 //Colores de la víbora
  126.                 for(int i=0; i< Viborita.Count; i++)
  127.                 {
  128.                     if (i == 0)
  129.                         colorVib = Brushes.Black; //Dibuja la cabeza
  130.                     else
  131.                         colorVib = Brushes.OrangeRed; //Dibuja el resto del cuerpo
  132.  
  133.                     //Dibujar la víbora
  134.                     area.FillEllipse(colorVib, new Rectangle(Viborita[i].X * Config.Ancho,
  135.                         Viborita[i].Y * Config.Alto,
  136.                         Config.Ancho, Config.Alto));
  137.  
  138.                     //Dibujar comida
  139.                     area.FillEllipse(Brushes.Green,
  140.                         new Rectangle(comida.X * Config.Ancho,
  141.                         comida.Y * Config.Alto, Config.Ancho, Config.Alto));
  142.  
  143.                 }
  144.             }
  145.             else
  146.             {
  147.                 string finjuego = "Fin del juego.\nTu puntuación final fue: " + Config.Puntuacion +
  148.                     "\n Presiona la tecla Enter para reintentar.";
  149.                 if (RecordFinal==0)
  150.                 {
  151.                     Record = Config.Puntuacion;
  152.                     RecordFinal = Record;
  153.                 }
  154.                 if(Config.Puntuacion>RecordFinal)
  155.                 {
  156.                     RecordFinal = Config.Puntuacion;
  157.                 }
  158.                 lbFinJuego.Text = finjuego;
  159.                 lbFinJuego.Visible = true;
  160.             }
  161.         }
  162.  
  163.         private void Moverse()
  164.         {
  165.             for(int i = Viborita.Count -1; i>=0; i--)
  166.             {
  167.                 //Mover cabeza
  168.                 if (i == 0)
  169.                 {
  170.                     switch (Config.direccion)
  171.                     {
  172.                         case Direccion.Der:
  173.                             Viborita[i].X++;
  174.                             break;
  175.                         case Direccion.Izq:
  176.                             Viborita[i].X--;
  177.                             break;
  178.                         case Direccion.Arriba:
  179.                             Viborita[i].Y--;
  180.                             break;
  181.                         case Direccion.Abajo:
  182.                             Viborita[i].Y++;
  183.                             break;
  184.                     }
  185.                     //Obtener las posiciones máximas de X y Y
  186.                     int maxX = pbArea.Size.Width / Config.Ancho;
  187.                     int maxY = pbArea.Size.Height / Config.Alto;
  188.  
  189.                     //Detectar la colisión en las paredes
  190.                     if (Viborita[i].X < 0 || Viborita[i].Y < 0
  191.                         || Viborita[i].X >= maxX || Viborita[i].Y >= maxY)
  192.                     {
  193.                         Muere();
  194.                     }
  195.                     //Detectar colisión con el cuerpo
  196.                     for(int j=1; j<Viborita.Count; j++)
  197.                     {
  198.                         if(Viborita[i].X == Viborita[j].X &&
  199.                             Viborita[i].Y == Viborita[j].Y)
  200.                         {
  201.                             Muere();
  202.                         }                
  203.                     }
  204.                     //Detectar colisión con la comida
  205.                     if(Viborita[0].X==comida.X && Viborita[0].Y == comida.Y)
  206.                     {
  207.                         Come();
  208.                     }
  209.                 }
  210.                 else
  211.                 {
  212.                     //Mover el cuerpo
  213.                     Viborita[i].X = Viborita[i - 1].X;
  214.                     Viborita[i].Y = Viborita[i - 1].Y;
  215.                 }
  216.             }
  217.         }
  218.  
  219.         private void Come()
  220.         {
  221.             //Agregar un círculo a la víbora
  222.             Circulos comida = new Circulos();
  223.             comida.X = Viborita[Viborita.Count - 1].X;
  224.             comida.Y = Viborita[Viborita.Count - 1].Y;
  225.  
  226.             Viborita.Add(comida);
  227.  
  228.             //Actualizar puntaje
  229.             Config.Puntuacion += Config.Puntos;
  230.             lbPuntos.Text = Config.Puntuacion.ToString();
  231.                        
  232.             GenerarComida();
  233.         }
  234.  
  235.         private void Muere()
  236.         {
  237.             Config.FinJuego = true;
  238.         }
  239.  
  240.         private void lbFinJuego_Click(object sender, EventArgs e)
  241.         {
  242.  
  243.         }
  244.  
  245.         private void Form1_KeyDown(object sender, KeyEventArgs e)
  246.         {
  247.             Entrada.CambiarEstado(e.KeyCode, true);
  248.         }
  249.  
  250.         private void Form1_KeyUp(object sender, KeyEventArgs e)
  251.         {
  252.             Entrada.CambiarEstado(e.KeyCode, false);
  253.         }
  254.  
  255.         private void label1_Click_1(object sender, EventArgs e)
  256.         {
  257.  
  258.         }
  259.  
  260.         private void label1_Click_2(object sender, EventArgs e)
  261.         {
  262.  
  263.         }
  264.  
  265.         private void tbNomre_TextChanged(object sender, EventArgs e)
  266.         {
  267.          
  268.         }
  269.  
  270.         private void BAplicar_Click(object sender, EventArgs e)
  271.         {
  272.         }
  273.  
  274.         private void pbArea_Click(object sender, EventArgs e)
  275.         {
  276.          
  277.         }
  278.     }
  279. }