2014年1月28日 星期二

[C#] msdn - 迷宮遊戲 (類似電流急急棒?)



Step by Step
學習 撰寫自己的方法 (own function)
學習 滑鼠碰觸事件
學習 播放音效

因為使用者調整視窗大小時,(面板)(Panel)就會沒有用處
所以當表單為固定大小,可以用Panel這個容器



------------------------------------------------

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

namespace Maze
{
    public partial class Form1 : Form
    {
        // This SoundPlayer plays a sound whenever the player hits a wall.
        System.Media.SoundPlayer startSoundPlayer = new System.Media.SoundPlayer(@"C:\windows\media\chord.wav");

        // This SoundPlayer plays a sound when the player finishes the game.
        System.Media.SoundPlayer finishSoundPlayer = new System.Media.SoundPlayer(@"C:\windows\media\tada.wav");

        public Form1()
        {
            InitializeComponent();
            MoveToStart();
        }

        private void finishLabel_MouseEnter(object sender, EventArgs e)
        {
            // Play a sound, show a congratulatory MessageBox, then close the form.
            finishSoundPlayer.Play();
            MessageBox.Show("   Congratulations!\n" +
                            "    恭喜過關!\n\n" +
                            "試試看能否找到別的路線吧!");
            //Close();//若想要過關就關閉程式,則用Close();
            MoveToStart();
        }

        /// 
        /// Play a sound, then move the mouse pointer to a point 10 pixels down and to 
        /// the right of the starting point in the upper-left corner of the maze.
        ///  
        private void MoveToStart()
        {
            startSoundPlayer.Play();
            Point startingPoint = panel1.Location;
            startingPoint.Offset(10, 10);
            Cursor.Position = PointToScreen(startingPoint);
        }

        private void wall_MouseEnter(object sender, EventArgs e)
        {
            // When the mouse pointer hits a wall or enters the panel,
            // call the MoveToStart() method.
            MoveToStart();
        }

    }
}

沒有留言: