our notes for our youngers

December 9, 2009

Shooting Game ေလးေရးရေအာင္ – 2

pic1

pic1

download source window apppication here

http://www.mediafire.com/download.php?kyndziv3ijv

ကြၽန္ေတာ္ Exercise Two အေနနဲ႔ ေပးခဲ႔တဲ႔ ေသနတ္ပစ္တဲ႔ game ေလးပါ။ ကြၽန္ေတာ္ video tutorial အေနနဲ႔ လုပ္ေပးခ်င္ေပမယ့္ မိုးကုန္သြားျပီဆိုေတာ့ မီးက အခြင့္အေရး မေပးေတာ့ဘူးဗ်ာ။ ဒါေၾကာင့္ ကုဒ္ေလးကိုပဲ ျမန္ျမန္ကူးလာျပီး တင္ေပးလိုက္ပါတယ္။ ညီတို႔ Program ကို Run ျပီး Code ကို Trace လိုက္ၾကည့္လိုက္ရင္ နားလည္မွာပါ။ မရွင္းတဲ႔ အပိုင္းေလးေတြကို ေကာမန္႔မွာ ျပန္ေမးပါ။ ကြၽန္ေတာ္ ျပန္ရွင္းျပပါ့မယ္။ ေနာက္ထပ္ exercise three အေနနဲ႔ေရာ ဘာေရးရင္ ေကာင္းမလဲ… အၾကံရွိရင္လည္း ေကာ္မန္႔ေပးခဲ႔ၾကပါဦး။

