viernes, 5 de diciembre de 2014

Realizar un juego matemático con Visual studio


MenuStrip: Proporciona un sistema de menús para un formulario.

Timer:El control Timer es un temporizador que nos permite ejecutar instrucciones de código, rutinas, funciones etc..., cada cierto intervalo de tiempo.
Este control es invisible en tiempo de ejecución, esto quiere decir que no tiene interfaz gráfica, solo es visible cuando lo agregamos a un formulario y estamos en modo de diseño .
La propiedad mas importante de este control es la propiedad Interval

IntervalDevuelve o establece el número de milisegundos entre dos llamadas al evento Timer de un control Timer.

-Primero tienes que agregar un windows form en donde agregaras las instrucciones.
-Utilizas el menustrip en donde puedes poner los menús y submenus que desees en este caso serán Operaciones:Suma , resta, divicion, multiplicacion y asi con los demas menus.
(Puedes agregar a cada submenu un ShortcutKeys para un acceso rápido a el )
-Agregar el timer.

-Agregas el siguiente código .
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Resover_Operaciones_Basicas
{
    public partial class Form1 : Form
    {
        float ki,matte;
     
        public Form1()
        {
         
            InitializeComponent();
        }

     
        private void Suma_Click(object sender, EventArgs e)
        {
            Multiplicacion.Checked = false;
            Division.Checked = false;
            Resta.Checked = false;
            beta();
        }
     
        private void but_jugar_Click(object sender, EventArgs e)
        {
            if (Suma.Checked)
            { label13.Text = "+"; }
            if (Resta.Checked)
            { label13.Text = "-"; }
            if (Division.Checked)
            { label13.Text = "/"; }
            if (Multiplicacion.Checked)
            { label13.Text = "*"; }

            matte = matte + 1;
            tb_resultado.Text = "";
            tb_t_a_m.Text = "00";
            tb_t_a_s.Text = "00";
            but_jugar.Visible = false;
            gb_operacion.Visible = true;
            gb_tiempo.Visible = true;
            alpha();
            gamma();
            beta();
            timer1.Start();
        }

        private void tiempoToolStripMenuItem_Click(object sender, EventArgs e)
        {
         
        }
        private void beta()
        {
            if (Suma.Checked)
            { lab_simbolo.Text = "+"; }
            if (Resta.Checked)
            { lab_simbolo.Text = "-"; }
            if (Division.Checked)
            { lab_simbolo.Text = "/"; }
            if (Multiplicacion.Checked)
            { lab_simbolo.Text = "*"; }
        }
        private void alpha()
        {
            if (Segundo_30.Checked)
            {
                tb_t_s_m.Text = 00.ToString();
                tb_t_s_s.Text = 30.ToString();

            }
            if (Segundo_90.Checked)
            {
                tb_t_s_m.Text = 01.ToString();
                tb_t_s_s.Text = 30.ToString();
            }
            if (minuto_1.Checked)
            {
                tb_t_s_s.Text = 00.ToString();
                tb_t_s_m.Text = 01.ToString();
            }
        }
        private void Segundo_30_Click(object sender, EventArgs e)
        {
            minuto_1.Checked = false;
            Segundo_90.Checked = false;
            alpha();
        }

        private void minuto_1_Click(object sender, EventArgs e)
        {
            Segundo_30.Checked = false;
            Segundo_90.Checked = false;
            alpha();
        }

        private void Segundo_90_Click(object sender, EventArgs e)
        {
            minuto_1.Checked = false;
            Segundo_30.Checked = false;
            alpha();
        }
        private void gamma()
        {
            int r_a = 0, r_b = 0;
            Random a = new Random(DateTime.Now.Millisecond);
            Random b = new Random(DateTime.Now.Second);

            if (Cifra1.Checked == true)
            {
                r_a = a.Next(1, 10);
                r_b = b.Next(1, 10);
            }
            if (Cifra2.Checked)
            {
                r_a = a.Next(10, 100);
                r_b = b.Next(10, 100);
            }
            if (Cifra_3.Checked)
            {
                r_a = a.Next(100, 999);
                r_b = b.Next(100, 999);
            }

            tb_val1.Text = r_a.ToString();
            tb_val2.Text = r_b.ToString();
        }
        private void Cifra2_Click(object sender, EventArgs e)
        {
            Cifra1.Checked = false;
            Cifra_3.Checked = false;
         
        }

        private void Cifra_3_Click(object sender, EventArgs e)
        {
            Cifra1.Checked = false;
            Cifra2.Checked = false;        
         
        }
        private void Cifra_1_Click(object sender, EventArgs e)
        {
            Cifra2.Checked = false;
            Cifra_3.Checked = false;
         
        }

        private void Resta_Click(object sender, EventArgs e)
        {
            Multiplicacion.Checked = false;
            Division.Checked = false;
            Suma.Checked = false;
            beta();
         
        }

        private void Multiplicacion_Click(object sender, EventArgs e)
        {
         
            Division.Checked = false;
            Resta.Checked = false;
            Suma.Checked = false;
            beta();
        }

        private void Division_Click(object sender, EventArgs e)
        {
         
            Multiplicacion.Checked = false;
            Resta.Checked = false;
            Suma.Checked = false;
            beta();
        }

        private void tb_resultado_KeyDown(object sender, KeyEventArgs e)
        {
            float q=0, w=0, r=0,y=0;
            DialogResult yui= 0;

            if (e.KeyCode == Keys.Enter && lab_simbolo.Text == "+")
            {

                q = float.Parse(tb_val1.Text);
                w = float.Parse(tb_val2.Text);
                r = float.Parse(tb_resultado.Text);
                y = q + w;
                timer1.Stop();
                if (y == r)
                {

                    yui = MessageBox.Show(" Correcta :) ", "Tu respuesta es: ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    ki = ki + 1;
                }
                else
                {
                    yui = MessageBox.Show("Incorrecta :( ", "Tu respesta es:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                if (yui == DialogResult.OK)
                {
                    but_jugar.Visible = true;
                    gb_operacion.Visible = false;
                    gb_tiempo.Visible = false;

                }
            }
            if (e.KeyCode == Keys.Enter && lab_simbolo.Text == "*")
            {

                q = float.Parse(tb_val1.Text);
                w = float.Parse(tb_val2.Text);
                r = float.Parse(tb_resultado.Text);
                y = q * w;
                timer1.Stop();
                if (y == r)
                {

                    yui = MessageBox.Show(" Correcta :) ", "Tu respuesta es: ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    ki = ki + 1;

                }
                else
                {
                    yui = MessageBox.Show("Incorrecta :( ", "Tu respesta es:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                if (yui == DialogResult.OK)
                {
                    but_jugar.Visible = true;
                    gb_operacion.Visible = false;
                    gb_tiempo.Visible = false;
                }
            }
             if (e.KeyCode == Keys.Enter && lab_simbolo.Text == "/" )
            {

                q = float.Parse(tb_val1.Text);
                w = float.Parse(tb_val2.Text);
                r = float.Parse(tb_resultado.Text);
                y = q / w;
                timer1.Stop();
                if (y == r)
                {

                    yui = MessageBox.Show(" Correcta :) ", "Tu respuesta es: ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    ki = ki + 1;
                 
                }
                else
                {
                    yui = MessageBox.Show("Incorrecta :( ", "Tu respesta es:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                if (yui == DialogResult.OK)
                {
                but_jugar.Visible = true;
                gb_operacion.Visible = false;
                gb_tiempo.Visible = false;
                }
             }
               if (e.KeyCode == Keys.Enter && lab_simbolo.Text == "-" )
            {

                q = float.Parse(tb_val1.Text);
                w = float.Parse(tb_val2.Text);
                r = float.Parse(tb_resultado.Text);
                y = q - w;
                timer1.Stop();
                if (y == r)
                {

                    yui = MessageBox.Show(" Correcta :) ", "Tu respuesta es: ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    ki = ki + 1;
                 
                }
                else
                {
                    yui = MessageBox.Show("Incorrecta :( ", "Tu respesta es:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                if (yui == DialogResult.OK)
                {
                but_jugar.Visible = true;
                gb_operacion.Visible = false;
                gb_tiempo.Visible = false;
                }
            }
        }

        private void gb_operacion_Enter(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            tb_t_a_s.Text = ((int.Parse(tb_t_a_s.Text)) + 1).ToString();
            if (int.Parse(tb_t_a_s.Text) == 60)
            {
                tb_t_a_m.Text = (int.Parse(tb_t_a_m.Text) + 1).ToString();
                tb_t_a_s.Text = "0";
            }
            if (int.Parse(tb_t_a_s.Text) == int.Parse(tb_t_s_s.Text) && int.Parse(tb_t_a_m.Text) == int.Parse(tb_t_s_m.Text) )
            {
                timer1.Stop();
                DialogResult w;
                w = MessageBox.Show("Tu tiempo a terminado", "Fin del juego", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                if (w == DialogResult.OK)
                {
                    tb_resultado.Text = "0";
                    gb_operacion.Visible = false;
                    gb_tiempo.Visible = false;
                    but_jugar.Visible = true;
                }
            }

         
        }
        private void awer()
        {
            DialogResult w;
            w = MessageBox.Show( "El Porcentaje de las operaciones que has realizado bien :) es de "+(((100 * ki) / matte).ToString())+"%", "RATIO", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void tb_t_a_s_TextChanged(object sender, EventArgs e)
        {

        }

        private void tb_resultado_TextChanged(object sender, EventArgs e)
        {

        }

        private void salirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult w;
            w = MessageBox.Show("¿Quieres anbandonar el juego?", "Vas a salir", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
             if (w == DialogResult.Yes)
             {
                 Application.Exit();
             }

        }

        private void porcentageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            awer();
        }

        private void instruccionesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            instrucciones instruct = new instrucciones();
            instruct.ShowDialog();
            this.Show();
        }
Al ejecutar

Si acaba tu tiempo:




0 comentarios :

Publicar un comentario