Cara membuat aplikasi Game Threat menggunakan Visual Studio C#

Kode untuk membuat game thread


Tambahkan Namespace Thread
using System.Threading;
using System.Threading.Tasks;

namespace aplikasigame
{
    public partial class Form1 : Form
    {
        public bool PlayAgain = false;
        static Random r;
        static Thread Player1Thd;
        static Thread Player2Thd;
        static Thread Player3Thd;
        public static int countPlayer1;
        public static int countPlayer2;
        public static int countPlayer3;

        public Form1()
        {
            InitializeComponent();
            countPlayer1 = 0;
            countPlayer2 = 0;
            countPlayer3 = 0;
            r = new Random();
        }

Ketikkan method berikut
        public static void Player1()
        {
            while(true)
            {
                Thread.Sleep(50);
                if(countPlayer1 > 650)
                {
                    countPlayer1.abort();
                    break;
                }
                countPlayer1 = countPlayer1 + (1 + r.Next(6));
            }
        }

        public static void Player2()
        {
            while (true)
            {
                Thread.Sleep(50);
                if (countPlayer2 > 650)
                {
                    countPlayer2.abort();
                    break;
                }
                countPlayer2 = countPlayer2 + (1 + r.Next(6));
            }
        }

        public static void Player3()
        {
            while (true)
            {
                Thread.Sleep(50);
                if (countPlayer3 > 650)
                {
                    countPlayer3.abort();
                    break;
                }
                countPlayer3 = countPlayer3 + (1 + r.Next(6));
            }
        }



        private void Form1_Load(object sender, EventArgs e)
        {

        }

Double klik pada bagian button Play Game dan ketikkan kode berikut

        private void btnPLay_Click(object sender, EventArgs e)
        {
            countPlayer1 = 0;
            countPlayer2 = 0;
            countPlayer3 = 0;

            Player1Thd = new Thread(new ThreadStart(Player1));
            Player1Thd.IsBackground = true;
            Player1Thd.Start();

            Player2Thd = new Thread(new ThreadStart(Player2));
            Player2Thd.IsBackground = true;
            Player2Thd.Start();

            Player3Thd = new Thread(new ThreadStart(Player3));
            Player3Thd.IsBackground = true;
            Player3Thd.Start();

            timer1.Start();
        }

Double klik icon pada timer dan ketikkan kode berikut

        private void timer1_Tick(object sender, EventArgs e)
        {
            bool check = false;

            if (countPlayer1 > 650)
            {
                timer1.Stop();
                countPlayer1 = 0;
                countPlayer2 = 0;
                countPlayer3 = 0;
                check = true;
                int i = Int32.Parse(label2.Text);
                i++;
                label2.Text = i.ToString();
                MessageBox.Show("Player one win the race");
            }

            if (countPlayer1 > 650)
            {
                timer1.Stop();
                countPlayer1 = 0;
                countPlayer2 = 0;
                countPlayer3 = 0;
                check = true;
                int i = Int32.Parse(label4.Text);
                i++;
                label4.Text = i.ToString();
                MessageBox.Show("Player one win the race");
            }

            if (countPlayer1 > 650)
            {
                timer1.Stop();
                countPlayer1 = 0;
                countPlayer2 = 0;
                countPlayer3 = 0;
                check = true;
                int i = Int32.Parse(label6.Text);
                i++;
                label6.Text = i.ToString();
                MessageBox.Show("Player one win the race");
            }

            if (check == false)
            {
                button1.Left = countPlayer1;
                button2.Left = countPlayer2;
                button3.Left = countPlayer3;
            }
        }
    }
}

Tampilannya nanti akan seperti gambar dibawah ini.

Atau lebih detailnya bisa langsung klik link tutorial di Youtube.

Postingan populer dari blog ini

Dicoding Submission Bookshelf API - Belajar Membuat Aplikasi Back-End untuk Pemula

Tutorial membuat aplikasi chatting Client Visual Studio C# dengan menggunakan koneksi TCP