public partial class Form1 : Form
{
    int startCounter = 6;
    int bulletCount = 7;
    int totalPoint = 0;
    int currentPoint = 0;
    int tmr = 0;
    bool PlayingTime = false;
    bool bulletMovingTime = false;
    Random rnd = new Random();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyValue == 113 && !PlayingTime)
        {
            StartGame();
        }
        else if (PlayingTime)
        {
            if (e.KeyValue == 38)
            {
                if (picGun.Top > groupBox1.Height + groupBox1.Top)
                {
                    picGun.Top -= 5;
                    if (!bulletMovingTime)
                    {
                        picMovingBullet.Top -= 5;
                    }
                }
            }
            else if (e.KeyValue == 40)
            {
                if (picGun.Top + picGun.Height < panel1.Top)                 {                     picGun.Top += 5;                     if (!bulletMovingTime)                     {                         picMovingBullet.Top += 5;                     }                 }             }             else if (e.KeyValue == 13 || e.KeyValue == 32)             {                 if (!bulletMovingTime)                 {                     if (bulletCount >= 1)
                    {
                        bulletCount--;
                        lblTotalBullet.Text = bulletCount.ToString();
                        totalPoint += Convert.ToInt32(lblTarget.Text);
                        bulletMovingTime = true;
                        tmrMovingBullet.Enabled = true;
                        picMovingBullet.Left = picGun.Left + picGun.Width;
                        picMovingBullet.Top = picGun.Top + 3;
                        switch (bulletCount)
                        {
                            case 1: picBullet1.Visible = true;
                                picBullet2.Visible = false;
                                picBullet3.Visible = false;
                                picBullet4.Visible = false;
                                picBullet5.Visible = false;
                                picBullet6.Visible = false;
                                picBullet7.Visible = false;
                                break;
                            case 2: picBullet1.Visible = true;
                                picBullet2.Visible = true;
                                picBullet3.Visible = false;
                                picBullet4.Visible = false;
                                picBullet5.Visible = false;
                                picBullet6.Visible = false;
                                picBullet7.Visible = false;
                                break;
                            case 3: picBullet1.Visible = true;
                                picBullet2.Visible = true;
                                picBullet3.Visible = true;
                                picBullet4.Visible = false;
                                picBullet5.Visible = false;
                                picBullet6.Visible = false;
                                picBullet7.Visible = false;
                                break;
                            case 4: picBullet1.Visible = true;
                                picBullet2.Visible = true;
                                picBullet3.Visible = true;
                                picBullet4.Visible = true;
                                picBullet5.Visible = false;
                                picBullet6.Visible = false;
                                picBullet7.Visible = false;
                                break;
                            case 5: picBullet1.Visible = true;
                                picBullet2.Visible = true;
                                picBullet3.Visible = true;
                                picBullet4.Visible = true;
                                picBullet5.Visible = true;
                                picBullet6.Visible = false;
                                picBullet7.Visible = false;
                                break;
                            case 6: picBullet1.Visible = true;
                                picBullet2.Visible = true;
                                picBullet3.Visible = true;
                                picBullet4.Visible = true;
                                picBullet5.Visible = true;
                                picBullet6.Visible = true;
                                picBullet7.Visible = false;
                                break;
                            case 7: picBullet1.Visible = true;
                                picBullet2.Visible = true;
                                picBullet3.Visible = true;
                                picBullet4.Visible = true;
                                picBullet5.Visible = true;
                                picBullet6.Visible = true;
                                picBullet7.Visible = true;
                                break;
                            default: picBullet1.Visible = false;
                                picBullet2.Visible = false;
                                picBullet3.Visible = false;
                                picBullet4.Visible = false;
                                picBullet5.Visible = false;
                                picBullet6.Visible = false;
                                picBullet7.Visible = false;
                                break;
                        }
                    }
                    else
                    {
                        tmrMovingTarget.Enabled = false;
                    }
                }
            }
        }
    }

    private void tmrTimer_Tick(object sender, EventArgs e)
    {
        tmr++;
        lblTimer.Text = tmr.ToString("D4");
        if (currentPoint != 0 && totalPoint != 0)
        {
            decimal accuracy = ((Convert.ToDecimal(currentPoint) / Convert.ToDecimal(totalPoint)) * 100);
            lblAccuracy.Text = Math.Round(accuracy).ToString("N0") + "%";
            lblPoint.Text = currentPoint + "/" + totalPoint;
        }
    }

    private void tmrMovingTarget_Tick(object sender, EventArgs e)
    {
        lblTarget.Top = rnd.Next(groupBox1.Height + groupBox1.Top, panel1.Top - lblTarget.Height);
        lblTarget.Text = Convert.ToString(rnd.Next(1, 6) * 2 - 1);
    }

    private void tmrStartCounter_Tick(object sender, EventArgs e)
    {
        if (startCounter == 6)
        {
            lblReady.Text = "Ready to start";
            startCounter--;
        }
        else if (startCounter >= 1)
        {
            lblReady.Text = (startCounter--).ToString();
        }
        else if (startCounter == 0)
        {
            lblReady.Text = "GO!";
            startCounter--;
        }
        else
        {
            PlayingTime = true;
            tmrMovingTarget.Enabled = true;
            tmrStartCounter.Enabled = false;
            lblReady.Visible = false;
            tmrTimer.Enabled = true;
        }
    }

    private void tmrMovingBullet_Tick(object sender, EventArgs e)
    {
        if (picMovingBullet.Left + picMovingBullet.Width >= this.ClientSize.Width)
        {
            tmrMovingBullet.Enabled = false;
            bulletMovingTime = false;
            picMovingBullet.Left = picGun.Left + picGun.Width;
            picMovingBullet.Top = picGun.Top + 3;
            ShowMessage();
        }
        else if ((picMovingBullet.Left + picMovingBullet.Height >= lblTarget.Left) &&
            (picMovingBullet.Top + picMovingBullet.Height > lblTarget.Top && picMovingBullet.Top < lblTarget.Top + lblTarget.Height))
        {
            currentPoint += Convert.ToInt32(lblTarget.Text);
            lblPoint.Text = currentPoint + "/" + totalPoint;
            tmrMovingBullet.Enabled = false;
            bulletMovingTime = false;
            picMovingBullet.Left = picGun.Left + picGun.Width;
            picMovingBullet.Top = picGun.Top + 3;
            ShowMessage();
        }
        else
            picMovingBullet.Left += 3;
    }

    private void ShowMessage()
    {
        if (bulletCount == 0)
        {
            tmrMovingTarget.Enabled = false;
            tmrTimer.Enabled = false;
            lblPoint.Text = currentPoint + "/" + totalPoint;
            PlayingTime = false;
            decimal accuracy = ((Convert.ToDecimal(currentPoint) / Convert.ToDecimal(totalPoint)) * 100);
            lblAccuracy.Text = Math.Round(accuracy).ToString("N0") + "%";
            MessageBox.Show("Your Score : " + currentPoint + "\nAccuracy : " + Math.Round(accuracy).ToString("N0") + "%\nTotal Time : " + tmr/100 + "." + tmr%100 + " - seconds");
        }
    }

    private void StartGame()
    {
        lblAccuracy.Text = "0%";
        lblPoint.Text = "0/0";
        tmrStartCounter.Enabled = true;
        lblReady.Visible = true;
        startCounter = 6;
        bulletCount = 7;
        totalPoint = 0;
        currentPoint = 0;
        tmr = 0;
        lblTimer.Text = "0000";
        lblTotalBullet.Text = bulletCount.ToString();
        PlayingTime = true;
        picBullet1.Visible = true;
        picBullet2.Visible = true;
        picBullet3.Visible = true;
        picBullet4.Visible = true;
        picBullet5.Visible = true;
        picBullet6.Visible = true;
        picBullet7.Visible = true;
    }
}

