MainForm.cs
资源名称:Visual.rar [点击查看]
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:15k
源码类别:
C#编程
开发平台:
Others
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- namespace Tetris_CS
- {
- /// <summary>
- /// Form1 的摘要说明。
- /// </summary>
- public class MainForm : System.Windows.Forms.Form
- {
- private System.Windows.Forms.Panel screenPanel;
- private System.Windows.Forms.Panel nextPanel;
- private System.Windows.Forms.Timer timer;
- private System.Windows.Forms.MainMenu mainMenu;
- private System.Windows.Forms.MenuItem menuItem4;
- private System.Windows.Forms.MenuItem gameMenu;
- private System.Windows.Forms.MenuItem startMenu;
- private System.Windows.Forms.MenuItem stopMenu;
- private System.Windows.Forms.MenuItem exitMenu;
- private System.Windows.Forms.MenuItem helpMenu;
- private System.Windows.Forms.MenuItem aboutMenu;
- private System.ComponentModel.IContainer components;
- private Random rndShape = new Random();
- private Shape nextShape;
- private Body mainBody = new Body();
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.Label label2;
- private System.Windows.Forms.Label label3;
- private System.Windows.Forms.Label scoreLabel;
- private GAME_STATUS gameStatus;
- public int speed;
- public string jjjj;
- private int score;
- private System.Windows.Forms.Label linesLabel;
- private System.Windows.Forms.Label label4;
- private System.Windows.Forms.ComboBox comboBox1;
- private int lines;
- enum GAME_STATUS {GAME_STOP, GAME_RUN, GAME_OVER};
- public MainForm()
- {
- //
- // Windows 窗体设计器支持所必需的
- //
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
- //
- Shape.InitTetrisDefine();
- }
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows Form Designer generated code
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.components = new System.ComponentModel.Container();
- System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
- this.screenPanel = new System.Windows.Forms.Panel();
- this.nextPanel = new System.Windows.Forms.Panel();
- this.timer = new System.Windows.Forms.Timer(this.components);
- this.mainMenu = new System.Windows.Forms.MainMenu();
- this.gameMenu = new System.Windows.Forms.MenuItem();
- this.startMenu = new System.Windows.Forms.MenuItem();
- this.stopMenu = new System.Windows.Forms.MenuItem();
- this.menuItem4 = new System.Windows.Forms.MenuItem();
- this.exitMenu = new System.Windows.Forms.MenuItem();
- this.helpMenu = new System.Windows.Forms.MenuItem();
- this.aboutMenu = new System.Windows.Forms.MenuItem();
- this.label1 = new System.Windows.Forms.Label();
- this.label2 = new System.Windows.Forms.Label();
- this.label3 = new System.Windows.Forms.Label();
- this.linesLabel = new System.Windows.Forms.Label();
- this.scoreLabel = new System.Windows.Forms.Label();
- this.label4 = new System.Windows.Forms.Label();
- this.comboBox1 = new System.Windows.Forms.ComboBox();
- this.SuspendLayout();
- //
- // screenPanel
- //
- this.screenPanel.BackColor = System.Drawing.Color.White;
- this.screenPanel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
- this.screenPanel.Dock = System.Windows.Forms.DockStyle.Left;
- this.screenPanel.Location = new System.Drawing.Point(0, 0);
- this.screenPanel.Name = "screenPanel";
- this.screenPanel.Size = new System.Drawing.Size(206, 305);
- this.screenPanel.TabIndex = 0;
- //
- // nextPanel
- //
- this.nextPanel.BackColor = System.Drawing.Color.White;
- this.nextPanel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
- this.nextPanel.Location = new System.Drawing.Point(280, 0);
- this.nextPanel.Name = "nextPanel";
- this.nextPanel.Size = new System.Drawing.Size(104, 96);
- this.nextPanel.TabIndex = 1;
- //
- // timer
- //
- this.timer.Interval = 300;
- this.timer.Tick += new System.EventHandler(this.OnTimer);
- //
- // mainMenu
- //
- this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
- this.gameMenu,
- this.helpMenu});
- //
- // gameMenu
- //
- this.gameMenu.Index = 0;
- this.gameMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
- this.startMenu,
- this.stopMenu,
- this.menuItem4,
- this.exitMenu});
- this.gameMenu.Text = "文件(&F)";
- //
- // startMenu
- //
- this.startMenu.Index = 0;
- this.startMenu.Text = "开始(&S)";
- this.startMenu.Click += new System.EventHandler(this.startMenu_Click);
- //
- // stopMenu
- //
- this.stopMenu.Enabled = false;
- this.stopMenu.Index = 1;
- this.stopMenu.Text = "停止(&T)";
- this.stopMenu.Click += new System.EventHandler(this.stopMenu_Click);
- //
- // menuItem4
- //
- this.menuItem4.Index = 2;
- this.menuItem4.Text = "-";
- //
- // exitMenu
- //
- this.exitMenu.Index = 3;
- this.exitMenu.Text = "退出(&X)";
- this.exitMenu.Click += new System.EventHandler(this.exitMenu_Click);
- //
- // helpMenu
- //
- this.helpMenu.Index = 1;
- this.helpMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
- this.aboutMenu});
- this.helpMenu.Text = "帮助(&H)";
- //
- // aboutMenu
- //
- this.aboutMenu.Index = 0;
- this.aboutMenu.Text = "关于俄罗斯方块(&A)";
- this.aboutMenu.Click += new System.EventHandler(this.aboutMenu_Click);
- //
- // label1
- //
- this.label1.Location = new System.Drawing.Point(216, 8);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(56, 16);
- this.label1.TabIndex = 2;
- this.label1.Text = " ";
- //
- // label2
- //
- this.label2.Location = new System.Drawing.Point(216, 120);
- this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(56, 16);
- this.label2.TabIndex = 3;
- this.label2.Text = "分 数:";
- //
- // label3
- //
- this.label3.Location = new System.Drawing.Point(216, 144);
- this.label3.Name = "label3";
- this.label3.Size = new System.Drawing.Size(56, 16);
- this.label3.TabIndex = 4;
- this.label3.Text = "行 数:";
- //
- // linesLabel
- //
- this.linesLabel.Location = new System.Drawing.Point(288, 144);
- this.linesLabel.Name = "linesLabel";
- this.linesLabel.Size = new System.Drawing.Size(56, 16);
- this.linesLabel.TabIndex = 7;
- this.linesLabel.Text = "0";
- //
- // scoreLabel
- //
- this.scoreLabel.Location = new System.Drawing.Point(288, 120);
- this.scoreLabel.Name = "scoreLabel";
- this.scoreLabel.Size = new System.Drawing.Size(56, 16);
- this.scoreLabel.TabIndex = 6;
- this.scoreLabel.Text = "0";
- //
- // label4
- //
- this.label4.Location = new System.Drawing.Point(216, 168);
- this.label4.Name = "label4";
- this.label4.Size = new System.Drawing.Size(56, 16);
- this.label4.TabIndex = 5;
- this.label4.Text = "速 度:";
- //
- // comboBox1
- //
- this.comboBox1.Items.AddRange(new object[] {
- "0",
- "1",
- "2",
- "3",
- "4",
- "5",
- "6",
- "7",
- "8",
- "9"});
- this.comboBox1.Location = new System.Drawing.Point(286, 166);
- this.comboBox1.Name = "comboBox1";
- this.comboBox1.Size = new System.Drawing.Size(58, 20);
- this.comboBox1.TabIndex = 10;
- this.comboBox1.Text = "0";
- //
- // MainForm
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(386, 305);
- this.Controls.Add(this.comboBox1);
- this.Controls.Add(this.linesLabel);
- this.Controls.Add(this.scoreLabel);
- this.Controls.Add(this.label4);
- this.Controls.Add(this.label3);
- this.Controls.Add(this.label2);
- this.Controls.Add(this.label1);
- this.Controls.Add(this.nextPanel);
- this.Controls.Add(this.screenPanel);
- this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
- this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
- this.MaximizeBox = false;
- this.Menu = this.mainMenu;
- this.Name = "MainForm";
- this.Text = "俄罗斯方块";
- this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.MainForm_KeyDown);
- this.Load += new System.EventHandler(this.MainForm_Load);
- this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainFormPaint);
- this.ResumeLayout(false);
- }
- #endregion
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.Run(new MainForm());
- }
- public void DrawScreen()
- {
- if (gameStatus == GAME_STATUS.GAME_RUN || gameStatus == GAME_STATUS.GAME_OVER)
- {
- ReDrawNextShape();
- Graphics grMain = screenPanel.CreateGraphics();
- grMain.FillRectangle(new SolidBrush(Color.White), 0, 0, screenPanel.Width, screenPanel.Height);
- mainBody.Draw(grMain);
- }
- if (gameStatus == GAME_STATUS.GAME_STOP || gameStatus == GAME_STATUS.GAME_OVER)
- {
- Graphics grMain = screenPanel.CreateGraphics();
- string logo = "俄 罗 斯 方 块";
- DrawText(logo, grMain, new Point(10, (int)(screenPanel.Height*0.28)), 20);
- }
- if (gameStatus == GAME_STATUS.GAME_OVER)
- {
- Graphics grMain = screenPanel.CreateGraphics();
- string logo = "游 戏 结 束";
- DrawText(logo, grMain, new Point(20, (int)(screenPanel.Height*0.42)), 15);
- }
- }
- private void MainFormPaint(object sender, System.Windows.Forms.PaintEventArgs e)
- {
- DrawScreen();
- }
- private void OnTimer(object sender, System.EventArgs e)
- {
- if (gameStatus == GAME_STATUS.GAME_RUN)
- {
- Graphics grMain = screenPanel.CreateGraphics();
- if (mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_DOWN))
- {
- DisposeShapeDown();
- }
- }
- }
- private void startMenu_Click(object sender, System.EventArgs e)
- {
- StartGame();
- }
- private void stopMenu_Click(object sender, System.EventArgs e)
- {
- GameOver();
- }
- private void exitMenu_Click(object sender, System.EventArgs e)
- {
- this.Close();
- }
- public void StartGame()
- {
- comboBox1.Enabled = false;
- score = 0;
- speed = 0;
- lines = 0;
- ChangeLines(0);
- timer.Interval = SpeedToTime(speed);
- timer.Enabled = true;
- startMenu.Enabled = false;
- stopMenu.Enabled = true;
- gameStatus = GAME_STATUS.GAME_RUN;
- mainBody.Reset();
- GetNextShape(true);
- DrawScreen();
- }
- public bool GetNextShape()
- {
- return GetNextShape(false);
- }
- private void MainForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
- {
- int key = e.KeyValue;
- bool ret;
- Graphics grMain = screenPanel.CreateGraphics();
- if (gameStatus == GAME_STATUS.GAME_RUN)
- {
- switch (key)
- {
- case 38: // up
- ret = mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_ROATE);
- break;
- case 37: // left
- ret = mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_LEFT);
- break;
- case 39: // right
- ret = mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_RIGHT);
- break;
- case 40: // fall down
- ret = mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_FALL);
- break;
- default:
- ret = false;
- break;
- }
- if (ret && key == 40)
- {
- DisposeShapeDown();
- }
- }
- }
- private void MainForm_Load(object sender, System.EventArgs e)
- {
- }
- public bool GetNextShape(bool initGame)
- {
- int shapeCount = 7;
- if (initGame)
- {
- int indexShape = rndShape.Next(shapeCount);
- nextShape = new Shape(indexShape);
- }
- bool ret = mainBody.SetNextShape(nextShape);
- int indNextShape = rndShape.Next(shapeCount);
- nextShape = new Shape(indNextShape);
- return ret;
- }
- public void DisposeShapeDown()
- {
- int count = mainBody.ClearLines();
- if (GetNextShape())
- {
- GameOver();
- }
- if (count > 0)
- {
- ChangeLines(count);
- DrawScreen();
- }
- else
- {
- ReDrawNextShape();
- }
- }
- public void ReDrawNextShape()
- {
- Graphics grNext = nextPanel.CreateGraphics();
- grNext.FillRectangle(new SolidBrush(Color.White), 0, 0, nextPanel.Width, nextPanel.Height);
- nextShape.Draw(grNext, nextPanel.Size);
- Graphics grMain = screenPanel.CreateGraphics();
- mainBody.DrawNextShape(grMain);
- }
- public void GameOver()
- {
- gameStatus = GAME_STATUS.GAME_OVER;
- timer.Enabled = false;
- startMenu.Enabled = true;
- stopMenu.Enabled = false;
- comboBox1.Enabled = true;
- DrawScreen();
- }
- public void DrawText(string text, Graphics g, Point pt, int font)
- {
- Font drawFont = new Font("Courier new", font, FontStyle.Bold);
- for (int i=0; i<text.Length; i++)
- {
- int corIndex = i;
- if (i >= 7)
- corIndex = i % 7;
- SolidBrush drawBrush = new SolidBrush(Block.GetColor(corIndex));
- string drawText = new String(' ', i);
- drawText += text.Substring(i, 1);
- g.DrawString(drawText, drawFont, drawBrush, pt);
- }
- }
- public void ChangeLines(int count)
- {
- switch (count)
- {
- case 1:
- score += 100;
- break;
- case 2:
- score += 300;
- break;
- case 3:
- score += 500;
- break;
- case 4:
- score += 1000;
- break;
- default:
- break;
- }
- //if ((lines+count) / 3 > lines / 3)
- //{
- lines += count;
- jjjj =comboBox1.Text;
- speed = Convert.ToInt32(jjjj);
- //speed++;
- timer.Interval = SpeedToTime(speed);
- //}
- lines += count;
- scoreLabel.Text = score.ToString();
- //speedLabel.Text = speed.ToString();
- linesLabel.Text = lines.ToString();
- }
- public int SpeedToTime(int nSpeed)
- {
- switch (nSpeed)
- {
- case 0:
- return(1000);
- case 1:
- return(900);
- case 2:
- return(800);
- case 3:
- return(700);
- case 4:
- return(600);
- case 5:
- return(500);
- case 6:
- return(400);
- case 7:
- return(300);
- case 8:
- return(200);
- case 9:
- return(150);
- default:
- return(150);
- }
- }
- private void aboutMenu_Click(object sender, System.EventArgs e)
- {
- }
- }
- }