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

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 Tetris_CS
  8. {
  9. /// <summary>
  10. /// Form1 的摘要说明。
  11. /// </summary>
  12. public class MainForm : System.Windows.Forms.Form
  13. {
  14. private System.Windows.Forms.Panel screenPanel;
  15. private System.Windows.Forms.Panel nextPanel;
  16. private System.Windows.Forms.Timer timer;
  17. private System.Windows.Forms.MainMenu mainMenu;
  18. private System.Windows.Forms.MenuItem menuItem4;
  19. private System.Windows.Forms.MenuItem gameMenu;
  20. private System.Windows.Forms.MenuItem startMenu;
  21. private System.Windows.Forms.MenuItem stopMenu;
  22. private System.Windows.Forms.MenuItem exitMenu;
  23. private System.Windows.Forms.MenuItem helpMenu;
  24. private System.Windows.Forms.MenuItem aboutMenu;
  25. private System.ComponentModel.IContainer components;
  26. private Random rndShape = new Random();
  27. private Shape nextShape;
  28. private Body mainBody = new Body();
  29. private System.Windows.Forms.Label label1;
  30. private System.Windows.Forms.Label label2;
  31. private System.Windows.Forms.Label label3;
  32. private System.Windows.Forms.Label scoreLabel;
  33. private GAME_STATUS gameStatus;
  34. public int speed;
  35. public string jjjj;
  36. private int score;
  37. private System.Windows.Forms.Label linesLabel;
  38. private System.Windows.Forms.Label label4;
  39. private System.Windows.Forms.ComboBox comboBox1;
  40. private int lines;
  41. enum GAME_STATUS {GAME_STOP, GAME_RUN, GAME_OVER};
  42. public MainForm()
  43. {
  44. //
  45. // Windows 窗体设计器支持所必需的
  46. //
  47. InitializeComponent();
  48. //
  49. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  50. //
  51. Shape.InitTetrisDefine();
  52. }
  53. /// <summary>
  54. /// 清理所有正在使用的资源。
  55. /// </summary>
  56. protected override void Dispose( bool disposing )
  57. {
  58. if( disposing )
  59. {
  60. if (components != null) 
  61. {
  62. components.Dispose();
  63. }
  64. }
  65. base.Dispose( disposing );
  66. }
  67. #region Windows Form Designer generated code
  68. /// <summary>
  69. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  70. /// 此方法的内容。
  71. /// </summary>
  72. private void InitializeComponent()
  73. {
  74. this.components = new System.ComponentModel.Container();
  75. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
  76. this.screenPanel = new System.Windows.Forms.Panel();
  77. this.nextPanel = new System.Windows.Forms.Panel();
  78. this.timer = new System.Windows.Forms.Timer(this.components);
  79. this.mainMenu = new System.Windows.Forms.MainMenu();
  80. this.gameMenu = new System.Windows.Forms.MenuItem();
  81. this.startMenu = new System.Windows.Forms.MenuItem();
  82. this.stopMenu = new System.Windows.Forms.MenuItem();
  83. this.menuItem4 = new System.Windows.Forms.MenuItem();
  84. this.exitMenu = new System.Windows.Forms.MenuItem();
  85. this.helpMenu = new System.Windows.Forms.MenuItem();
  86. this.aboutMenu = new System.Windows.Forms.MenuItem();
  87. this.label1 = new System.Windows.Forms.Label();
  88. this.label2 = new System.Windows.Forms.Label();
  89. this.label3 = new System.Windows.Forms.Label();
  90. this.linesLabel = new System.Windows.Forms.Label();
  91. this.scoreLabel = new System.Windows.Forms.Label();
  92. this.label4 = new System.Windows.Forms.Label();
  93. this.comboBox1 = new System.Windows.Forms.ComboBox();
  94. this.SuspendLayout();
  95. // 
  96. // screenPanel
  97. // 
  98. this.screenPanel.BackColor = System.Drawing.Color.White;
  99. this.screenPanel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  100. this.screenPanel.Dock = System.Windows.Forms.DockStyle.Left;
  101. this.screenPanel.Location = new System.Drawing.Point(0, 0);
  102. this.screenPanel.Name = "screenPanel";
  103. this.screenPanel.Size = new System.Drawing.Size(206, 305);
  104. this.screenPanel.TabIndex = 0;
  105. // 
  106. // nextPanel
  107. // 
  108. this.nextPanel.BackColor = System.Drawing.Color.White;
  109. this.nextPanel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  110. this.nextPanel.Location = new System.Drawing.Point(280, 0);
  111. this.nextPanel.Name = "nextPanel";
  112. this.nextPanel.Size = new System.Drawing.Size(104, 96);
  113. this.nextPanel.TabIndex = 1;
  114. // 
  115. // timer
  116. // 
  117. this.timer.Interval = 300;
  118. this.timer.Tick += new System.EventHandler(this.OnTimer);
  119. // 
  120. // mainMenu
  121. // 
  122. this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  123.  this.gameMenu,
  124.  this.helpMenu});
  125. // 
  126. // gameMenu
  127. // 
  128. this.gameMenu.Index = 0;
  129. this.gameMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  130.  this.startMenu,
  131.  this.stopMenu,
  132.  this.menuItem4,
  133.  this.exitMenu});
  134. this.gameMenu.Text = "文件(&F)";
  135. // 
  136. // startMenu
  137. // 
  138. this.startMenu.Index = 0;
  139. this.startMenu.Text = "开始(&S)";
  140. this.startMenu.Click += new System.EventHandler(this.startMenu_Click);
  141. // 
  142. // stopMenu
  143. // 
  144. this.stopMenu.Enabled = false;
  145. this.stopMenu.Index = 1;
  146. this.stopMenu.Text = "停止(&T)";
  147. this.stopMenu.Click += new System.EventHandler(this.stopMenu_Click);
  148. // 
  149. // menuItem4
  150. // 
  151. this.menuItem4.Index = 2;
  152. this.menuItem4.Text = "-";
  153. // 
  154. // exitMenu
  155. // 
  156. this.exitMenu.Index = 3;
  157. this.exitMenu.Text = "退出(&X)";
  158. this.exitMenu.Click += new System.EventHandler(this.exitMenu_Click);
  159. // 
  160. // helpMenu
  161. // 
  162. this.helpMenu.Index = 1;
  163. this.helpMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  164.  this.aboutMenu});
  165. this.helpMenu.Text = "帮助(&H)";
  166. // 
  167. // aboutMenu
  168. // 
  169. this.aboutMenu.Index = 0;
  170. this.aboutMenu.Text = "关于俄罗斯方块(&A)";
  171. this.aboutMenu.Click += new System.EventHandler(this.aboutMenu_Click);
  172. // 
  173. // label1
  174. // 
  175. this.label1.Location = new System.Drawing.Point(216, 8);
  176. this.label1.Name = "label1";
  177. this.label1.Size = new System.Drawing.Size(56, 16);
  178. this.label1.TabIndex = 2;
  179. this.label1.Text = " ";
  180. // 
  181. // label2
  182. // 
  183. this.label2.Location = new System.Drawing.Point(216, 120);
  184. this.label2.Name = "label2";
  185. this.label2.Size = new System.Drawing.Size(56, 16);
  186. this.label2.TabIndex = 3;
  187. this.label2.Text = "分 数:";
  188. // 
  189. // label3
  190. // 
  191. this.label3.Location = new System.Drawing.Point(216, 144);
  192. this.label3.Name = "label3";
  193. this.label3.Size = new System.Drawing.Size(56, 16);
  194. this.label3.TabIndex = 4;
  195. this.label3.Text = "行 数:";
  196. // 
  197. // linesLabel
  198. // 
  199. this.linesLabel.Location = new System.Drawing.Point(288, 144);
  200. this.linesLabel.Name = "linesLabel";
  201. this.linesLabel.Size = new System.Drawing.Size(56, 16);
  202. this.linesLabel.TabIndex = 7;
  203. this.linesLabel.Text = "0";
  204. // 
  205. // scoreLabel
  206. // 
  207. this.scoreLabel.Location = new System.Drawing.Point(288, 120);
  208. this.scoreLabel.Name = "scoreLabel";
  209. this.scoreLabel.Size = new System.Drawing.Size(56, 16);
  210. this.scoreLabel.TabIndex = 6;
  211. this.scoreLabel.Text = "0";
  212. // 
  213. // label4
  214. // 
  215. this.label4.Location = new System.Drawing.Point(216, 168);
  216. this.label4.Name = "label4";
  217. this.label4.Size = new System.Drawing.Size(56, 16);
  218. this.label4.TabIndex = 5;
  219. this.label4.Text = "速 度:";
  220. // 
  221. // comboBox1
  222. // 
  223. this.comboBox1.Items.AddRange(new object[] {
  224.    "0",
  225.    "1",
  226.    "2",
  227.    "3",
  228.    "4",
  229.    "5",
  230.    "6",
  231.    "7",
  232.    "8",
  233.    "9"});
  234. this.comboBox1.Location = new System.Drawing.Point(286, 166);
  235. this.comboBox1.Name = "comboBox1";
  236. this.comboBox1.Size = new System.Drawing.Size(58, 20);
  237. this.comboBox1.TabIndex = 10;
  238. this.comboBox1.Text = "0";
  239. // 
  240. // MainForm
  241. // 
  242. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  243. this.ClientSize = new System.Drawing.Size(386, 305);
  244. this.Controls.Add(this.comboBox1);
  245. this.Controls.Add(this.linesLabel);
  246. this.Controls.Add(this.scoreLabel);
  247. this.Controls.Add(this.label4);
  248. this.Controls.Add(this.label3);
  249. this.Controls.Add(this.label2);
  250. this.Controls.Add(this.label1);
  251. this.Controls.Add(this.nextPanel);
  252. this.Controls.Add(this.screenPanel);
  253. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  254. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  255. this.MaximizeBox = false;
  256. this.Menu = this.mainMenu;
  257. this.Name = "MainForm";
  258. this.Text = "俄罗斯方块";
  259. this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.MainForm_KeyDown);
  260. this.Load += new System.EventHandler(this.MainForm_Load);
  261. this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainFormPaint);
  262. this.ResumeLayout(false);
  263. }
  264. #endregion
  265. /// <summary>
  266. /// 应用程序的主入口点。
  267. /// </summary>
  268. [STAThread]
  269. static void Main() 
  270. {
  271. Application.Run(new MainForm());
  272. }
  273. public void DrawScreen()
  274. {
  275. if (gameStatus == GAME_STATUS.GAME_RUN || gameStatus == GAME_STATUS.GAME_OVER)
  276. {
  277. ReDrawNextShape();
  278. Graphics grMain = screenPanel.CreateGraphics();
  279. grMain.FillRectangle(new SolidBrush(Color.White), 0, 0, screenPanel.Width, screenPanel.Height);
  280. mainBody.Draw(grMain);
  281. }
  282. if (gameStatus == GAME_STATUS.GAME_STOP || gameStatus == GAME_STATUS.GAME_OVER)
  283. {
  284. Graphics grMain = screenPanel.CreateGraphics();
  285. string logo = "俄 罗 斯 方 块";
  286. DrawText(logo, grMain, new Point(10, (int)(screenPanel.Height*0.28)), 20);
  287. }
  288. if (gameStatus == GAME_STATUS.GAME_OVER)
  289. {
  290. Graphics grMain = screenPanel.CreateGraphics();
  291. string logo = "游  戏  结  束";
  292. DrawText(logo, grMain, new Point(20, (int)(screenPanel.Height*0.42)), 15);
  293. }
  294. }
  295. private void MainFormPaint(object sender, System.Windows.Forms.PaintEventArgs e)
  296. {
  297. DrawScreen();
  298. }
  299. private void OnTimer(object sender, System.EventArgs e)
  300. {
  301. if (gameStatus == GAME_STATUS.GAME_RUN)
  302. {
  303. Graphics grMain = screenPanel.CreateGraphics();
  304. if (mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_DOWN))
  305. {
  306. DisposeShapeDown();
  307. }
  308. }
  309. }
  310. private void startMenu_Click(object sender, System.EventArgs e)
  311. {
  312. StartGame();
  313. }
  314. private void stopMenu_Click(object sender, System.EventArgs e)
  315. {
  316. GameOver();
  317. }
  318. private void exitMenu_Click(object sender, System.EventArgs e)
  319. {
  320. this.Close();
  321. }
  322. public void StartGame()
  323. {
  324. comboBox1.Enabled = false;
  325. score = 0;
  326. speed = 0;
  327. lines = 0;
  328. ChangeLines(0);
  329. timer.Interval = SpeedToTime(speed);
  330. timer.Enabled = true;
  331. startMenu.Enabled = false;
  332. stopMenu.Enabled = true;
  333. gameStatus = GAME_STATUS.GAME_RUN;
  334. mainBody.Reset();
  335. GetNextShape(true);
  336. DrawScreen();
  337. }
  338. public bool GetNextShape()
  339. {
  340. return GetNextShape(false);
  341. }
  342. private void MainForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
  343. {
  344. int key = e.KeyValue;
  345. bool ret;
  346. Graphics grMain = screenPanel.CreateGraphics();
  347. if (gameStatus == GAME_STATUS.GAME_RUN)
  348. {
  349. switch (key)
  350. {
  351. case 38: // up
  352. ret = mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_ROATE);
  353. break;
  354. case 37: // left
  355. ret = mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_LEFT);
  356. break;
  357. case 39: // right
  358. ret = mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_RIGHT);
  359. break;
  360. case 40: // fall down
  361. ret = mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_FALL);
  362. break;
  363. default:
  364. ret = false;
  365. break;
  366. }
  367. if (ret && key == 40)
  368. {
  369. DisposeShapeDown();
  370. }
  371. }
  372. }
  373. private void MainForm_Load(object sender, System.EventArgs e)
  374. {
  375. }
  376. public bool GetNextShape(bool initGame)
  377. {
  378. int shapeCount = 7;
  379. if (initGame)
  380. {
  381. int indexShape = rndShape.Next(shapeCount);
  382. nextShape = new Shape(indexShape);
  383. }
  384. bool ret = mainBody.SetNextShape(nextShape);
  385. int indNextShape = rndShape.Next(shapeCount);
  386. nextShape = new Shape(indNextShape);
  387. return ret;
  388. }
  389. public void DisposeShapeDown()
  390. {
  391. int count = mainBody.ClearLines();
  392. if (GetNextShape())
  393. {
  394. GameOver();
  395. }
  396. if (count > 0)
  397. {
  398. ChangeLines(count);
  399. DrawScreen();
  400. }
  401. else
  402. {
  403. ReDrawNextShape();
  404. }
  405. }
  406. public void ReDrawNextShape()
  407. {
  408. Graphics grNext = nextPanel.CreateGraphics();
  409. grNext.FillRectangle(new SolidBrush(Color.White), 0, 0, nextPanel.Width, nextPanel.Height);
  410. nextShape.Draw(grNext, nextPanel.Size);
  411. Graphics grMain = screenPanel.CreateGraphics();
  412. mainBody.DrawNextShape(grMain);
  413. }
  414. public void GameOver()
  415. {
  416. gameStatus = GAME_STATUS.GAME_OVER;
  417. timer.Enabled = false;
  418. startMenu.Enabled = true;
  419. stopMenu.Enabled = false;
  420.             comboBox1.Enabled = true;
  421. DrawScreen();
  422. }
  423. public void DrawText(string text, Graphics g, Point pt, int font)
  424. {
  425. Font drawFont = new Font("Courier new", font, FontStyle.Bold);
  426. for (int i=0; i<text.Length; i++)
  427. {
  428. int corIndex = i;
  429. if (i >= 7) 
  430. corIndex = i % 7;
  431. SolidBrush drawBrush = new SolidBrush(Block.GetColor(corIndex));
  432. string drawText = new String(' ', i);
  433. drawText += text.Substring(i, 1);
  434. g.DrawString(drawText, drawFont, drawBrush, pt);
  435. }
  436. }
  437. public void ChangeLines(int count)
  438. {
  439. switch (count)
  440. {
  441. case 1:
  442. score += 100;
  443. break;
  444. case 2:
  445. score += 300;
  446. break;
  447. case 3:
  448. score += 500;
  449. break;
  450. case 4:
  451. score += 1000;
  452. break;
  453. default:
  454. break;
  455. }
  456. //if ((lines+count) / 3 > lines / 3)
  457. //{
  458. lines += count;
  459. jjjj =comboBox1.Text;
  460.                 speed = Convert.ToInt32(jjjj);
  461. //speed++;
  462. timer.Interval = SpeedToTime(speed);
  463. //}
  464. lines += count;
  465.             
  466. scoreLabel.Text = score.ToString();
  467. //speedLabel.Text = speed.ToString();
  468. linesLabel.Text = lines.ToString();
  469. }
  470. public int SpeedToTime(int nSpeed)
  471. {
  472. switch (nSpeed)
  473. {
  474. case 0:
  475. return(1000);
  476. case 1:
  477. return(900);
  478. case 2:
  479. return(800);
  480. case 3:
  481. return(700);
  482. case 4:
  483. return(600);
  484. case 5:
  485. return(500);
  486. case 6:
  487. return(400);
  488. case 7:
  489. return(300);
  490. case 8:
  491. return(200);
  492. case 9:
  493. return(150);
  494. default:
  495. return(150);
  496. }
  497. }
  498. private void aboutMenu_Click(object sender, System.EventArgs e)
  499. {
  500. }
  501. }
  502. }