December 4, 2009

Basic Window Application (Part 2)

Filed under: Window Form Application — Tags: , , , , — sevenlamp @ 5:29 PM

Event 2 (Mouse Event)

ဒီေန႔ ကြၽန္ေတာ္တို႔ window form application ရဲ႕ event ေတြ အေၾကာင္းပဲ ဆက္ရေအာင္။ အခုကြၽန္ေတာ္က program run လိုက္ျပီး form ေပၚမွာ mouse ေရႊ႕ရင္ အဲ့ဒီ mouse pointer ရဲ႕ location ေလးကို form ရဲ႕ caption မွာ ျပခ်င္တယ္ဆိုပါေတာ့။ ဒါဆိုရင္ ကြၽန္ေတာ္က ဘယ္ event ကိုသံုးရမလဲ ဆိုရင္ Form ရဲ႕ Mouse Move Event ကိုသံုးရပါမယ္။

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
        this.Text = e.Location.ToString();
}

ကြၽန္ေတာ္က form Title bar မွာ မျပခ်င္ဘူး mouse pointer ေလးရဲ႕ ေဘးမွာ ပဲ location ေလးကို ေပၚေနေစခ်င္တယ္ဆိုရင္ေတာ့ Form ေပၚမွာ စာသားေတြ ျပဖို႔အတြက္ Label Control ကိုသံုးျပီး အခုလိုေရးရပါမယ္။

pic1

pic1

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
      label1.Left = e.X + 10;
      label1.Top = e.Y - 10;
      label1.Text = e.Location.ToString();
}

အရင္ဆံုး Form ေပၚမွာ Label တစ္ခုထည့္ရပါမယ္။ Mouse Move event မွာ အဲ့ဒီ Label ေလးရဲ႕ Location ကိုေရႊဖို႔အတြက္ Left နဲ႔ Top ကို mouse pointer လက္ရွိေရာက္ေနတဲ႔ location(x,y) သြားထည့္ေပးလိုက္ပါတယ္။ ဒီေနရာမွာ တစ္ခါတည္း သိလိုက္ဖို႔လိုတာက form ေပၚမွာ control ေတြ ေနရာေရႊ႕ခ်င္ရင္ အဲ့ဒီ control ရဲ႕ left နဲ႔ top ကို ေျပာင္းျခင္းအားျဖင့္ ေရႊ႕နိုင္တယ္ဆိုတာေလးပါ။

November 26, 2009

Shooting Game ေလးေရးရေအာင္

Shooting the best…. ( Exercise Two )

shooting the best...

pic_1

ကြၽန္ေတာ္တို႔ ေလ့က်င့္ခန္းေလး ေနာက္တစ္ခု အေနနဲ႔ ေသနတ္ပစ္တဲ႔ ဂိမ္းေလး ေရးၾကရေအာင္။ ဒီဂိမ္းေလးမွာ Timer Control ကို အဓိကသံုးထားပါတယ္။ ေနာက္တစ္ခုက Form ေပၚမွာ key ႏွိပ္တဲ႔ event မွာ user ႏွိပ္တဲ႔ key ကို ျပန္စစ္တဲ႔ အလုပ္ေလးလည္း ပါတာေပါ့။ ဒီ ၂ ခ်က္ထဲပဲ ေသခ်ာသိဖို႔လိုပါတယ္။ အိုေက… အားလံုးပဲ စမ္းေရးၾကည့္ၾကပါဦး။ ထံုးစံ အတိုင္း ေနာက္တစ္ပါတ္မွာ source code ကိုတင္ေပးပါ့မယ္။ ေလာေလာဆယ္ေတာ့ exe file ေလးကို ေဒါင္းျပီး runၾကည့္ play ၾကည့္ၾကပါဦး။

 

exe file

http://www.mediafire.com/download.php?gnzww3iyjnc

November 6, 2009

Basic Window Application (Part 1)

Filed under: Window Form Application — Tags: , , , , — sevenlamp @ 5:03 PM

Event

