frmSnake.cs
上传用户:cljx11
上传日期:2022-08-09
资源大小:49k
文件大小:5k
源码类别:

其他智力游戏

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace Snake_v2
  10. {
  11.     public partial class frmSnake : Form
  12.     {
  13.         public frmSnake()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         private Block b = new Block();
  18.         private SnakeMove mov = new SnakeMove();
  19.         private int _event = 1;
  20.         private bool _door = true;
  21.         private bool _over = false;
  22.         private void GameReset()
  23.         {
  24.             Graphics gp = picBG.CreateGraphics();
  25.             SnakeBody.BodyClear();
  26.             SnakeMap.MapClear();
  27.             gp.Clear(Color.LightSteelBlue);
  28.             SnakeBody.NewSnake(3);
  29.             Apple.NewApple();
  30.             _event = 1;
  31.             GamePaint(gp);
  32.         }
  33.         private void GameOver(Graphics gp)
  34.         {
  35.             SnakeBody.BodyClear();
  36.             SnakeMap.MapClear();
  37.             gp.Clear(Color.LightSteelBlue);
  38.             gp.DrawString("Game Over~~!", new Font("Verdana", 20), new SolidBrush(Color.Orange), 70, 55);
  39.             timer.Enabled = false;
  40.             _over = true;
  41.             gp.Dispose();
  42.         }
  43.         private void GamePaint(Graphics gp)
  44.         {
  45.             gp.Clear(Color.LightSteelBlue);
  46.             for (int i = 0; i < SnakeBody.BodyX.Count; i++)
  47.                 b.SnakeBody((int)SnakeBody.BodyX[i], (int)SnakeBody.BodyY[i], gp);
  48.             b.Apple(Apple.GetAppleX(), Apple.GetAppleY(), gp);
  49.         }
  50.         private void picBG_Paint(object sender, PaintEventArgs e)
  51.         {
  52.             Graphics gp = e.Graphics;
  53.             gp.Clear(Color.LightSteelBlue);
  54.             if (_over == false) GamePaint(gp);
  55.             else
  56.             {
  57.                 gp.Clear(Color.LightSteelBlue);
  58.                 gp.DrawString("Game Over~~!", new Font("Verdana", 20), new SolidBrush(Color.Orange), 70, 55);
  59.             }
  60.         }
  61.         private void btnStar_Click(object sender, EventArgs e)
  62.         {
  63.             GameReset();
  64.             timer.Enabled = true;
  65.             _over = false;
  66.         }
  67.         private void btnClose_Click(object sender, EventArgs e)
  68.         {
  69.             this.Close();
  70.         }
  71.         #region TimerTick事件
  72.         private void timer_Tick(object sender, EventArgs e)
  73.         {
  74.             Graphics gp = picBG.CreateGraphics();
  75.             if (_event == 1)
  76.             {
  77.                 if (mov.Mov(1, 0,gp) == false)
  78.                 {
  79.                     GameOver(gp);
  80.                     return;
  81.                 }
  82.             }
  83.             else if (_event == -1)
  84.             {
  85.                 if (mov.Mov(-1, 0,gp) == false)
  86.                 {
  87.                     GameOver(gp);
  88.                     return;
  89.                 }
  90.             }
  91.             else if (_event == 2)
  92.             {
  93.                 if (mov.Mov(0, -1,gp) == false)
  94.                 {
  95.                     GameOver(gp);
  96.                     return;
  97.                 }
  98.             }
  99.             else if (_event == -2)
  100.             {
  101.                 if (mov.Mov(0, 1,gp) == false)
  102.                 {
  103.                     GameOver(gp);
  104.                     return;
  105.                 }
  106.             }
  107.             _door = true;
  108.         }
  109.         #endregion
  110.         #region 键盘响应
  111.         protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  112.         {
  113.             if (keyData == Keys.Right)
  114.             {
  115.                 if (_event != -1 && _door)
  116.                 {
  117.                     _event = 1;
  118.                     _door = false;
  119.                 }
  120.                 return true;
  121.             }
  122.             else if (keyData == Keys.Left)
  123.             {
  124.                 if (_event != 1 && _door)
  125.                 {
  126.                     _event = -1;
  127.                     _door = false;
  128.                 }
  129.                 return true;
  130.             }
  131.             else if (keyData == Keys.Up)
  132.             {
  133.                 if (_event != -2 && _door)
  134.                 {
  135.                     _event = 2;
  136.                     _door = false;
  137.                 }
  138.                 return true;
  139.             }
  140.             else if (keyData == Keys.Down)
  141.             {
  142.                 if (_event != 2 && _door)
  143.                 {
  144.                     _event = -2;
  145.                     _door = false;
  146.                 }
  147.                 return true;
  148.             }
  149.             return base.ProcessCmdKey(ref msg, keyData);
  150.         }
  151.         #endregion
  152.     }
  153. }