实例介绍
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.IO; using System.Runtime.InteropServices; namespace Games { public partial class Main : MyForm { private GameData data = GameData.Instance; private Point down_location; private bool isCanOperate; public Main() { InitializeComponent(); data.OnReGood = data_ReGood; data.OnReGrade = data_ReGrade; data.OnChange = data_OnChange; data.OnGameOver = data_OnGameOver; } protected override void OnClosed(EventArgs e) { data.Save(); base.OnClosed(e); } void data_OnGameOver() { new Game_Over(data.Grade, data.Good, () => { }, Shoot, this.Close).ShowDialog(this); } void data_OnChange() { using (Graphics g = this.CreateGraphics()) { DrawBlock(g); } } void data_ReGrade() { using (Graphics g = this.CreateGraphics()) { DrawGrade(g); } } void data_ReGood() { using (Graphics g = this.CreateGraphics()) { DrawBest(g); } } private void Shoot() { using (Bitmap image = new Bitmap(this.Width, this.Height)) { this.DrawToBitmap(image, new Rectangle(0, 0, this.Width, this.Height)); String savePath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) "/2048截图" DateTime.Now.Ticks ".png"; image.Save(savePath); new _2048messageBox("保存成功", "保存在" savePath).ShowDialog(this); } } /// <summary> /// 颜色生成使用原来楼主的颜色。 /// </summary> /// <param name="i">需要获得颜色的方块中的数字</param> /// <returns>返回对应的颜色</returns> private Color GetBlockColor(int i) { switch (i) { case 0: return Color.BurlyWood; case 2: return Color.LightSalmon; case 4: return Color.Peru; case 8: return Color.Chocolate; case 16: return Color.Gray; case 32: return Color.DarkSeaGreen; case 64: return Color.Gold; case 128: return Color.HotPink; case 256: return Color.DarkOrange; case 512: return Color.LightPink; case 1024: return Color.DarkRed; case 2048: return Color.Red; default: return Color.FromArgb(255, 20, 20, 20); } } /// <summary> /// 字体也是使用原来楼主的字体。 /// </summary> /// <param name="i"></param> /// <returns></returns> private Font GetWordFont(int i) { if (i < 100) { if (i < 10) { return new Font("黑体", 40.5f, FontStyle.Bold); } return new Font("黑体", 35.5f, FontStyle.Bold); } if (i < 1000) { return new Font("黑体", 35.5f, FontStyle.Bold); } return new Font("黑体", 30.5f, FontStyle.Bold); } /// <summary> /// 画最高分。 /// </summary> /// <param name="g"></param> private void DrawBest(Graphics g) { g.FillRectangle(Brushes.DarkGray, 316, 37, 100, 64); g.DrawString("BEST", new Font("微软雅黑", 9F, FontStyle.Bold, GraphicsUnit.Point), Brushes.White, 348, 42); String good = data.Good.ToString(); Font drawFont = new Font("微软雅黑", 18F, FontStyle.Bold, GraphicsUnit.Point); SizeF drawSize = g.MeasureString(good, drawFont); g.DrawString(good, drawFont, Brushes.White, ((100 - (int)drawSize.Width) >> 1) 316, 60); } /// <summary> /// 画当前成绩。 /// </summary> /// <param name="g"></param> private void DrawGrade(Graphics g) { g.FillRectangle(Brushes.DarkGray, 210, 37, 100, 64); g.DrawString("SCORE", new Font("微软雅黑", 9F, FontStyle.Bold, GraphicsUnit.Point), Brushes.White, 238, 42); String grade = data.Grade.ToString(); Font drawFont = new Font("微软雅黑", 18F, FontStyle.Bold, GraphicsUnit.Point); SizeF drawSize = g.MeasureString(grade, drawFont); g.DrawString(grade, drawFont, Brushes.White, ((100 - (int)drawSize.Width) >> 1) 210, 60); } /// <summary> /// 画方块和数字。 /// </summary> /// <param name="g"></param> private void DrawBlock(Graphics g) { for (int i = 0; i < data.Length; i ) { int x = (i - ((i >> 2) << 2)) * 100 26; int y = (i >> 2) * 100 153; g.FillRectangle(new SolidBrush(GetBlockColor(data[i])), x, y, 90, 90); if (data[i] != 0) { Font drawFont = GetWordFont(data[i]); string drawString = data[i].ToString(); SizeF drawSize = g.MeasureString(drawString, drawFont); g.DrawString(drawString, drawFont, Brushes.White, ((90 - (int)drawSize.Width) / 2) x, ((90 - (int)drawSize.Height) / 2) y); } } } protected override void OnPaint(PaintEventArgs e) { e.Graphics.DrawString("2048", new System.Drawing.Font("微软雅黑", 48F, FontStyle.Bold, GraphicsUnit.Point), Brushes.Gray, 15, 25); e.Graphics.DrawString("按F1打开帮助", new Font("微软雅黑", 12F, FontStyle.Regular, GraphicsUnit.Point), Brushes.Black, 315, 115); DrawGrade(e.Graphics); DrawBest(e.Graphics); DrawBlock(e.Graphics); base.OnPaint(e); } protected override void OnKeyDown(KeyEventArgs e) { switch (e.KeyCode) { case Keys.Up: if (e.Shift) { this.Opacity = 0.1; } else { data.Up(); } break; case Keys.Down: if (e.Shift) { if (this.Opacity > 0.2) { this.Opacity -= 0.1; } } else { data.Down(); } break; case Keys.Left: data.Left(); break; case Keys.Right: data.Right(); break; case Keys.F4: this.TopMost = !this.TopMost; break; case Keys.F5: data.Reset(); break; case Keys.F6: this.Shoot(); break; case Keys.F1: new _2048messageBox("提示", "F4:窗口总在最前\r\nF5:重新开始\r\nF6:截图并保存\r\nShife ↑↓:调整透明度\r\n↑↓←→:控制方块移动").ShowDialog(this); break; } base.OnKeyDown(e); } /// <summary> /// 重写鼠标点击事件的触发方法,相当于鼠标点击事件。 /// </summary> /// <param name="e"></param> protected override void OnMouseDown(MouseEventArgs e) { // 判断点击的位置是不是在允许手势的位置。 if (e.Y > 150) { isCanOperate = true; down_location = e.Location; } base.OnMouseDown(e); } protected override void OnMouseUp(MouseEventArgs e) { // 如果能操作手势。 if (isCanOperate) { isCanOperate = false; int x = down_location.X - e.X; int y = down_location.Y - e.Y; // 获得点击到释放的最大位置动向如果动向大于50那么相应手势。 if (x < -50 && x < (y < 0 ? y : -y)) { data.Right(); } else if (x > 50 && x > (y > 0 ? y : -y)) { data.Left(); } else if (y < -50 && y < (x < 0 ? x : -x)) { data.Down(); } else if (y > 50 && y > (x > 0 ? x : -x)) { data.Up(); } } base.OnMouseUp(e); } protected override void OnClosing(CancelEventArgs e) { DialogResult res = MessageBox.Show("离开游戏?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (res != DialogResult.OK) { e.Cancel = true; } base.OnClosing(e); } } }
标签: 2048
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论