Window Form Application ေတြမွာ အေရးၾကီးတဲ႔ အစိတ္အပိုင္း တစ္ခုအေနနဲ႔ သိဖို႔လာလိုလာတာက Event ဆိုတာေလးပါ။ ဟုတ္ျပီ… Event ဆိုတာဘာလဲ? Event ဆိုတာကို အလြယ္ေျပာရရင္ အျဖစ္အပ်က္တစ္ခုလို႔ ေျပာရပါမယ္။ အျပင္ေလာကမွာ မိုးရြာတယ္၊ မိုးခ်ိန္းတယ္၊ ေလတိုက္တယ္၊ အုန္းသီးေၾကြတယ္ :D စသည္ျဖင့္ေပၚ အဲ့ဒီလို ျဖစ္သြားတာေတြ အားလံုးကို Event လို႔ ေျပာလို႔ ရမယ္ထင္ပါတယ္။ ကြၽန္ေတာ္တို႔ Programming မွာ လည္း user ရဲ႕ လုပ္ေဆာင္မႈေပၚမူတည္ျပီး ျဖစ္ပ်က္သြားတာေတြ အားလံုးကို Event လို႔ေခၚပါတယ္။ User က Window Form ေပၚမွာ Mouse ကို ႏွိပ္လိုက္ရင္ ႏွိပ္တယ္ ဆိုတဲ႔ အျဖစ္အပ်က္ျဖစ္ပါတယ္။ ဒီလိုပါပဲ Form ၾကီးကို ပိတ္လိုက္ရင္လည္း Form ပိတ္တယ္ဆိုတဲ႔ အျဖစ္အပ်က္ျဖစ္သြားပါတယ္။ အဲ့ဒီလိုမ်ိဳး User ရဲ႕ action တိုင္း အတြက္ Event ေတြျဖစ္ေနပါတယ္။ အဲ့ဒီလို Event ေတြ ျဖစ္သြားတဲ႔ အခ်ိန္မွာ ကြၽန္ေတာ္တို႔ programmer ေတြက user ေတြကို ျပန္ျပီး ေပးခ်င္တဲ႔ response ေတြ ရွိလာျပီဆိုရင္ေတာ့ အဲ့ဒီ အတြက္ Coding ေတြ ေရးေပးရပါမယ္။ ( ဥပမာ user က form ကို click လုပ္ရင္ Hello လို႔ user ကို ျပခ်င္တယ္။ ဒါမွမဟုတ္ form ေပၚမွာ mouse ေရႊ႕ရင္ mouse pointer ရဲ႕ location ေလး ျပခ်င္တယ္။ စသည္ျဖင့္ေပါ..)။ အဲ့ဒီလို ေရးဖို႔ လုပ္တာကို Event Handling လုပ္တယ္လို႔ ေခၚပါတယ္။ Event ကို Handle လုပ္တာကလည္း အေရးၾကီးပါတယ္။ ကြၽန္ေတာ္တို႔ ကုဒ္ေတြ မေရးခင္ အရင္ဆံုး ဘယ္ event မွာ ေရးရမွာလဲ ဆိုတာကို ခြဲျခားေရြးနိုင္ဖို႔ လိုပါတယ္။ ကြၽန္ေတာ္က Mouse_Click Event မွာ ေရးရမယ္ ကုဒ္ကို Key_Press Event မွာ သြားေရးရင္ Mouse ႏွိပ္တဲ႔အခါ အလုပ္လုပ္မွာ မဟုတ္ပါဘူး။

အခုကြၽန္ေတာ္တို႔ window form ရဲ႕ event ေလးေတြကို နည္းနည္း ေလ့လာလိုက္ရေအာင္။ Form မွာ ပထမဆံုး သိဖို႔လိုတဲ႔ event က Load Event ပါ။ ကြၽန္ေတာ္တို႔ Form object ကို memory ေပၚဆြဲတင္တဲ႔ အခ်ိန္မွာ form load event ျဖစ္ပါတယ္။ form load event က form ၾကီး screen မွာ မေပၚခင္ျဖစ္တာ ျဖစ္တဲ႔အတြက္ေၾကာင္း form မွာ initialize လုပ္ဖို႔ လိုတဲ႔ အပိုင္းေတြကို form load ထဲမွာ ေရးေပးေလ့ရွိတယ္။ ဥပမာ.. ကြၽန္ေတာ္က form ေပၚလာတာနဲ႔ form background color ကို random color ေလး ျဖစ္ေစခ်င္တယ္ဆိုရင္ form load event ထဲမွာ ဒီလိုေလး ေရးေပးရမွာပါ….

private void Form1_Load(object sender, EventArgs e)
{      
        Random rnd = new Random();      
        this.BackColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
}

