C#贪吃蛇游戏之--界面层(五)

来源:互联网 发布:map生成json 编辑:程序博客网 时间:2024/05/16 14:26

界面层调用逻辑层定义好的方法即可,相对比较简单,直接帖代码就好了!

    界面暂且设计成上面的样子,不怎么美观,根据个人爱好设置了!

MySnakeUI.cs

  1. public partial class MySnakeUI : Form  
  2. {  
  3.     //游戏逻辑类  
  4.     GameLogic gameLogic;  
  5.     //游戏信息存取类  
  6.     SaveGameInfo saveInfo;  
  7.     int windowW = 250;  
  8.     int windowH = 350;  
  9.     bool IsStop = false;  
  10.     //最高积分  
  11.     private int largeScore;  
  12.     //存放角色和积分信息  
  13.     private string[] scores;  
  14.     private Timer gameTimer;  
  15.   
  16.     public MySnakeUI()  
  17.     {  
  18.         InitializeComponent();  
  19.         panGameInfo.Visible = false;  
  20.         saveInfo = new SaveGameInfo();  
  21.         LoadGameInfo();  
  22.         gameTimer = new Timer();  
  23.         gameTimer.Interval = (int)SnakeState.Normal;  
  24.         gameTimer.Tick+=new EventHandler(gameTimer_Tick);             
  25.    }  
  26.   
  27.     private void gameTimer_Tick(object sender, EventArgs e)  
  28.     {  
  29.         gameLogic.SnakeMove();  
  30.         lbScore.Text = gameLogic.GetFenShu();  
  31.         if (gameLogic.GameOver)  
  32.         {  
  33.             gameTimer.Enabled = false;  
  34.             lbGOver.Text = "Game Over!";  
  35.             lbGOver.Location = new Point(panel1.Width / 2 - 35, panel1.Height / 2 - 40);  
  36.             lbGOver.Visible = true;  
  37.             cbBoxBackW.Enabled = true;  
  38.             cbBoxBackH.Enabled = true;  
  39.             cbBocSpeed.Enabled = true;  
  40.             btnOK.Enabled = true;  
  41.             btnStart.Enabled = true;  
  42.             if (Convert.ToInt16(lbScore.Text) > largeScore)  
  43.             {  
  44.                 lbGOver.Visible = false;  
  45.                 panGameInfo.Visible = true;  
  46.                 txtNewScore.Text = lbScore.Text;  
  47.                 btnSave.Enabled = true;  
  48.                 LoadGameHeros();  
  49.             }  
  50.         }  
  51.     }  
  52.   
  53.     //开始游戏  
  54.     public void GameStart()  
  55.     {  
  56.         lbGOver.Text = "";  
  57.         lbGOver.Location = new Point(180, 1);  
  58.         cbBoxBackW.Enabled = false;  
  59.         cbBoxBackH.Enabled = false;  
  60.         cbBocSpeed.Enabled = false;  
  61.         btnOK.Enabled = false;  
  62.         if (gameLogic != null)  
  63.             gameLogic.CrearAll();  
  64.         gameLogic = new GameLogic(panel1, windowW, windowH);  
  65.         gameLogic.InitGame();  
  66.         gameTimer.Start();  
  67.     }  
  68.   
  69.     /// <summary>  
  70.     /// 菜单选项  
  71.     /// </summary>  
  72.     private void MenuBegin_Click(object sender, EventArgs e)  
  73.     {  
  74.         GameStart();  
  75.     }  
  76.   
  77.     private void 退出EToolStripMenuItem_Click(object sender, EventArgs e)  
  78.     {  
  79.         this.Dispose();  
  80.         Application.Exit();  
  81.     }  
  82.   
  83.     private void 排行榜PToolStripMenuItem_Click(object sender, EventArgs e)  
  84.     {  
  85.         panGameInfo.Visible = true;  
  86.         btnSave.Enabled = false;  
  87.         LoadGameHeros();  
  88.         lbGOver.Visible = false;  
  89.     }  
  90.   
  91.     private void 最高分HToolStripMenuItem_Click(object sender, EventArgs e)  
  92.     {  
  93.         MessageBox.Show("最高分:" + "" + largeScore.ToString() + "/n/n" + "玩家:" + "" + lbPlayer.Text);  
  94.     }  
  95.   
  96.     private void 关于游戏GToolStripMenuItem_Click(object sender, EventArgs e)  
  97.     {  
  98.         AboutGame form = new AboutGame();  
  99.         form.ShowDialog();  
  100.     }  
  101.   
  102.     //方向键  
  103.     private void MySnakeUI_KeyDown(object sender, KeyEventArgs e)  
  104.     {  
  105.         switch (e.KeyCode)  
  106.         {  
  107.             case Keys.Up:  
  108.                 if (gameLogic.SnakeDirection != SnakeDirection.Down)  
  109.                 gameLogic.SnakeDirection = SnakeDirection.Up;  
  110.                 break;  
  111.             case Keys.Down:  
  112.                 if (gameLogic.SnakeDirection != SnakeDirection.Up)  
  113.                     gameLogic.SnakeDirection = SnakeDirection.Down;  
  114.                 break;  
  115.             case Keys.Left:  
  116.                 if (gameLogic.SnakeDirection != SnakeDirection.Right)  
  117.                     gameLogic.SnakeDirection = SnakeDirection.Left;  
  118.                 break;  
  119.             case Keys.Right:  
  120.                 if (gameLogic.SnakeDirection != SnakeDirection.Left)  
  121.                     gameLogic.SnakeDirection = SnakeDirection.Right;  
  122.                 break;  
  123.             default:  
  124.                 break;  
  125.         }  
  126.     }  
  127.   
  128.     //配置速度  
  129.     private void cbBocSpeed_SelectedIndexChanged(object sender, EventArgs e)  
  130.     {  
  131.         switch (cbBocSpeed.SelectedItem.ToString())  
  132.         {  
  133.             case "非常慢":  
  134.                 gameTimer.Interval = 150;  
  135.                 break;  
  136.             case "比较慢":  
  137.                 gameTimer.Interval = 100;  
  138.                 break;  
  139.             case "快":  
  140.                 gameTimer.Interval = 70;  
  141.                 break;  
  142.             case "很快":  
  143.                 gameTimer.Interval = 50;  
  144.                 break;  
  145.             case "超快":  
  146.                 gameTimer.Interval = 20;  
  147.                 break;  
  148.             default:  
  149.                 break;  
  150.         }  
  151.     }  
  152.   
  153.     //配置游戏场景  
  154.     private void btnOK_Click(object sender, EventArgs e)  
  155.     {  
  156.         if (cbBoxBackW.SelectedIndex==-1) return;  
  157.         if (cbBoxBackH.SelectedIndex==-1) return;  
  158.         windowW = Convert.ToInt16(cbBoxBackW.SelectedItem.ToString());  
  159.         windowH = Convert.ToInt16(cbBoxBackH.SelectedItem.ToString());  
  160.         //稍微调整了大小,有点勉强  
  161.         panel1.Width = (windowW == 250 || windowW == 300) ? windowW + 7 : (windowW == 350) ? windowW + 15 : windowW + 22;  
  162.         this.Height = (windowH == 300) ? windowH + 51 : (windowH == 350) ? windowH + 57 : windowH + 62;  
  163.         this.Width = panel1.Width + 118;  
  164.     }  
  165.   
  166.     //信息保存  
  167.     private void btnSave_Click(object sender, EventArgs e)  
  168.     {  
  169.         saveInfo = new SaveGameInfo();  
  170.         if (txtPlayer.Text == "")  
  171.             txtPlayer.Text = "隐姓埋名";  
  172.         char[] ch = (txtPlayer.Text).ToCharArray();  
  173.         string str="";  
  174.         //过滤'-'  
  175.         foreach (char c in ch)  
  176.         {  
  177.             if (c == '-')  
  178.                 continue;  
  179.             str += c;  
  180.         }  
  181.         if (saveInfo.SaveInfo(txtNewScore.Text, str))  
  182.             MessageBox.Show("保存成功!");  
  183.         btnSave.Enabled = false;  
  184.         LoadGameHeros();  
  185.     }  
  186.   
  187.     //关闭信息保存  
  188.     private void btnClose_Click(object sender, EventArgs e)  
  189.     {  
  190.         panGameInfo.Visible = false;  
  191.         lbGOver.Visible = true;  
  192.         LoadGameInfo();  
  193.         btnOK.Focus();  
  194.     }  
  195.   
  196.     //加载游戏信息   
  197.     public void LoadGameInfo()  
  198.     {  
  199.         scores = saveInfo.ReadInfo();  
  200.         if (scores != null)  
  201.         {  
  202.             if (scores.Length > 1)  
  203.             {  
  204.                 string[] scoreAndplayer = new string[] { };  
  205.                 foreach (string s in scores)  
  206.                 {  
  207.                     if (s == "")  
  208.                         continue;  
  209.                     scoreAndplayer = s.Split(';');  
  210.                 }  
  211.                 lbTScore.Text = scoreAndplayer[0];  
  212.                 lbPlayer.Text = scoreAndplayer[1];  
  213.                 largeScore = Convert.ToInt16(lbTScore.Text);  
  214.             }  
  215.             else  
  216.             {  
  217.                 lbTScore.Text = "加载中…";  
  218.                 lbPlayer.Text = "加载中…";  
  219.             }  
  220.         }  
  221.         else  
  222.         {  
  223.             lbTScore.Text = "加载中…";  
  224.             lbPlayer.Text = "加载中…";  
  225.         }  
  226.     }  
  227.   
  228.     //加载英雄榜  
  229.     public void LoadGameHeros()  
  230.     {  
  231.         scores = saveInfo.ReadInfo();  
  232.         if (scores != null)  
  233.         {  
  234.             if (scores.Length > 1)  
  235.             {  
  236.                 if (listHeros.Items.Count > 0)  
  237.                 {  
  238.                     listHeros.Items.Clear();  
  239.                 }  
  240.                 for (int i = scores.Length - 2; i >= 0; i--)  
  241.                 {  
  242.                     listHeros.Items.Add("No." + (scores.Length - i - 1) + "  " + scores[i]);  
  243.                 }  
  244.             }  
  245.             else  
  246.             {  
  247.                 listHeros.Items.Add("英雄,去创造奇迹吧!");  
  248.             }  
  249.         }  
  250.         else  
  251.         {  
  252.             listHeros.Items.Add("英雄,去创造奇迹吧!");  
  253.         }  
  254.     }  
  255.   
  256.     /// <summary>  
  257.     /// 按方向键时焦点会转移到Enabel=true的控件上,导致无法改变方向,  
  258.     /// 所以干脆用图片替换Button控件了,下面是图片按钮的一些样式.  
  259.     /// </summary>  
  260.     private void btnStart_Click(object sender, EventArgs e)  
  261.     {  
  262.         if (IsStop == true)  
  263.         {  
  264.             gameTimer.Enabled = true;  
  265.             btnStop.Enabled = true;  
  266.             btnStart.Enabled = false;  
  267.         }  
  268.         else  
  269.             GameStart();  
  270.         btnStart.Enabled = false;  
  271.         IsStop = false;  
  272.     }  
  273.   
  274.     private void btnStart_MouseMove(object sender, MouseEventArgs e)  
  275.     {  
  276.         Graphics g;  
  277.         g = btnStart.CreateGraphics();  
  278.         Pen p = new Pen(Color.Orange,3);  
  279.         g.DrawRectangle(p,0,0,btnStart.Width,btnStart.Height);  
  280.     }  
  281.   
  282.     private void btnStart_MouseLeave(object sender, EventArgs e)  
  283.     {  
  284.         btnStart.Invalidate();  
  285.     }  
  286.   
  287.     private void btnStop_Click(object sender, EventArgs e)  
  288.     {  
  289.         gameTimer.Enabled = false;  
  290.         IsStop =true;  
  291.         btnStop.Enabled = false;  
  292.         btnStart.Enabled = true;  
  293.     }  
  294.   
  295.     private void btnStop_MouseMove(object sender, MouseEventArgs e)  
  296.     {  
  297.         Graphics g;  
  298.         g = btnStop.CreateGraphics();  
  299.         Pen p = new Pen(Color.Orange, 2);  
  300.         g.DrawRectangle(p, 1, 1, btnStart.Width-2, btnStart.Height-1);  
  301.     }  
  302.   
  303.     private void btnStop_MouseLeave(object sender, EventArgs e)  
  304.     {  
  305.         btnStop.Invalidate();  
  306.     }  
  307. }  

    运行界面:

    

    至此,一款贪吃蛇小游戏就完成了,目前只有一个关卡,界面也较为普通,这些以后会改善之。

    编译环境:VS2008,c#

    Over! ^-^

    源代码下载:http://download.csdn.net/source/2815215

原创粉丝点击