实例介绍
【实例简介】使用C#做的俄罗斯方块小游戏
【实例截图】
【核心代码】
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 label4; private System.Windows.Forms.Label speedLabel; private System.Windows.Forms.Label scoreLabel; private GAME_STATUS gameStatus; private int speed; private int score; private System.Windows.Forms.Label linesLabel; private System.Windows.Forms.PictureBox pictureBox1; 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.label4 = new System.Windows.Forms.Label(); this.speedLabel = new System.Windows.Forms.Label(); this.linesLabel = new System.Windows.Forms.Label(); this.scoreLabel = new System.Windows.Forms.Label(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); 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.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 = "关于Tetris...(&A)"; // // 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 = "行 数:"; // // 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 = "速 度:"; // // speedLabel // this.speedLabel.Location = new System.Drawing.Point(288, 168); this.speedLabel.Name = "speedLabel"; this.speedLabel.Size = new System.Drawing.Size(56, 16); this.speedLabel.TabIndex = 8; this.speedLabel.Text = "0"; // // 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"; // // pictureBox1 // this.pictureBox1.Image = ((System.Drawing.Bitmap)(resources.GetObject("pictureBox1.Image"))); this.pictureBox1.Location = new System.Drawing.Point(276, 258); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(36, 28); this.pictureBox1.TabIndex = 9; this.pictureBox1.TabStop = false; // // MainForm // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(386, 305); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.pictureBox1, this.speedLabel, this.linesLabel, this.scoreLabel, this.label4, this.label3, this.label2, this.label1, this.nextPanel, 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 = "Tetris"; 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 = "TETRIS"; DrawText(logo, grMain, new Point(20, (int)(screenPanel.Height*0.28)), 30); } if (gameStatus == GAME_STATUS.GAME_OVER) { Graphics grMain = screenPanel.CreateGraphics(); string logo = "GAME OVER"; DrawText(logo, grMain, new Point(20, (int)(screenPanel.Height*0.42)), 21); } } 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() { 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; 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) / 30 > lines / 30) { 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); } } } }
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论