日期:2011-05-11  浏览次数:20447 次

下面是游戏的源代码。有两个文件:Main.cs 和 Snake.cs

//
// Main.cs Begin
//
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace GreedySnake
{
 ///
 /// formMain 的摘要说明。
 ///
 public class formMain : System.Windows.Forms.Form
 {
  ///
  /// 必需的设计器变量。
  ///
  private System.ComponentModel.Container components = null;
  private System.Windows.Forms.MenuItem menuGame;
  private System.Windows.Forms.MenuItem menuGameBegin;
  private System.Windows.Forms.MenuItem menuGameEnd;
  private System.Windows.Forms.MainMenu menuMain;
  private System.Windows.Forms.MenuItem menuGameRebegin;
  private System.Windows.Forms.MenuItem menuOption;
  private System.Windows.Forms.MenuItem menuOptionTopMost;

  private System.Windows.Forms.MenuItem menuOptionGameDifficulty;
  private System.Windows.Forms.MenuItem menuOptionSplit;
  private System.Windows.Forms.MenuItem menuOptionGameDifficultyEasy;
  private System.Windows.Forms.MenuItem menuOptionGameDifficultyCommon;
  private System.Windows.Forms.MenuItem menuOptionGameDifficultyDifficult;

  private System.Windows.Forms.Panel panelGameRegion;
  private System.Windows.Forms.Panel panel1;
  private System.Windows.Forms.Label labelGameDifficulty;
  private System.Windows.Forms.Label labelGameCurrentDifficulty;
  private System.Windows.Forms.Label labelGameIntroduce;
  private System.Windows.Forms.Label labelUp;
  private System.Windows.Forms.Label labelDown;
  private System.Windows.Forms.Label labelLeft;
  private System.Windows.Forms.Label labelRight;
  private System.Windows.Forms.Label labelStatistic;
  private System.Windows.Forms.Label labelInitCount;
  private System.Windows.Forms.Label labelCurrentCount;
  private System.Windows.Forms.Label labelScore;
 
  private System.Windows.Forms.Label labelEatGreenCount;

  private const int ciSpeed = 80; // 游戏速度(默认难度:一般)
  private int iSpeed = ciSpeed;  // 游戏速度

  private const int iCount = 18;  // 蛇长
  private const int iRadius = 5;  // 蛇骨接的半径
  private const int iStep = 2 * iRadius;  // 两个相邻的蛇骨中心相差的距离

  private const int iMaxCount = 5;  // 最多能剩下的食物总数

  private Snake snake;
  private SnakeFood snakeFood;

  public formMain()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  #region 清理正在使用的资源
  ///
  /// 清理所有正在使用的资源。
  ///
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }
  #endregion

  #region Windows 窗体设计器生成的代码
  ///
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  ///
  private void InitializeComponent()
  {
&nb