您好,欢迎光临本网站![请登录][注册会员]  
文件名称: c#做的俄罗斯方块,不下后悔
  所属分类: C#
  开发工具:
  文件大小: 242kb
  下载次数: 0
  上传时间: 2009-05-18
  提 供 者: zhou1*****
 详细说明: 提供一个核心类,接口仅几个,方便外围开发,你可以在此基础上做一些扩展,如使其界面更美观,方块更美观等 class DyPanel :Panel { #region 类属性 private Timer t_slow,t_fast; private XY current_p, start_p; private XY []cur_square; private XY []afterChange_squar; private int wi dth, height; private SolidBrush bkbrush; private bool [][]ha; private int m, n; private XY [][][]a; private int rotate_outer, rotate_inner; private int outer, inner; private Random ra; private Panel p; private XY reference; private int mark; private Label markl; public char move_flag; private SolidBrush sqbrush; #endregion #region 公有的方法 public DyPanel(Rectangle rf,int width,int height,SolidBrush brush,ref Timer slow,ref Timer fast,Panel p,XY reference,Label markl) { sqbrush = new SolidBrush(Color.FromArgb(150,225,200,12)); this.markl = markl; mark = 0; markl.Text = mark.ToString(); ra = new Random(); this.p = p; this.reference = reference; a=new XY[6][][]; for (int i = 0; i < 6;i++ ) a[i]=new XY[4][]; for (int i = 0; i < 6; i++) { for (int j = 0; j < 4; j++) { a[i][j]=new XY[4]; for (int e = 0; e < 4; e++) a[i][j][e] = new XY(); } } setA(); cur_square=new XY[4]; afterChange_squar=new XY[4]; for (int i = 0; i < 4; i++) { cur_square[i] = new XY(); afterChange_squar[i] = new XY(); } outer = rand(0, 6); inner = rand(0, 4); rotate_outer = outer; rotate_inner = inner; copyXY(a[outer][inner], ref cur_square); outer = rand(0, 6); inner = rand(0, 4); current_p = new XY(); start_p = new XY(); this.width = width; this.height = height; this.bkbrush = brush; this.t_slow = slow; this.t_fast = fast; move_flag = 'p'; this.Size = new Size(rf.Right - rf.Left, rf.Bottom - rf.Top); this.Location = rf.Location; n = (this.ClientRectangle.Right - this.ClientRectangle.Left) / width; m = (this.ClientRectangle.Bottom - this.ClientRectangle.Top) / height; this.start_p.x = n / 2; this.start_p.y = 2; current_p.x = start_p.x; current_p.y = start_p.y; ha = new bool[m][]; for (int i = 0; i < m; i++) { ha[i] = new bool[n]; for (int j = 0; j < n; j++) ha[i][j] = false; } this.BackColor = Color.SlateBlue; this.Paint += new System.Windows.Forms.PaintEventHandler(this.panel_Paint); setTimer(true, false); } public int getMark() { return mark; } public void ReStart() { setTimer(false, false); mark = 0; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) ha[i][j] = false; current_p.x=start_p.x; current_p.y = start_p.y; mark = 0; markl.Text = mark.ToString(); Logic_Expunction(); Real_Expunction(); } public void draw() { Timer cur_t = t_fast.Enabled ? t_fast : t_slow; cur_t.Enabled = false; XY xy = new XY(); XY []original=new XY[4]; for (int i = 0; i < 4; i++) original[i] = new XY(); if (Square_Move(move_flag, ref xy,ref original)) { if (move_flag != 'w' && move_flag != 'W') { setSelfLogic(cur_square, xy, false, 4); int i; for (i = 0; i < 4; i++) { if (ha[cur_square[i].y - cur_square[0].y + current_p.y][cur_square[i].x - cur_square[0].x + current_p.x]) break; } if(i==4) { draw_square(new SolidBrush(this.BackColor), xy); setSelfLogic(cur_square, current_p, true, 4); draw_square(sqbrush, current_p); } else { current_p.x = xy.x; current_p.y = xy.y; setSelfLogic(cur_square, current_p, true, 4); } } else { setSelfLogic(original, xy, false, 4); draw_square(new SolidBrush(this.BackColor), xy,original); setSelfLogic(cur_square, current_p, true, 4); draw_square(sqbrush, current_p); } } else { setSelfLogic(cur_square, current_p, true, 4); current_p.x = start_p.x; current_p.y = start_p.y; if (IsFail()) { MessageBox.Show("Fail!"); setTimer(false, false); return; } copyXY(a[outer][inner], ref cur_square); rotate_outer = outer; rotate_inner = inner; outer = rand(0, 6); inner = rand(0, 4); p.Invalidate(); Logic_Expunction(); Real_Expunction(); } move_flag = 'p'; cur_t.Enabled = true; } public void setTimer(bool slow, bool fast) { t_slow.Enabled = slow; t_fast.Enabled = fast; } public void draw(Panel p,bool bkColor) { Color color; if(bkColor) { color = p.BackColor; } else { color = sqbrush.Color; } for (int i = 0; i < 4; i++) { p.CreateGraphics().DrawRectangle(new Pen(Brushes.Chocolate, 1), (a[outer][inner][i].x - a[outer][inner][0].x + reference.x) * width, (a[outer][inner][i].y - a[outer][inner][0].y + reference.y) * height, width, height); } } #endregion #region 私有的方法 private bool Square_Move(char a, ref XY xy,ref XY []original) { switch (a) { case 'a': case 'A': if (CanLeft() && CanDown()) { xy.x = current_p.x; xy.y = current_p.y; current_p.x--; current_p.y++; } else if(CanDown()) { xy.x = current_p.x; xy.y = current_p.y; current_p.y++; } else return false; break; case 'd': case 'D': if (CanRight() && CanDown()) { xy.x = current_p.x; xy.y = current_p.y; current_p.x++; current_p.y++; } else if (CanDown()) { xy.x = current_p.x; xy.y = current_p.y; current_p.y++; } else return false; break; case 'w': case 'W': if(CanRotate()) { copyXY(cur_square, ref original); copyXY(afterChange_squar, ref cur_square); xy.x = current_p.x; xy.y = current_p.y; //current_p.y++; } break; default: if (CanDown()) { xy.x = current_p.x; xy.y = current_p.y; current_p.y++; } else return false; break; } return true; } private int rand(int least,int largest) { return ra.Next(least, largest); } private void copyXY(XY[] s, ref XY[] t) { for (int i = 0; i < 4; i++) { t[i].x = s[i].x; t[i].y = s[i].y; } } private void draw_square(SolidBrush sb, XY reference) { for (int i = 0; i < 4; i++) { this.CreateGraphics().FillRectangle(sb, (cur_square[i].x - cur_square[0].x + reference.x) * width, (cur_square[i].y - cur_square[0].y + reference.y) * height, width, height); } } private void draw_square(SolidBrush sb, XY reference,XY []orientation) { for (int i = 0; i < 4; i++) { this.CreateGraphics().FillRectangle(sb, (orientation[i].x - orientation[0].x + reference.x) * width, (orientation[i].y - orientation[0].y + reference.y) * height, width, height); } } private void setA() { a[0][0][0].x = 1; a[0][0][0].y = 1; a[0][0][1].x = 0; a[0][0][1].y = 1; a[0][0][2].x = 0; a[0][0][2].y = 2; a[0][0][3].x = 1; a[0][0][3].y = 0; a[0][1][0].x = 1; a[0][1][0].y = 1; a[0][1][1].x = 0; a[0][1][1].y = 0; a[0][1][2].x = 1; a[0][1][2].y = 0; a[0][1][3].x = 2; a[0][1][3].y = 1; a[0][2][0].x = 1; a[0][2][0].y = 1; a[0][2][1].x = 0; a[0][2][1].y = 1; a[0][2][2].x = 0; a[0][2][2].y = 2; a[0][2][3].x = 1; a[0][2][3].y = 0; a[0][3][0].x = 1; a[0][3][0].y = 1; a[0][3][1].x = 0; a[0][3][1].y = 0; a[0][3][2].x = 1; a[0][3][2].y = 0; a[0][3][3].x = 2; a[0][3][3].y = 1; //end 0 a[1][0][0].x = 1; a[1][0][0].y = 1; a[1][0][1].x = 0; a[1][0][1].y = 0; a[1][0][2].x = 0; a[1][0][2].y = 1; a[1][0][3].x = 1; a[1][0][3].y = 2; a[1][1][0].x = 1; a[1][1][0].y = 1; a[1][1][1].x = 0; a[1][1][1].y = 1; a[1][1][2].x = 1; a[1][1][2].y = 0; a[1][1][3].x = 2; a[1][1][3].y = 0; a[1][2][0].x = 1; a[1][2][0].y = 1; a[1][2][1].x = 0; a[1][2][1].y = 0; a[1][2][2].x = 0; a[1][2][2].y = 1; a[1][2][3].x = 1; a[1][2][3].y = 2; a[1][3][0].x = 1; a[1][3][0].y = 1; a[1][3][1].x = 0; a[1][3][1].y = 1; a[1][3][2].x = 1; a[1][3][2].y = 0; a[1][3][3].x = 2; a[1][3][3].y = 0; //end 1 a[2][0][0].x = 1; a[2][0][0].y = 1; a[2][0][1].x = 0; a[2][0][1].y = 0; a[2][0][2].x = 1; a[2][0][2].y = 0; a[2][0][3].x = 0; a[2][0][3].y = 1; a[2][1][0].x = 1; a[2][1][0].y = 1; a[2][1][1].x = 0; a[2][1][1].y = 0; a[2][1][2].x = 1; a[2][1][2].y = 0; a[2][1][3].x = 0; a[2][1][3].y = 1; a[2][2][0].x = 1; a[2][2][0].y = 1; a[2][2][1].x = 0; a[2][2][1].y = 0; a[2][2][2].x = 1; a[2][2][2].y = 0; a[2][2][3].x = 0; a[2][2][3].y = 1; a[2][3][0].x = 1; a[2][3][0].y = 1; a[2][3][1].x = 0; a[2][3][1].y = 0; a[2][3][2].x = 1; a[2][3][2].y = 0; a[2][3][3].x = 0; a[2][3][3].y = 1; //end 2 a[3][0][0].x = 1; a[3][0][0].y = 1; a[3][0][1].x = 0; a[3][0][1].y = 0; a[3][0][2].x = 1; a[3][0][2].y = 0; a[3][0][3].x = 1; a[3][0][3].y = 2; a[3][1][0].x = 2; a[3][1][0].y = 1; a[3][1][1].x = 2; a[3][1][1].y = 0; a[3][1][2].x = 1; a[3][1][2].y = 1; a[3][1][3].x = 0; a[3][1][3].y = 1; a[3][2][0].x = 1; a[3][2][0].y = 2; a[3][2][1].x = 0; a[3][2][1].y = 0; a[3][2][2].x = 0; a[3][2][2].y = 1; a[3][2][3].x = 0; a[3][2][3].y = 2; a[3][3][0].x = 1; a[3][3][0].y = 0; a[3][3][1].x = 2; a[3][3][1].y = 0; a[3][3][2].x = 0; a[3][3][2].y = 0; a[3][3][3].x = 0; a[3][3][3].y = 1; //end 3 a[4][0][0].x = 0; a[4][0][0].y = 1; a[4][0][1].x = 0; a[4][0][1].y = 0; a[4][0][2].x = 0; a[4][0][2].y = 2; a[4][0][3].x = 1; a[4][0][3].y = 0; a[4][1][0].x = 2; a[4][1][0].y = 1; a[4][1][1].x = 0; a[4][1][1].y = 0; a[4][1][2].x = 1; a[4][1][2].y = 0; a[4][1][3].x = 2; a[4][1][3].y = 0; a[4][2][0].x = 1; a[4][2][0].y = 2; a[4][2][1].x = 0; a[4][2][1].y = 2; a[4][2][2].x = 1; a[4][2][2].y = 0; a[4][2][3].x = 1; a[4][2][3].y = 1; a[4][3][0].x = 1; a[4][3][0].y = 1; a[4][3][1].x = 0; a[4][3][1].y = 0; a[4][3][2].x = 0; a[4][3][2].y = 1; a[4][3][3].x = 2; a[4][3][3].y = 1; //end 4 a[5][0][0].x = 1; a[5][0][0].y = 0; a[5][0][1].x = 0; a[5][0][1].y = 0; a[5][0][2].x = 2; a[5][0][2].y = 0; a[5][0][3].x = 3; a[5][0][3].y = 0; a[5][1][0].x = 0; a[5][1][0].y = 1; a[5][1][1].x = 0; a[5][1][1].y = 2; a[5][1][2].x = 0; a[5][1][2].y = 3; a[5][1][3].x = 0; a[5][1][3].y = 0; a[5][2][0].x = 1; a[5][2][0].y = 0; a[5][2][1].x = 0; a[5][2][1].y = 0; a[5][2][2].x = 2; a[5][2][2].y = 0; a[5][2][3].x = 3; a[5][2][3].y = 0; a[5][3][0].x = 0; a[5][3][0].y = 1; a[5][3][1].x = 0; a[5][3][1].y = 2; a[5][3][2].x = 0; a[5][3][2].y = 3; a[5][3][3].x = 0; a[5][3][3].y = 0; } private bool IsFail() { for (int i = 0; i < 4; i++) if (ha[cur_square[i].y - cur_square[0].y + current_p.y + 1][cur_square[i].x - cur_square[0].x + current_p.x]) { setSelfLogic(cur_square, current_p, true, 4); return true; } return false; } private void setSelfLogic(XY[] xy, XY reference,bool logic,int m) { for(int i=0;i= n) return false; } setSelfLogic(cur_square, current_p, false, 4); for (int i = 0; i < 4; i++) if (ha[cur_square[i].y - cur_square[0].y + current_p.y][cur_square[i].x - cur_square[0].x + current_p.x+1]) { setSelfLogic(cur_square, current_p, true, 4); return false; } setSelfLogic(cur_square, current_p, true, 4); return true; } private bool CanRotate() { XY topleft, buttomright; topleft = new XY(); buttomright = new XY(); topleft.x = cur_square[0].x; topleft.y = cur_square[0].y; buttomright.x = cur_square[0].x; buttomright.y = cur_square[0].y; for (int i = 1; i < 4; i++) { buttomright.x = buttomright.x > cur_square[i].x ? buttomright.x : cur_square[i].x; buttomright.y = buttomright.y > cur_square[i].y ? buttomright.y : cur_square[i].y; topleft.x = topleft.x < cur_square[i].x ? topleft.x : cur_square[i].x; topleft.y = topleft.y < cur_square[i].y ? topleft.y : cur_square[i].y; } for (int y = topleft.y; y <= buttomright.y; y++) for (int x = topleft.x; x <= buttomright.x; x++) if (ha[y - cur_square[0].y + current_p.y][x - cur_square[0].x + current_p.x] && Isnotinself(new XY(x - cur_square[0].x + current_p.x, y - cur_square[0].y + current_p.y), cur_square, current_p, 4)) return false; Rotate(a[rotate_outer], ref afterChange_squar, 4); int temp_x; topleft.x = afterChange_squar[0].x; topleft.y = afterChange_squar[0].y; buttomright.x = afterChange_squar[0].x; buttomright.y = afterChange_squar[0].y; temp_x = afterChange_squar[0].x; for (int i = 1; i < 4; i++) { buttomright.x = buttomright.x > afterChange_squar[i].x ? buttomright.x : afterChange_squar[i].x; buttomright.y = buttomright.y > afterChange_squar[i].y ? buttomright.y : afterChange_squar[i].y; topleft.x = topleft.x < afterChange_squar[i].x ? topleft.x : afterChange_squar[i].x; topleft.y = topleft.y < afterChange_squar[i].y ? topleft.y : afterChange_squar[i].y; } for (int i = 0; i < 4; i++) { if (afterChange_squar[i].y - afterChange_squar[0].y + current_p.y >= m - 1 || afterChange_squar[i].x - afterChange_squar[0].x + current_p.x > n - 1 || afterChange_squar[i].x - afterChange_squar[0].x + current_p.x < 0) return false; } for (int y = topleft.y; y <= buttomright.y; y++) for (int x = topleft.x; x <= buttomright.x; x++) if ((ha[y - afterChange_squar[0].y + current_p.y][x - afterChange_squar[0].x + current_p.x] && Isnotinself(new XY(x - afterChange_squar[0].x + current_p.x, y - afterChange_squar[0].y + current_p.y), cur_square, current_p, 4))) return false; return true; } private bool CanDown() { for (int i = 0; i < 4; i++) { if (cur_square[i].y - cur_square[0].y + current_p.y + 1 > m-1) return false; } setSelfLogic(cur_square, current_p, false, 4); for (int i = 0; i < 4; i++) if (ha[cur_square[i].y - cur_square[0].y + current_p.y + 1][cur_square[i].x - cur_square[0].x +current_p.x]) { setSelfLogic(cur_square, current_p, true, 4); return false; } setSelfLogic(cur_square, current_p, true, 4); return true; } private void Rotate(XY[][] source, ref XY[] retur,int m) { retur[0].x = source[rotate_inner][0].x; retur[0].y = source[rotate_inner][0].y; rotate_inner++; rotate_inner = rotate_inner%4; for(int i=0;i<4;i++) { retur[i].x = source[rotate_inner][i].x; retur[i].y = source[rotate_inner][i].y; } } private bool Isnotinself(XY xy,XY []self,XY reference,int m) { for(int i=0;i= 0; i--) { int j; for (j = 0; j < n; j++) if (ha[i][j] == false) break; if (j == n) { for (int k = i; k >= 1; k--) { for (int e = 0; e < n; e++) ha[k][e] = ha[k - 1][e]; } for (int e = 0; e < n; e++) ha[0][e] = false; mark += 10; markl.Text = mark.ToString(); i++; } } } private void Real_Expunction() { for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) if (ha[i][j]) this.CreateGraphics().FillRectangle(sqbrush, j * width, i * height, width, height); else this.CreateGraphics().FillRectangle(new SolidBrush(this.BackColor), j * width, i * height, width, height); } private void panel_Paint(object sender, PaintEventArgs e) { Logic_Expunction(); Real_Expunction(); } #endregion } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 输入关键字,在本站1000多万海量源码库中尽情搜索: