Form1.cs
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:6k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. namespace WindowsApplication9
  8. {
  9. /// <summary>
  10. /// Form1 的摘要说明。
  11. /// </summary>
  12. public class Form1 : System.Windows.Forms.Form
  13. {
  14. private System.Windows.Forms.Timer timer1;
  15. private System.Windows.Forms.Button button1;
  16. private System.Windows.Forms.Button button2;
  17. private System.ComponentModel.IContainer components;
  18. private System.Windows.Forms.TextBox textBox1;
  19. private System.Windows.Forms.Button button3;
  20. private System.Windows.Forms.Label label1;
  21. private System.Windows.Forms.Button button4;
  22. private System.Windows.Forms.Label label2;
  23. //声明场地类
  24. private Floor floor;
  25. public Form1()
  26. {
  27. //
  28. // Windows 窗体设计器支持所必需的
  29. //
  30. InitializeComponent();
  31. floor = new Floor(new Point(100, 100));
  32. //
  33. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  34. //
  35. }
  36. /// <summary>
  37. /// 清理所有正在使用的资源。
  38. /// </summary>
  39. protected override void Dispose( bool disposing )
  40. {
  41. if( disposing )
  42. {
  43. if (components != null) 
  44. {
  45. components.Dispose();
  46. }
  47. }
  48. base.Dispose( disposing );
  49. }
  50. #region Windows 窗体设计器生成的代码
  51. /// <summary>
  52. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  53. /// 此方法的内容。
  54. /// </summary>
  55. private void InitializeComponent()
  56. {
  57. this.components = new System.ComponentModel.Container();
  58. this.timer1 = new System.Windows.Forms.Timer(this.components);
  59. this.button1 = new System.Windows.Forms.Button();
  60. this.button2 = new System.Windows.Forms.Button();
  61. this.textBox1 = new System.Windows.Forms.TextBox();
  62. this.button3 = new System.Windows.Forms.Button();
  63. this.label1 = new System.Windows.Forms.Label();
  64. this.button4 = new System.Windows.Forms.Button();
  65. this.label2 = new System.Windows.Forms.Label();
  66. this.SuspendLayout();
  67. // 
  68. // timer1
  69. // 
  70. this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
  71. // 
  72. // button1
  73. // 
  74. this.button1.Location = new System.Drawing.Point(16, 8);
  75. this.button1.Name = "button1";
  76. this.button1.Size = new System.Drawing.Size(48, 23);
  77. this.button1.TabIndex = 0;
  78. this.button1.Text = "开始";
  79. this.button1.Click += new System.EventHandler(this.button1_Click);
  80. // 
  81. // button2
  82. // 
  83. this.button2.Location = new System.Drawing.Point(72, 8);
  84. this.button2.Name = "button2";
  85. this.button2.Size = new System.Drawing.Size(48, 23);
  86. this.button2.TabIndex = 1;
  87. this.button2.Text = "暂停";
  88. this.button2.Click += new System.EventHandler(this.button2_Click);
  89. // 
  90. // textBox1
  91. // 
  92. this.textBox1.Location = new System.Drawing.Point(240, 8);
  93. this.textBox1.Name = "textBox1";
  94. this.textBox1.Size = new System.Drawing.Size(64, 21);
  95. this.textBox1.TabIndex = 2;
  96. this.textBox1.Text = "100";
  97. // 
  98. // button3
  99. // 
  100. this.button3.Location = new System.Drawing.Point(312, 8);
  101. this.button3.Name = "button3";
  102. this.button3.Size = new System.Drawing.Size(64, 23);
  103. this.button3.TabIndex = 3;
  104. this.button3.Text = "改变速度";
  105. this.button3.Click += new System.EventHandler(this.button3_Click);
  106. // 
  107. // label1
  108. // 
  109. this.label1.Location = new System.Drawing.Point(128, 8);
  110. this.label1.Name = "label1";
  111. this.label1.Size = new System.Drawing.Size(100, 16);
  112. this.label1.TabIndex = 4;
  113. this.label1.Text = "输入大于1的整数";
  114. // 
  115. // button4
  116. // 
  117. this.button4.Location = new System.Drawing.Point(400, 8);
  118. this.button4.Name = "button4";
  119. this.button4.Size = new System.Drawing.Size(64, 23);
  120. this.button4.TabIndex = 5;
  121. this.button4.Text = "重新开始";
  122. this.button4.Click += new System.EventHandler(this.button4_Click);
  123. // 
  124. // label2
  125. // 
  126. this.label2.Location = new System.Drawing.Point(48, 272);
  127. this.label2.Name = "label2";
  128. this.label2.Size = new System.Drawing.Size(392, 16);
  129. this.label2.TabIndex = 6;
  130. this.label2.Text = "说明:鼠标控制蛇的方向,左击向右转,右击向左转。";
  131. // 
  132. // Form1
  133. // 
  134. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  135. this.BackColor = System.Drawing.SystemColors.Control;
  136. this.ClientSize = new System.Drawing.Size(488, 317);
  137. this.Controls.Add(this.label2);
  138. this.Controls.Add(this.button4);
  139. this.Controls.Add(this.label1);
  140. this.Controls.Add(this.button3);
  141. this.Controls.Add(this.textBox1);
  142. this.Controls.Add(this.button2);
  143. this.Controls.Add(this.button1);
  144. this.Name = "Form1";
  145. this.Text = "Form1";
  146. this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
  147. this.Load += new System.EventHandler(this.Form1_Load);
  148. this.ResumeLayout(false);
  149. }
  150. #endregion
  151. /// <summary>
  152. /// 应用程序的主入口点。
  153. /// </summary>
  154. [STAThread]
  155. static void Main() 
  156. {
  157. Application.Run(new Form1());
  158. }
  159. private void button1_Click(object sender, System.EventArgs e)
  160. {
  161. timer1.Enabled = true;
  162. }
  163. private void button2_Click(object sender, System.EventArgs e)
  164. {
  165. timer1.Enabled = false;
  166. }
  167. private void timer1_Tick(object sender, System.EventArgs e)
  168. {
  169. floor.Display(this.CreateGraphics());
  170. }
  171. private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  172. {
  173. if (e.Button == MouseButtons.Left)
  174. floor.S.TurnDirection(0);
  175. else if (e.Button == MouseButtons.Right)
  176. floor.S.TurnDirection(1);
  177. }
  178. private void button3_Click(object sender, System.EventArgs e)
  179. {
  180. timer1.Interval = Convert.ToInt32(textBox1.Text);
  181. }
  182. private void button4_Click(object sender, System.EventArgs e)
  183. {
  184. floor.ReSet(this.CreateGraphics());
  185. }
  186. private void Form1_Load(object sender, System.EventArgs e)
  187. {
  188. floor.initFloor(this.CreateGraphics());
  189. floor.Display(this.CreateGraphics());
  190. floor.S.snakeDie += new SnakeDele(S_snakeDie);
  191. }
  192. private void S_snakeDie()
  193. {
  194. timer1.Enabled = false;
  195. }
  196. }
  197. }