NEWS

[C#] Hướng dẫn viết Game chơi chẵn lẻ random number effect Winform

[C#] Hướng dẫn viết Game chơi chẵn lẻ random number effect Winform
Đăng bởi: Thảo Meo - Lượt xem: 5419 11:50:35, 28/07/2021PHẦN MỀM

Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn cách viết game chơi chẵn lẻ đơn giản sử dụng Random Number trong C#, winform.

[C#] Game chẵn lẻ trong csharp winform

Dưới đây, là giao diện demo game chẵn lẻ:

game_random_number

Bình thường mình cũng hay chơi trò này lắm...

Để tạo cho bắt mắt, các bạn chỉ cần tạo thêm effect cho số nó đang quay thôi.

Video demo game chẵn lẻ:

Source code C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RandomNumberEffect
{
    public partial class Form1 : Form
    {
        private int total = 0;
        private Random rnd = new Random();
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_play_Click(object sender, EventArgs e)
        {
            lbl_win_lose.Visible = false;
            var min = Convert.ToInt32(txt_min.Text);
            var max = Convert.ToInt32(txt_max.Text);            
            var result = rnd.Next(min, max);
            var result_label = chk_even.Checked == (result % 2 == 0) ? "WINNER" : "LOSER";
           
            if(result < total)
            {
                int valAdd = Math.Abs( result - total);               
                total = result;
                AddTotalLabelEffect(total, true, valAdd, result_label);
            }
            else
            {
                int valSubtract = Math.Abs( total - result);               
                total = result;
                AddTotalLabelEffect(total, false, valSubtract, result_label);
            }

        }
        private void AddTotalLabelEffect(int add, bool isAdd, int val, string result_label)
        {
            int value = 0;
            if (isAdd)
            {
                value = total - val;
            }
            else
            {
                value = total + val;
            }

            var d = add / 100;
            Task.Run(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    if (isAdd)
                    {
                        value += d;
                    }
                    else
                    {
                        value -= d;
                    }

                    this.BeginInvoke(new Action(() => { txt_result.Text =  Math.Abs(value).ToString(); }));
                    Thread.Sleep(10);
                }
                Thread.Sleep(10);
                this.BeginInvoke(new Action(() => { txt_result.Text = total.ToString();
                    lbl_win_lose.Text = result_label;
                    if(result_label == "WINNER")
                    {
                        lbl_win_lose.ForeColor = Color.Green;
                    }
                    else
                    {
                        lbl_win_lose.ForeColor = Color.DarkGray;
                    }

                    lbl_win_lose.Visible = true;
                }));

            });

        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

Tags: random number c#game chẵn lẻ

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn viết Game chơi chẵn lẻ random number effect Winform
Đăng bởi: Thảo Meo - Lượt xem: 5419 11:50:35, 28/07/2021PHẦN MỀM