C#贪吃蛇游戏之--界面层(五)
来源:互联网 发布:map生成json 编辑:程序博客网 时间:2024/05/16 14:26
界面层调用逻辑层定义好的方法即可,相对比较简单,直接帖代码就好了!
界面暂且设计成上面的样子,不怎么美观,根据个人爱好设置了!
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! ^-^
源代码下载:http://download.csdn.net/source/2815215
- C#贪吃蛇游戏之--界面层(五)
- C#贪吃蛇游戏之--实体对象层(二)
- C#贪吃蛇游戏之--逻辑层(三)
- C#贪吃蛇游戏之--游戏信息存取(四)
- C#贪吃蛇游戏之--构想篇(一)
- c#编写的贪吃蛇游戏
- C#编写贪吃蛇游戏 RedSnake
- 用C#实现贪吃蛇游戏
- c#简单贪吃蛇游戏的思想
- 【游戏算法之贪吃蛇】我想的贪吃蛇算法
- 编程实现贪吃蛇游戏界面的主要代码
- linux字符界面下贪吃蛇游戏…
- C语言之贪吃蛇游戏源码
- Java之贪吃蛇游戏的开发
- C语言之贪吃蛇游戏
- C#游戏编程:《控制台小游戏系列》之《六、贪吃蛇实例》
- 纸牌游戏之五 主界面类
- MFC实现贪吃蛇游戏之游戏窗口
- Core Data in IOS
- 超级玛丽跳 时间的优化
- C#贪吃蛇游戏之--游戏信息存取(四)
- Google需要从Android得到什么?
- VMWare中ubuntu打开终端方法
- C#贪吃蛇游戏之--界面层(五)
- Uva 10534 波浪子序列(二次快速LIS)
- 1007 最短路
- 界面编程之基本界面组件(7)ImageView(图像视图)
- 搭建 struts2 架构
- URAL 1018 Binary Apple Tree
- 深入分析Java对象的建构顺序
- dwz+thinkphp 我的视图测试
- 普通 56K猫拔号上网工具 wvdial 介绍