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;
    }
}

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

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

Follow

Get every new post delivered to your Inbox.