界面層調用邏輯層定義好的方法即可,相對比較簡單,直接帖代碼就好了!
界面暫且設計成上面的樣子,不怎麼美觀,根據個人愛好設置了!
MySnakeUI.cs
- public partial class MySnakeUI : Form
- {
- //遊戲邏輯類
- GameLogic gameLogic;
- //遊戲信息存取類
- SaveGameInfo saveInfo;
- int windowW = 250;
- int windowH = 350;
- bool IsStop = false;
- //最高積分
- private int largeScore;
- //存放角色和積分信息
- private string[] scores;
- private Timer gameTimer;
- public MySnakeUI()
- {
- InitializeComponent();
- panGameInfo.Visible = false;
- saveInfo = new SaveGameInfo();
- LoadGameInfo();
- gameTimer = new Timer();
- gameTimer.Interval = (int)SnakeState.Normal;
- gameTimer.Tick+=new EventHandler(gameTimer_Tick);
- }
- private void gameTimer_Tick(object sender, EventArgs e)
- {
- gameLogic.SnakeMove();
- lbScore.Text = gameLogic.GetFenShu();
- if (gameLogic.GameOver)
- {
- gameTimer.Enabled = false;
- lbGOver.Text = "Game Over!";
- lbGOver.Location = new Point(panel1.Width / 2 - 35, panel1.Height / 2 - 40);
- lbGOver.Visible = true;
- cbBoxBackW.Enabled = true;
- cbBoxBackH.Enabled = true;
- cbBocSpeed.Enabled = true;
- btnOK.Enabled = true;
- btnStart.Enabled = true;
- if (Convert.ToInt16(lbScore.Text) > largeScore)
- {
- lbGOver.Visible = false;
- panGameInfo.Visible = true;
- txtNewScore.Text = lbScore.Text;
- btnSave.Enabled = true;
- LoadGameHeros();
- }
- }
- }
- //開始遊戲
- public void GameStart()
- {
- lbGOver.Text = "";
- lbGOver.Location = new Point(180, 1);
- cbBoxBackW.Enabled = false;
- cbBoxBackH.Enabled = false;
- cbBocSpeed.Enabled = false;
- btnOK.Enabled = false;
- if (gameLogic != null)
- gameLogic.CrearAll();
- gameLogic = new GameLogic(panel1, windowW, windowH);
- gameLogic.InitGame();
- gameTimer.Start();
- }
- /// <summary>
- /// 菜單選項
- /// </summary>
- private void MenuBegin_Click(object sender, EventArgs e)
- {
- GameStart();
- }
- private void 退出EToolStripMenuItem_Click(object sender, EventArgs e)
- {
- this.Dispose();
- Application.Exit();
- }
- private void 排行榜PToolStripMenuItem_Click(object sender, EventArgs e)
- {
- panGameInfo.Visible = true;
- btnSave.Enabled = false;
- LoadGameHeros();
- lbGOver.Visible = false;
- }
- private void 最高分HToolStripMenuItem_Click(object sender, EventArgs e)
- {
- MessageBox.Show("最高分:" + "" + largeScore.ToString() + "/n/n" + "玩家:" + "" + lbPlayer.Text);
- }
- private void 關於遊戲GToolStripMenuItem_Click(object sender, EventArgs e)
- {
- AboutGame form = new AboutGame();
- form.ShowDialog();
- }
- //方向鍵
- private void MySnakeUI_KeyDown(object sender, KeyEventArgs e)
- {
- switch (e.KeyCode)
- {
- case Keys.Up:
- if (gameLogic.SnakeDirection != SnakeDirection.Down)
- gameLogic.SnakeDirection = SnakeDirection.Up;
- break;
- case Keys.Down:
- if (gameLogic.SnakeDirection != SnakeDirection.Up)
- gameLogic.SnakeDirection = SnakeDirection.Down;
- break;
- case Keys.Left:
- if (gameLogic.SnakeDirection != SnakeDirection.Right)
- gameLogic.SnakeDirection = SnakeDirection.Left;
- break;
- case Keys.Right:
- if (gameLogic.SnakeDirection != SnakeDirection.Left)
- gameLogic.SnakeDirection = SnakeDirection.Right;
- break;
- default:
- break;
- }
- }
- //配置速度
- private void cbBocSpeed_SelectedIndexChanged(object sender, EventArgs e)
- {
- switch (cbBocSpeed.SelectedItem.ToString())
- {
- case "非常慢":
- gameTimer.Interval = 150;
- break;
- case "比較慢":
- gameTimer.Interval = 100;
- break;
- case "快":
- gameTimer.Interval = 70;
- break;
- case "很快":
- gameTimer.Interval = 50;
- break;
- case "超快":
- gameTimer.Interval = 20;
- break;
- default:
- break;
- }
- }
- //配置遊戲場景
- private void btnOK_Click(object sender, EventArgs e)
- {
- if (cbBoxBackW.SelectedIndex==-1) return;
- if (cbBoxBackH.SelectedIndex==-1) return;
- windowW = Convert.ToInt16(cbBoxBackW.SelectedItem.ToString());
- windowH = Convert.ToInt16(cbBoxBackH.SelectedItem.ToString());
- //稍微調整了大小,有點勉強
- panel1.Width = (windowW == 250 || windowW == 300) ? windowW + 7 : (windowW == 350) ? windowW + 15 : windowW + 22;
- this.Height = (windowH == 300) ? windowH + 51 : (windowH == 350) ? windowH + 57 : windowH + 62;
- this.Width = panel1.Width + 118;
- }
- //信息保存
- private void btnSave_Click(object sender, EventArgs e)
- {
- saveInfo = new SaveGameInfo();
- if (txtPlayer.Text == "")
- txtPlayer.Text = "隱姓埋名";
- char[] ch = (txtPlayer.Text).ToCharArray();
- string str="";
- //過濾'-'
- foreach (char c in ch)
- {
- if (c == '-')
- continue;
- str += c;
- }
- if (saveInfo.SaveInfo(txtNewScore.Text, str))
- MessageBox.Show("保存成功!");
- btnSave.Enabled = false;
- LoadGameHeros();
- }
- //關閉信息保存
- private void btnClose_Click(object sender, EventArgs e)
- {
- panGameInfo.Visible = false;
- lbGOver.Visible = true;
- LoadGameInfo();
- btnOK.Focus();
- }
- //加載遊戲信息
- public void LoadGameInfo()
- {
- scores = saveInfo.ReadInfo();
- if (scores != null)
- {
- if (scores.Length > 1)
- {
- string[] scoreAndplayer = new string[] { };
- foreach (string s in scores)
- {
- if (s == "")
- continue;
- scoreAndplayer = s.Split(';');
- }
- lbTScore.Text = scoreAndplayer[0];
- lbPlayer.Text = scoreAndplayer[1];
- largeScore = Convert.ToInt16(lbTScore.Text);
- }
- else
- {
- lbTScore.Text = "加載中…";
- lbPlayer.Text = "加載中…";
- }
- }
- else
- {
- lbTScore.Text = "加載中…";
- lbPlayer.Text = "加載中…";
- }
- }
- //加載英雄榜
- public void LoadGameHeros()
- {
- scores = saveInfo.ReadInfo();
- if (scores != null)
- {
- if (scores.Length > 1)
- {
- if (listHeros.Items.Count > 0)
- {
- listHeros.Items.Clear();
- }
- for (int i = scores.Length - 2; i >= 0; i--)
- {
- listHeros.Items.Add("No." + (scores.Length - i - 1) + " " + scores[i]);
- }
- }
- else
- {
- listHeros.Items.Add("英雄,去創造奇蹟吧!");
- }
- }
- else
- {
- listHeros.Items.Add("英雄,去創造奇蹟吧!");
- }
- }
- /// <summary>
- /// 按方向鍵時焦點會轉移到Enabel=true的控件上,導致無法改變方向,
- /// 所以乾脆用圖片替換Button控件了,下面是圖片按鈕的一些樣式.
- /// </summary>
- private void btnStart_Click(object sender, EventArgs e)
- {
- if (IsStop == true)
- {
- gameTimer.Enabled = true;
- btnStop.Enabled = true;
- btnStart.Enabled = false;
- }
- else
- GameStart();
- btnStart.Enabled = false;
- IsStop = false;
- }
- private void btnStart_MouseMove(object sender, MouseEventArgs e)
- {
- Graphics g;
- g = btnStart.CreateGraphics();
- Pen p = new Pen(Color.Orange,3);
- g.DrawRectangle(p,0,0,btnStart.Width,btnStart.Height);
- }
- private void btnStart_MouseLeave(object sender, EventArgs e)
- {
- btnStart.Invalidate();
- }
- private void btnStop_Click(object sender, EventArgs e)
- {
- gameTimer.Enabled = false;
- IsStop =true;
- btnStop.Enabled = false;
- btnStart.Enabled = true;
- }
- private void btnStop_MouseMove(object sender, MouseEventArgs e)
- {
- Graphics g;
- g = btnStop.CreateGraphics();
- Pen p = new Pen(Color.Orange, 2);
- g.DrawRectangle(p, 1, 1, btnStart.Width-2, btnStart.Height-1);
- }
- private void btnStop_MouseLeave(object sender, EventArgs e)
- {
- btnStop.Invalidate();
- }
- }
運行界面:
至此,一款貪喫蛇小遊戲就完成了,目前只有一個關卡,界面也較爲普通,這些以後會改善之。
編譯環境:VS2008,c#
Over! ^-^