ဒီလိုပါပဲ.. form ၾကီး ေပၚေနတဲ႔ အခ်ိန္မွာ form ေပၚမွာ click ႏွိပ္တိုင္းမွာလည္း ကာလာေျပာင္းေနခ်င္တယ္ ဆိုရင္ Form ရဲ႕ Click Event မွာပါ သြားေရးေပးရမွာပါ။

private void Form1_Click(object sender, EventArgs e)
{
        Random rnd = new Random();      
        this.BackColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
}

အခုကြၽန္ေတာ္က form ကို user က ပိတ္လိုက္တဲ႔ အခ်ိန္မွာ user ကို ပိတ္မွာ ေသခ်ာလားဆိုျပီး confirm message ေလး ေမးခ်င္တယ္ဆိုပါေတာ့… ဒါဆိုရင္ ဘယ္မွာ ေရးရမွာလဲ ? ဟုတ္ကဲ႔ Form ရဲ႕ FormClosing Event မွာ ေရးရပါမယ္။ ဒီလိုမ်ိဳးပါ…

Pic_1

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{     
       DialogResult res = MessageBox.Show("Sure to close?", "Confirm...",
                         MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
       if (res == DialogResult.No)
               e.Cancel = true;
}

MessageBox class ထဲက show function ေလးကို ေခၚလိုက္ရင္ ကြၽန္ေတာ္တို႔ form ေပၚမွာ dialog form ေလး တစ္ခု ျပေပးပါတယ္။ dialog form ဆိုတာက အဲ့ဒီ form ေပၚေနရင္ တျခား form ေတြကို အလုပ္ဆက္လုပ္လို႔ မရပါဘူး။ အဲ့ဒီ dialog form ပိတ္သြားမွ အလုပ္ဆက္လုပ္လို႔ ရပါတယ္။ အဲ့ဒီ MessageBox form ေလးရဲ႕ Show function မွာ အဓိက parameter ၄ ခု ေပးလို႔ ရပါတယ္။

၁ – ေပၚခ်င္တဲ႔ Message ( here : Sure to close? )
၂ – Title bar Text ( here : Confirm… )
၃ – ေပၚခ်င္တဲ႔ Button အမ်ိဳးအစားေတြ ( here : Yes and No )
MessageBoxButtons ဆိုတဲ႔ enum ေလးထဲက တန္ဖိုး တစ္ခုခုေရြးေပးရပါမယ္။ default ကေတာ့ OK button ပါ။
၄ – ေပၚခ်င္တဲ႔ Icon ( here : Warning )
ဒါလည္း တူတူပါပဲ… MessageBoxIcon အမ်ိဳးအစား enum ပါ။

ဒီ parameter ၄ ခုကို နံပါတ္ ၁ တစ္ခုထဲ ေပးမယ္ဆိုလည္း ရပါတယ္။ ဒါေပမယ့္ နံပါတ္ ၂ ပဲေပးမယ္ဆိုရင္ေတာ့ မရပါဘူး။ နံပါတ္ ၃ ကိုေပးခ်င္တယ္ဆိုရင္ နံပါတ္ ၁ ေရာ နံပါတ္ ၂ ပါေပးမွ ရပါမယ္။

Dialog form ေတြအားလံုး ပိတ္သြားတဲ႔ အခ်ိန္မွာ DialogResult အမ်ိဳးအစား enum တစ္ခု return ျပန္ပါတယ္။ အခု ျပန္လာတဲ႔ return value ကို ကြၽန္ေတာ္က No နဲ႔ ညီလား ဆိုျပီး စစ္လိုက္ပါတယ္။ (ကြၽန္ေတာ္က MessageBoxButtons မွာ YesNo ကိုေရြးလိုက္တဲ႔အတြက္ Return value ဟာ Yes ေသာ္လည္းေကာင္း၊ No ေသာ္လည္းေကာင္း return ျပန္ပါ့မယ္။ ) ကြၽန္ေတာ္တို႔ အခုေရးေနတဲ႔ Event က form ပိတ္တဲ႔ event ျဖစ္တဲ႔အတြက္ User က Yes ကိုႏွိပ္ရင္ ကြၽန္ေတာ္တို႔က ဘာမွ လုပ္ေပးစရာ မလိုပါဘူး။ အဲ.. User က No ကိုႏွိပ္ရင္ေတာ့ ကြၽန္ေတာ္တို႔က Form မပိတ္ေအာင္ e.Cancel ထဲကို true value သြားထည့္ေပးရပါတယ္။ ဒါဆို form မပိတ္ေတာ့ပါဘူး။ ခုေလာက္ဆို Event ဆိုတာကို နည္းနည္း နားလည္လာမယ္ ထင္ပါတယ္။

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.