MusicData类库 public class Music_Entity { /// <summary> /// 音乐名 /// </summary> public static List<string> MusicName = new List<string>(); /// <summary> /// 音乐路径 /// </summary> public static List<string> MusicPath = new List<string>(); /// <summary> /// 音乐背景 /// </summary> public static List<string> MusicImg = new List<string>(); /// <summary> /// 音乐节奏 /// </summary> public static List<string> MusicHit = new List<string>(); } public class MusicDataHelper { /// <summary> /// 音乐数据读取路径 /// </summary> private static string inPath = @"./MusicSet.xml"; /// <summary> /// 读取数据 /// </summary> public MusicDataHelper() { string name = string.Empty; string path = string.Empty; string img = string.Empty; string hit = string.Empty; // 加载指定的xml文件 XDocument xml = XDocument.Load(inPath); // 使用查询语法获取music集合 var Music = from p in xml.Root.Elements("Music") select new { name = p.Element("MusicName").Value, path = p.Element("MusicPath").Value, img = p.Element("MusicImg").Value, hit = p.Element("MusicHit").Value }; foreach (var i in Music) { Music_Entity.MusicName.Add(i.name); Music_Entity.MusicPath.Add(i.path); Music_Entity.MusicImg.Add(i.img); Music_Entity.MusicHit.Add(i.hit); } } 启动窗体 public FrmStartup() { InitializeComponent(); timer1.Enabled = true; } /// <summary> /// 时间单位为 1 /s秒 /// </summary> private int t = 0; private static FrmMain ob_frmmain; private void StartGame() { if (ob_frmmain == null || ob_frmmain.IsDisposed) { ob_frmmain = new FrmMain(); ob_frmmain.Show(); this.Hide(); } else { ob_frmmain.Activate(); } } private void FrmStart_Load(object sender, EventArgs e) { ScreenSet _ScreenSet = new ScreenSet(this); _ScreenSet.Fill(); int ScreenHeight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height; //显示器高度 int ScreenWidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width; //显示器宽度 picStart.Location = new Point(ScreenWidth / 2 - picStart.Width / 2, ScreenHeight / 2 - picStart.Height / 2); } private void FrmStart_KeyDown(object sender, KeyEventArgs e) { if(e.KeyCode == Keys.Escape) { this.StartGame(); } } private void timer1_Tick(object sender, EventArgs e) { t++; if (t >=10) { this.StartGame(); timer1.Enabled = false; } } } 主窗体(选择菜单) public FrmMain() { InitializeComponent(); if (!MusicDao.isAdd) { _MusicDataHelper = new MusicDataHelper(); MusicDao.isAdd = true; } this.GetMusicData(); cmbHitDisplay.SelectedIndex = 1; } private static MusicDataHelper _MusicDataHelper; private static FrmGame ob_frmgame; /// <summary> /// 按键音乐 /// </summary> private string hitMusicPath; /// <summary> /// 判断是否退出游戏 /// </summary> private bool isExit = false; /// <summary> /// 判断是否全屏 /// </summary> private bool isFill = true; /// <summary> /// 读取音乐数据 /// </summary> private void GetMusicData() { foreach (string i in Music_Entity.MusicName) { lstMusic.Items.Add(i); } } /// <summary> /// 音乐背景显示 /// </summary> /// <param name="temp"></param> private void MusicImgDisplay(string temp) { string path = Application.StartupPath + temp; picMusic.Image = Image.FromFile(path); } private void ExitThis() { if (MessageBox.Show("这么好玩的游戏,你也想跑?", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No) { isExit = true; } this.Close(); } private void lstMusic_SelectedValueChanged(object sender, EventArgs e) { try { int index = lstMusic.SelectedIndex; MusicDao.music_name = lstMusic.SelectedItem.ToString(); MusicDao.music_hit = Music_Entity.MusicHit[index]; MusicDao.music_path = Music_Entity.MusicPath[index]; string music_img = Music_Entity.MusicImg[index]; hitMusicPath = "//HitMusic//Button.wav"; MPlayer.MusicPlay(axHitMplayer, hitMusicPath); MPlayer.MusicPlay(axMplayer, MusicDao.music_path); this.MusicImgDisplay(music_img); } catch { } } private void cmbHitDisplay_SelectedIndexChanged(object sender, EventArgs e) { try { switch (cmbHitDisplay.SelectedIndex+1) { case 1: MusicDao.music_hitImg = Application.StartupPath + "//HitImg//s1.gif"; break; case 2: MusicDao.music_hitImg = Application.StartupPath + "//HitImg//s2.gif"; break; case 3: MusicDao.music_hitImg = Application.StartupPath + "//HitImg//s3.gif"; break; case 4: MusicDao.music_hitImg = Application.StartupPath + "//HitImg//s4.gif"; break; case 5: MusicDao.music_hitImg = Application.StartupPath + "//HitImg//s5.gif"; break; case 6: MusicDao.music_hitImg = Application.StartupPath + "//HitImg//s6.gif"; break; } picDisplay.Image = Image.FromFile(MusicDao.music_hitImg); } catch{} } private void btnStart_Click(object sender, EventArgs e) { MPlayer.MusicStop(axMplayer); hitMusicPath = "//HitMusic//Go.mp3"; MPlayer.MusicPlay(axHitMplayer, hitMusicPath); if (MusicDao.music_name != null) { ob_frmgame = new FrmGame(isFill); ob_frmgame.Show(); this.Hide(); } else { MessageBox.Show("Music都没有,咋样游戏吖?"); return; } } private void FrmMain_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Escape) this.ExitThis(); } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { this.ExitThis(); } private void 窗口模式ToolStripMenuItem1_Click(object sender, EventArgs e) { isFill = false; ScreenSet _ScreenSet = new ScreenSet(this); _ScreenSet.Normal(); } private void 全屏模式ToolStripMenuItem_Click(object sender, EventArgs e) { isFill = true; ScreenSet _ScreenSet = new ScreenSet(this); _ScreenSet.Fill(); } private void 关于ToolStripMenuItem_Click(object sender, EventArgs e) { ABout about = new ABout(); about.TopMost = true; about.ShowDialog(); } private void FrmMain_FormClosing(object sender, FormClosingEventArgs e) { if (isExit) { e.Cancel = true; isExit = false; } else Application.Exit(); } } 游戏窗体 public FrmGame(bool isFill) { InitializeComponent(); _MusicHelper = new MusicHelper(this); MPlayer.MusicPlay(axMusicPlayer, MusicDao.music_path); ScreenSet _ScreenSet = new ScreenSet(this); if (isFill) _ScreenSet.Fill(); else _ScreenSet.Normal(); } private static MusicHelper _MusicHelper; private static FrmMain ob_frmMain = new FrmMain(); private int score = 0; private int hit = 0; private void FrmGame_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.NumPad1: UserKeyDown(picNum1); break; case Keys.NumPad2: UserKeyDown(picNum2); break; case Keys.NumPad3: UserKeyDown(picNum3); break; case Keys.NumPad4: UserKeyDown(picNum4); break; case Keys.NumPad5: UserKeyDown(picNum5); break; case Keys.NumPad6: UserKeyDown(picNum6); break; case Keys.NumPad7: UserKeyDown(picNum7); break; case Keys.NumPad8: UserKeyDown(picNum8); break; case Keys.NumPad9: UserKeyDown(picNum9); break; } } /// <summary> /// 玩家按键 /// </summary> /// <param name="p"></param> private void UserKeyDown(PictureBox p) { if (p.Image != null) { score += 100; hit++; p.Image = null; score = score + hit; lblScore.Text = score.ToString() + "分"; } else { hit = 1; } lblHit.Text = hit.ToString(); } private void FrmGame_FormClosing(object sender, FormClosingEventArgs e) { Application.Exit(); } private void btnBack_Click(object sender, EventArgs e) { _MusicHelper.MusicExit(); } } 节奏数据类 class HitDataDao { /// <summary> /// 音乐节奏组 /// </summary> public List<string> MusicHits = new List<string>(); /// <summary> /// 节奏出现时间 /// </summary> public List<int> MusicTimer = new List<int>(); /// <summary> /// 音乐节奏路径 /// </summary> private static string MusciHitpath; public HitDataDao() { int index = 0; MusciHitpath = Application.StartupPath + MusicDao.music_hit; StreamWriter sw = new StreamWriter(MusciHitpath, true); sw.Close(); StreamReader sr = new StreamReader(MusciHitpath); string line; string[] str = new string[65530]; while ((line = sr.ReadLine()) != null) { str = line.Split('|'); } sr.Close(); foreach (string i in str) { if (index % 2 == 0) MusicHits.Add(i); else MusicTimer.Add(int.Parse(i)); index++; } } } 音乐播放类 /// <summary> /// 音乐播放 /// </summary> /// <param name="temp"></param> public static void MusicPlay(AxWindowsMediaPlayer axMplayer,string temp) { string path = Application.StartupPath + temp; axMplayer.URL = path; } /// <summary> /// 音乐停止 /// </summary> public static void MusicStop(AxWindowsMediaPlayer axMplayer) { axMplayer.URL = ""; } 音乐数据类 public class MusicDao { /// <summary> /// 音乐路径 /// </summary> public static string music_path { get; set; } /// <summary> /// 音乐名字 /// </summary> public static string music_name { get; set; } /// <summary> /// 音乐节奏 /// </summary> public static string music_hit { get; set; } /// <summary> /// 按键效果图 /// </summary> public static string music_hitImg { get; set; } /// <summary> /// 是否已加载 /// </summary> public static bool isAdd = false; } 屏幕模式类 private static Form frm; public ScreenSet(Form from) { frm = from; } /// <summary> /// 全屏模式 /// </summary> public void Fill() { frm.FormBorderStyle = FormBorderStyle.None; frm.WindowState = FormWindowState.Maximized; frm.TopMost = true; //这个一定要设置,不然一般会出现Bug } /// <summary> /// 窗口模式 /// </summary> public void Normal() { frm.FormBorderStyle = FormBorderStyle.Sizable; frm.WindowState = FormWindowState.Normal; } 整个乐动魔方核心 逻辑处理 class MusicHelper { private static FrmGame frm; private static FrmMain ob_frmMain = new FrmMain(); private HitDataDao _HitDataDao = new HitDataDao(); /// <summary> /// 音乐节奏时间器 /// </summary> private Timer tmMusic = new Timer(); /// <summary> /// 按键效果时间器 /// </summary> private Timer tmStop; /// <summary> /// 效果图暂存类 /// </summary> private List<PictureBox> picTemp = new List<PictureBox>(); /// <summary> /// 音乐效果图索引 /// </summary> private int _HitImg = 0; private int Img_T = 0; private int Music_T = 0; private int _HitIndex = 0; public MusicHelper(FrmGame from) { frm = from; tmMusic.Enabled = true; tmMusic.Interval = 100; tmMusic.Tick += new EventHandler(tmMusic_Tick); tmMusic.Start(); } /// <summary> /// 效果图出现位置 /// </summary> private void MusicHitDisplay() { foreach (PictureBox j in frm.panMusic.Controls) { if (j.Tag.ToString() == _HitDataDao.MusicHits[_HitIndex]) { j.Image = Image.FromFile(MusicDao.music_hitImg); picTemp.Add(j); tmStop = new Timer(); tmStop.Enabled = true; tmStop.Interval = 100; tmStop.Tick += new EventHandler(tmStop_Tick); Img_T = new int(); break; } } } private void tmMusic_Tick(object sender, EventArgs e) { Music_T++; try { if (Music_T == _HitDataDao.MusicTimer[_HitIndex]) { this.MusicHitDisplay(); _HitIndex++; } if (_HitIndex >= _HitDataDao.MusicTimer.Count) { MusicExit(); } } catch{} } public void MusicExit() { tmMusic.Enabled = false; foreach (PictureBox i in picTemp) { i.Image = null; } _HitIndex = 0; _HitImg = 0; Music_T = 0; Img_T = 0; ob_frmMain.Show(); frm.Dispose(); } private void tmStop_Tick(object sender, EventArgs e) { Img_T++; if (Img_T == 15) { picTemp[_HitImg].Image = null; _HitImg++; tmStop.Enabled = false; } } } 读取配置文件时,使用鸟LINQ to XML, 需要.net 3.0 以上 下载地址 http://www.vdisk.cn/down/index/6460602A4260 http://www.brsbox.com/filebox/down/fc/a07d2736bf617e7c59c07c5155131c42 name:5+x