开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 4381|回复: 2

【转】170行代码的俄罗斯方块

[复制链接]

0

精华

250

贡献

29

赞扬

帖子
56
软币
1362
在线时间
185 小时
注册时间
2013-6-12
发表于 2013-6-19 10:09:02 | 显示全部楼层 |阅读模式
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;

  9. namespace 俄罗斯方方块
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             this.Load += new System.EventHandler(load);
  16.             InitializeComponent();
  17.         }
  18.         Point[,] ai ={{new Point(150,0),new Point(150,-30),new Point(120,-30),new Point(180,-30)},//T
  19.                       {new Point(150,0),new Point(150,-30),new Point(150,-60),new Point(150,-90)},//长条
  20.                       {new Point(150,0),new Point(150,-30),new Point(150,-60),new Point(120,-60)},//L
  21.                       {new Point(150,0),new Point(180,0),new Point(150,-30),new Point(120,-30)},//Z
  22.                       {new Point(150,0),new Point(180,0),new Point(150,-30),new Point(180,-30)}};//方块
  23.         Label a = new Label();
  24.         Label b = new Label();
  25.         Label c = new Label();
  26.         Label d = new Label();//用户控制的方块
  27.         Random r = new Random();//随机种子
  28.         int bi = 0;//方块的种类
  29.         Timer tm = new Timer();//计时器
  30.         private void load(object sender, EventArgs e)
  31.         {
  32.             Width = 300;
  33.             Height = 360;
  34.             Location = new Point(500, 150);
  35.             Text = "俄罗斯方块";
  36.             KeyPreview = true;
  37.             KeyPress += new KeyPressEventHandler(press);//布局
  38.             a.Size = b.Size = c.Size = d.Size = new Size(30, 30);
  39.             a.BackColor = b.BackColor = c.BackColor = d.BackColor = Color.Red;
  40.             Controls.Add(a);
  41.             Controls.Add(b);
  42.             Controls.Add(c);
  43.             Controls.Add(d);//画方块   
  44.             xuanze();
  45.             //计时器
  46.             tm.Enabled = true;
  47.             tm.Interval = 700;
  48.             tm.Tick += new EventHandler(jishi);
  49.         }
  50.         void xuanze()//选择种类
  51.         {
  52.             bi = r.Next(0, 5);
  53.             a.Location = ai[bi, 0];
  54.             b.Location = ai[bi, 1];
  55.             c.Location = ai[bi, 2];
  56.             d.Location = ai[bi, 3];
  57.             jieshu();
  58.         }
  59.         void press(object sender, KeyPressEventArgs e)
  60.         {
  61.             int i=0;
  62.             string key = e.KeyChar.ToString();
  63.             if (key == "a") i=xialuo(-30, 0);
  64.             if (key == "d") i=xialuo(30, 0);
  65.             if (key == "s") i = xialuo(0, 30);//移动碰撞判断
  66.             if (key == "s" && (a.Top == 300 || b.Top == 300 || c.Top == 300 || d.Top == 300)) i = 1;//移动出界判断
  67.             if (key == "a" && (a.Left == 0 || b.Left == 0 || c.Left == 0 || d.Left == 0)) i = 1;
  68.             if (key == "d" && (a.Left == 270 || b.Left == 270 || c.Left == 270 || d.Left == 270)) i = 1;
  69.             if (key == "w") i = xuanzhuan();//旋转碰撞判断
  70.             if(i==0)
  71.             switch (key)
  72.             {
  73.                 case "a": a.Left -= 30; b.Left -= 30; c.Left -= 30; d.Left -= 30; break;
  74.                 case "d": a.Left += 30; b.Left += 30; c.Left += 30; d.Left += 30; break;
  75.                 case "s": a.Top += 30; b.Top += 30; c.Top += 30; d.Top += 30; break;
  76.                 case "w":
  77.                     if (bi<4)//if里面是旋转的算法
  78.                     {
  79.                         int m = b.Top - 30, n = b.Left - 30;
  80.                         c.Location = new Point(c.Top - m + n, 60 - (c.Left - n) + m);
  81.                         a.Location = new Point(a.Top - m + n, 60 - (a.Left - n) + m);
  82.                         d.Location = new Point(d.Top - m + n, 60 - (d.Left - n) + m);
  83.                     }
  84.                     break;
  85.             }
  86.         }
  87.         void jishi(object sender, EventArgs e)
  88.         {
  89.             if (xialuo(0, 30) == 1) huahua();
  90.             panduan();
  91.             a.Top += 30; b.Top += 30; c.Top += 30; d.Top += 30;

  92.         }
  93.         void panduan()////最下碰撞判断
  94.         {                       
  95.             if (a.Top == 300 || b.Top == 300 || c.Top == 300 || d.Top == 300)
  96.                 huahua();
  97.         }
  98.         void huahua()//画画- -!
  99.         {
  100.             Point[] pp={a.Location,b.Location,c.Location,d.Location};
  101.             for (int i = 0; i < 4; i++)
  102.             {
  103.                 Label s = new Label();
  104.                 s.BackColor = Color.Blue;
  105.                 s.Size = new Size(30, 30);
  106.                 s.Location = pp[i];
  107.                 Controls.Add(s);
  108.             }
  109.             xiaotu();
  110.             xuanze();
  111.         }
  112.         int xialuo(int x,int y)//移动碰撞判断
  113.         {
  114.             foreach (Control i in Controls)
  115.                 if (i.BackColor == Color.Blue)
  116.                     if (i.Location == new Point(a.Left + x, a.Top + y) || i.Location == new Point(b.Left + x, b.Top + y) || i.Location == new Point(c.Left + x, c.Top + y) || i.Location == new Point(d.Left + x, d.Top + y))
  117.                         return 1;
  118.             return 0;           
  119.         }
  120.         int xuanzhuan()//旋转碰撞判断
  121.         {
  122.             Point[] pp = { a.Location, b.Location, c.Location, d.Location };
  123.             int m = pp[1].Y - 30, n = pp[1].X - 30;
  124.             pp[2]= new Point(pp[2].Y - m + n, 60 - (pp[2].X - n) + m);
  125.             pp[0] = new Point(pp[0].Y - m + n, 60 - (pp[0].X - n) + m);
  126.             pp[3] = new Point(pp[3].Y - m + n, 60 - (pp[3].X - n) + m);
  127.             foreach (Control i in Controls)
  128.                 if (i.BackColor == Color.Blue)
  129.                     if(i.Location==pp[1]||i.Location==pp[0]||i.Location==pp[2]||i.Location==pp[3])
  130.                         return 1;
  131.             return 0;
  132.         }
  133.         void xiaotu()//消图
  134.         {
  135.             int k = 0;
  136.             for(int m=0;m<4;m++)
  137.             for (int i = 0; i <= 300; i += 30)
  138.             {
  139.                 k = 0;
  140.                 foreach (Control l in Controls)
  141.                     if (l.BackColor == Color.Blue && l.Top == i)
  142.                         k++;
  143.                 if (k > 9)
  144.                     foreach (Control l in Controls)
  145.                     {
  146.                         if (l.Top == i)
  147.                             l.Location = new Point(-100, -100);
  148.                         if (l.Top <i)
  149.                             l.Top += 30;
  150.                     }
  151.             }
  152.             foreach(Control l in Controls)
  153.             if(l.Location==new Point(-100,-100))
  154.              Controls.Remove(l);
  155.         }
  156.         void jieshu()//游戏结束判断
  157.         {
  158.             foreach(Control i in Controls)
  159.                 if(i.BackColor==Color.Blue)
  160.                     if (i.Location == a.Location || i.Location == b.Location || i.Location == c.Location || i.Location == d.Location)
  161.                     {
  162.                         tm.Stop();
  163.                         MessageBox.Show("游戏结束");
  164.                         Close();
  165.                     }
  166.         }
  167.     }
  168. }
复制代码
下载地址

评分

参与人数 1贡献 +2 收起 理由
羽叶 + 2 感谢分享

查看全部评分

回复

使用道具 举报

0

精华

1057

贡献

1950

赞扬

正版授权组

Rank: 14Rank: 14Rank: 14Rank: 14

帖子
320
软币
15538
在线时间
1534 小时
注册时间
2013-6-9
发表于 2013-6-19 20:28:22 | 显示全部楼层
呵呵,排版以后看着舒服多了
回复

使用道具 举报

0

精华

486

贡献

29

赞扬

帖子
143
软币
3676
在线时间
276 小时
注册时间
2013-6-7
发表于 2013-6-19 21:11:54 | 显示全部楼层
编译一下试试,呵呵
回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-5-6 16:57

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表