winphone平台下的DubbleBreaker游戏代码——代码分享一
来源:互联网 发布:制作婚纱照的软件 编辑:程序博客网 时间:2024/05/01 22:56
整理文件夹的时候,无意当中找到了自己以前写的不少程序代码,感觉还凑合,希望能对一些人有帮助。
记得第一次购买wince平台的三星手机i920,里面会自带一个游戏,叫DubbleBreaker。这个游戏的玩法是,最开始系统会随机初始化5种颜色的球,只有相邻的同种颜色小球能被双击消除掉,消除以后,左边的小球和上面的小球会自动下移下来填充。每次消除小球会有得分,得分等于你消除的(小球数量)X(小球数量-1),为了得分最高,尽量消球的时候把同种颜色全部小球放到最后消除,才能够得到高分。直到最后不能再消除为结束。
界面:
以前无聊的时候经常在手机上玩,屏幕太小,看时间长了眼睛受不了,决定把它搬到电脑上。随便给他加一些功能,比如无限晦气等,这样玩的比较刺激。不说了,赶紧贴代码吧。
核心代码:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Data;using System.Text;using System.Windows.Forms;using System.Collections;using System.Drawing.Drawing2D;namespace DubbleBreaker_GAME{ public partial class DubbleBreaker : UserControl { public DubbleBreaker() { InitializeComponent(); //双缓冲 this.DoubleBuffered = true; YUANWIDTH = WIDTH - 2; //默认开始 Start(); //this.MouseClick += new MouseEventHandler(UserControl1_MouseClick); this.MouseDown += new MouseEventHandler(UserControl1_MouseDown); this.Cursor = Cursors.Hand; ImageObject[0] = global::DubbleBreaker.Properties.Resources.img3a0; ImageObject[1] = global::DubbleBreaker.Properties.Resources.img3a1; ImageObject[2] = global::DubbleBreaker.Properties.Resources.img3a2; ImageObject[3] = global::DubbleBreaker.Properties.Resources.img3a3; } //ThreadPool.QueueUserWorkItem(new WaitCallback(delegate // { // for (int i = 0; i < 4; i++) // { // foreach (XiaoQiu xq in XIAOQIUS) // { // g.DrawImage(ImageObject[i], new Point(xq.X, xq.Y)); // } // Thread.Sleep(100); // } // })); /// <summary> /// 爆炸效果 /// </summary> Image[] ImageObject = new Image[4]; void UserControl1_MouseDown(object sender, MouseEventArgs e) { UserControl1_MouseClick(sender, e); } /// <summary> /// 撤销 /// </summary> public void Back() { if (MAX > 0)//允许撤销 { SELECTXQ.Clear(); //为了前进 hasLS_GO[MAX] = XIAOQIUS; XIAOQIUS = Copy((XiaoQiu[,])hasLS[MAX]); int f = (int)hasLS_F[MAX]; iZongFen -= f;//总分去掉 bShowTiShi = false; MAX--; } this.Refresh(); } /// <summary> /// 撤销 /// </summary> internal void Go() { if (hasLS_GO.Contains(MAX + 1)) { MAX++; SELECTXQ.Clear(); XIAOQIUS = Copy((XiaoQiu[,])hasLS_GO[MAX]); int f = (int)hasLS_F[MAX]; iZongFen += f;//总分去掉 bShowTiShi = false; } this.Refresh(); } /// <summary> /// 对象拷贝 /// </summary> /// <param name="source"></param> /// <returns></returns> XiaoQiu[,] Copy(XiaoQiu[,] source) { XiaoQiu[,] n = new XiaoQiu[WSIZE, HSIZE]; for (int i = 0; i < source.GetLongLength(0); i++) { for (int j = 0; j < source.GetLongLength(1); j++) { n[i, j] = (XiaoQiu)source[i, j].Clone(); } } return n; } //存储历史记录 int MAX = 0; Hashtable hasLS = new Hashtable(); Hashtable hasLS_GO = new Hashtable(); Hashtable hasLS_F = new Hashtable(); void UserControl1_MouseClick(object sender, MouseEventArgs e) { //判断是否还能再消除? bool isEnd = CheckEND(); if (!isEnd) { Sound.PlaySound("CanNotDo"); return; } foreach (XiaoQiu o in XIAOQIUS) { if ((e.X >= o.X && e.X < o.X + o.W) && (e.Y >= o.Y && e.Y < o.Y + o.H)) { bShowTiShi = false; if (o.Color == Color.White) { SELECTXQ.Clear(); break; } if (SELECTXQ.Count >= 2 && SELECTXQ.Contains(o)) { //计算积分 int count = SELECTXQ.Count; MAX++;//次数 //记录当前的东东 hasLS[MAX] = Copy(XIAOQIUS); hasLS_F[MAX] = count * (count - 1); XiaoQiuAction(); onXiaoChuClickDown(new DanJiClickDownEventArgs(count * (count - 1))); SELECTXQ.Clear(); Sound.PlaySound("FinishOneLine"); } else { //计算多少分 //把连接起来的东东都框起来 SELECTXQ.Clear(); List<XiaoQiu> tmp = new List<XiaoQiu>(); FindXiaoQiao(o.Color, o, SELECTXQ, tmp); tmp.Clear(); if (SELECTXQ.Count >= 2)//两个才算可以消 { CURRENT = o; int count = SELECTXQ.Count; int ifen = count * (count - 1); onDanJiClickDown(new DanJiClickDownEventArgs(ifen)); bShowTiShi = true; //显示分数圆圈 ShowFenShu(ifen); } else { onDanJiClickDown(new DanJiClickDownEventArgs(0)); SELECTXQ.Clear(); } } break; } } this.Refresh(); } /// <summary> /// 显示分数的圆圈 /// </summary> /// <param name="ifen"></param> private void ShowFenShu(int ifen) { for (int i = 0; i < XIAOQIUS.GetLongLength(1); i++) { for (int j = 0; j < XIAOQIUS.GetLongLength(0); j++) { if (SELECTXQ.Contains(XIAOQIUS[j, i])) { PTiShi.X = XIAOQIUS[j, i].X; PTiShi.Y = XIAOQIUS[j, i].Y; return; } } } } Point PTiShi = new Point(0, 0); bool bShowTiShi = false; /// <summary> /// 返回 /// </summary> /// <returns></returns> private bool CheckEND() { foreach (XiaoQiu o in XIAOQIUS) { if (o.Color == Color.White) continue; List<XiaoQiu> tmp = new List<XiaoQiu>(); List<XiaoQiu> tmp1 = new List<XiaoQiu>(); FindXiaoQiao(o.Color, o, tmp1, tmp); tmp.Clear(); if (tmp1.Count >= 2) { return true; } } return false; } /// <summary> /// 消球并积分 /// </summary> private void XiaoQiuAction() { //消球 //先把找到球设置为false,然后再进行调整 foreach (XiaoQiu xq in SELECTXQ) { xq.N = true; xq.Color = Color.White; } //再调整行列 //先调整列 从上到下 从右到左 for (int i = 0; i < XIAOQIUS.GetLongLength(0); i++) { for (int j = 0; j < XIAOQIUS.GetLongLength(1); j++) { XiaoQiu o = XIAOQIUS[i, j]; if (o.N)//如果不存在,把上面的小球下移 { int m = o.J - 1; while (m >= 0) { //交换 Color c = XIAOQIUS[i, m].Color; bool n = XIAOQIUS[i, m].N; XIAOQIUS[i, m].Color = XIAOQIUS[i, m + 1].Color; XIAOQIUS[i, m].N = XIAOQIUS[i, m + 1].N; XIAOQIUS[i, m + 1].Color = c; XIAOQIUS[i, m + 1].N = n; m--; } } } } //如果发现有些列没有数据了,应该把左边的列再移过来 for (int i = 0; i < XIAOQIUS.GetLongLength(0); i++) { bool b = false; for (int j = 0; j < XIAOQIUS.GetLongLength(1); j++) { XiaoQiu o = XIAOQIUS[i, j]; if (!o.N) { b = true; break; } } if (!b)//该列不存在 左边的列再移过来 { //循环多少行 for (int j = 0; j < XIAOQIUS.GetLongLength(1); j++) { //第i列不存在 int m = i - 1; while (m >= 0) { //交换 Color c = XIAOQIUS[m, j].Color; bool n = XIAOQIUS[m, j].N; XIAOQIUS[m, j].Color = XIAOQIUS[m + 1, j].Color; XIAOQIUS[m, j].N = XIAOQIUS[m + 1, j].N; XIAOQIUS[m + 1, j].Color = c; XIAOQIUS[m + 1, j].N = n; m--; } } } } } /// <summary> /// 从小球的周围选中相同颜色的。 /// </summary> private void FindXiaoQiao(Color c, XiaoQiu o, List<XiaoQiu> SELECTXQ, List<XiaoQiu> tmp) { //已经寻找过 if (tmp.Contains(o)) return; //判断 if (o.Color == c) { tmp.Add(o); SELECTXQ.Add(o); } else { tmp.Add(o); return; } //上 if (o.J != 0) FindXiaoQiao(c, XIAOQIUS[o.I - 0, o.J - 1], SELECTXQ, tmp); if (o.I != 0) FindXiaoQiao(c, XIAOQIUS[o.I - 1, o.J - 0], SELECTXQ, tmp); if (o.J != HSIZE - 1) FindXiaoQiao(c, XIAOQIUS[o.I - 0, o.J + 1], SELECTXQ, tmp); if (o.I != WSIZE - 1) FindXiaoQiao(c, XIAOQIUS[o.I + 1, o.J - 0], SELECTXQ, tmp); } //四种颜色 readonly Color[] COLORS = { Color.Red, Color.Gold, Color.Blue, Color.Green, Color.Fuchsia }; XiaoQiu[,] XIAOQIUS = null; readonly int WSIZE = 11; readonly int HSIZE = 12; readonly int WIDTH = 40; readonly int YUANWIDTH = 0; int iZongFen = 0; XiaoQiu CURRENT = null; List<XiaoQiu> SELECTXQ = new List<XiaoQiu>(); /// <summary> /// 返回总分 /// </summary> /// <returns></returns> public int GetZongFen() { return iZongFen; } /// <summary> /// 重绘背景 /// </summary> /// <param name="e"></param> protected override void OnPaint(PaintEventArgs e) { //开始绘制 Graphics g = e.Graphics; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = CompositingQuality.GammaCorrected; for (int i = 0; i < XIAOQIUS.GetLongLength(0); i++) { for (int j = 0; j < XIAOQIUS.GetLongLength(1); j++) { XiaoQiu xq = XIAOQIUS[i, j]; if (!xq.N) { GraphicsPath gp = new GraphicsPath(); Rectangle rec = new Rectangle(xq.X, xq.Y, YUANWIDTH, YUANWIDTH); gp.AddRectangle(rec); PathGradientBrush pb = new PathGradientBrush(gp); pb.CenterColor = Color.White; pb.CenterPoint = new PointF(xq.X + YUANWIDTH / 4f, xq.Y + YUANWIDTH / 4f); pb.SurroundColors = new Color[] { xq.Color }; g.FillEllipse(pb, rec); g.DrawEllipse(new Pen(Color.LightGray, 1), xq.X, xq.Y, YUANWIDTH, YUANWIDTH); //给选中的小球四周画上边框 if (SELECTXQ.Count > 0) { XiaoQiu o = xq; if (SELECTXQ.Contains(o)) { int x = o.X; int y = o.Y; int w = o.W - 1; int h = o.H - 1; Pen p = new Pen(Color.Fuchsia, 1); if ((o.J != 0 && !SELECTXQ.Contains(XIAOQIUS[o.I - 0, o.J - 1])) || (o.J == 0 && SELECTXQ.Contains(o))) g.DrawLine(p, x, y, x + w, y); if ((o.I != 0 && !SELECTXQ.Contains(XIAOQIUS[o.I - 1, o.J - 0])) || (o.I == 0 && SELECTXQ.Contains(o))) g.DrawLine(p, x, y, x, y + o.H); if ((o.J != (HSIZE - 1) && !SELECTXQ.Contains(XIAOQIUS[o.I - 0, o.J + 1])) || (o.J == (HSIZE - 1) && SELECTXQ.Contains(o))) g.DrawLine(p, x, y + h, x + w, y + h); if ((o.I != (WSIZE - 1) && !SELECTXQ.Contains(XIAOQIUS[o.I + 1, o.J - 0])) || (o.I == (WSIZE - 1) && SELECTXQ.Contains(o))) g.DrawLine(p, x + w, y, x + w, y + h); } } //提示信息 if (bShowTiShi) { Font font = new System.Drawing.Font("SimSun", 20, GraphicsUnit.Pixel); int f = SELECTXQ.Count * (SELECTXQ.Count - 1); SizeF sfa = g.MeasureString(f.ToString(), font); float w = Math.Max(sfa.Width, sfa.Height); float h = w; float off = 2f; RectangleF rectf = new RectangleF(PTiShi.X - w*2/3 - off, PTiShi.Y - h - off, w, h); g.FillEllipse(new SolidBrush(GetCenterColor(CURRENT.Color,Color.White)), rectf); g.DrawEllipse(new Pen(Color.LightGray, 1), rectf); g.DrawString(f.ToString(), font, new SolidBrush(Color.Black), rectf.X + (rectf.Width - sfa.Width) / 2f, rectf.Y + (rectf.Height - sfa.Height) / 2f); } } } } } /// <summary> /// 透明色 /// </summary> /// <param name="c1"></param> /// <returns></returns> Color GetCenterColor(Color c1) { return Color.FromArgb(6, c1.R, c1.G, c1.B); } /// <summary> /// 混合色 /// </summary> /// <param name="c1"></param> /// <param name="c2"></param> /// <returns></returns> Color GetCenterColor(Color c1, Color c2) { // 计算c1和c2(的)红、蓝、绿值(的)中间值 return Color.FromArgb(255, (c1.R + c2.R) / 2, (c1.G + c2.G) / 2, (c1.B + c2.B) / 2 ); } /// <summary> /// 偏移量 /// </summary> readonly int XOFFSET = 30; /// <summary> /// 偏移量 /// </summary> readonly int YOFFSET = 30; /// <summary> /// 开局 /// </summary> /// <returns></returns> public void Start() { XIAOQIUS = new XiaoQiu[WSIZE, HSIZE]; Random ro = new Random(10); long tick = DateTime.Now.Ticks; Random ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32)); for (int i = 0; i < XIAOQIUS.GetLongLength(0); i++) { for (int j = 0; j < XIAOQIUS.GetLongLength(1); j++) { int k = ran.Next(100) % COLORS.Length; //初始化小球 XIAOQIUS[i, j] = new XiaoQiu() { N = false, I = i, J = j, X = i * WIDTH + XOFFSET, Y = j * WIDTH + YOFFSET, W = WIDTH, H = WIDTH, Color = COLORS[k] }; } } this.Width = WIDTH * WSIZE + XOFFSET * 2; this.Height = WIDTH * HSIZE + YOFFSET * 2; iZongFen = 0; bShowTiShi = false; hasLS.Clear(); hasLS_F.Clear(); MAX = 0; //界面绘制 this.Refresh(); } public delegate void DanJiClickDownEventHandler(object sender, DanJiClickDownEventArgs e);//事件所需的委托 public delegate void XiaoChuClickDownEventHandler(object sender, DanJiClickDownEventArgs e);//事件所需的委托 //当颜色改变时触发事件 public event DanJiClickDownEventHandler DanJiClickDown; public event XiaoChuClickDownEventHandler XiaoChuClickDown; /// <summary> /// 点击事件 /// </summary> /// <param name="e"></param> protected virtual void onDanJiClickDown(DanJiClickDownEventArgs e) { if (DanJiClickDown != null) { DanJiClickDown(this, e);//触发事件 } } /// <summary> /// 消球事件 /// </summary> /// <param name="e"></param> protected virtual void onXiaoChuClickDown(DanJiClickDownEventArgs e) { if (XiaoChuClickDown != null) { this.iZongFen += e.Count; XiaoChuClickDown(this, e);//触发事件 } } /// <summary> /// 点击事件 /// </summary> public class DanJiClickDownEventArgs : EventArgs { private int _Count; public DanJiClickDownEventArgs(int count) { _Count = count; } public int Count { get { return _Count; } } } /// <summary> /// 小球属性 /// </summary> class XiaoQiu : ICloneable { public int I { get; set; } public int J { get; set; } public int X { get; set; } public int Y { get; set; } public int W { get; set; } public int H { get; set; } public Color Color { get; set; } /// <summary> /// N=false 存在,N=true,不存在 /// </summary> public bool N { get; set; } #region ICloneable Members public object Clone() { return this.MemberwiseClone(); } #endregion } }}
主程序:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace DubbleBreaker_GAME{ public partial class GAME : Form { public GAME() { InitializeComponent(); this.uc.DanJiClickDown += new DubbleBreaker.DanJiClickDownEventHandler(userControl11_DanJiClickDown); this.uc.XiaoChuClickDown += new DubbleBreaker.XiaoChuClickDownEventHandler(userControl11_XiaoChuClickDown); this.uc.Start(); } void userControl11_XiaoChuClickDown(object sender, DubbleBreaker.DanJiClickDownEventArgs e) { this.lb_1.Text = "0"; this.lb_2.Text = uc.GetZongFen().ToString(); } void userControl11_DanJiClickDown(object sender, DubbleBreaker.DanJiClickDownEventArgs e) { this.lb_1.Text = e.Count.ToString() ; } private void button1_Click(object sender, EventArgs e) { this.lb_1.Text = "0"; uc.Back(); this.lb_2.Text = uc.GetZongFen().ToString(); } private void button2_Click(object sender, EventArgs e) { uc.Start(); this.lb_1.Text = "0"; this.lb_2.Text = "0"; } private void Form1_Load(object sender, EventArgs e) { } private void button3_Click(object sender, EventArgs e) { this.lb_1.Text = "0"; uc.Go(); this.lb_2.Text = uc.GetZongFen().ToString(); } }}
完整项目文件,sorry,居然不允许上传附件。。。。。。如有需要,请留邮箱吧!
网龄也有10来年,第一次这么认真的发博客。
- winphone平台下的DubbleBreaker游戏代码——代码分享一
- 移动游戏平台的新趋势分享—91-mgas大会
- 一键分享代码
- 一键分享代码
- 一键分享代码
- java 猜拳游戏代码分享
- (实用)简单的百度各平台分享代码
- 分享到微信微博空间等第三方平台的JS代码
- 分享到微信微博空间等第三方平台的JS代码
- TinyOS平台下一些代码的分析
- 分享一个单机游戏的NSIS封装包脚本代码
- 分享一个猜数字游戏的JAVA代码
- 分享一个完整程序的代码(消除类游戏)
- 25个不错PHP的游戏编程脚本代码分享
- J2ME游戏代码示例(一)——俄罗斯方块
- J2ME游戏代码示例(一)——俄罗斯方块(转载)
- 分享各站点代码一、
- 网站一键分享代码
- shell 特殊符号
- 【面试攻略】化繁为简,把HR的上百道问题汇总成七大类(值得收藏)
- smarty总结(3)
- 安装 Chrome stable 版本
- Sybase ASE数据库相关资源整理,有问题请在文后留言
- winphone平台下的DubbleBreaker游戏代码——代码分享一
- uboot mkimage load address entry address
- 技术积累应用-通用编辑类
- 菩提本无树
- 技术积累应用-可视化编程平台
- worldwind1.4C#版编译问题汇总
- 九度oj 1020 转自别人的博客,想借鉴
- Objective-C语言:反射机制
- Objective-C语言:消息机制