惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
B
Blog
U
Unit 42
B
Blog RSS Feed
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
腾讯CDC
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Visual Studio Blog
博客园 - 聂微东
MyScale Blog
MyScale Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 三生石上(FineUI控件)
Engineering at Meta
Engineering at Meta

博客园 - LT

用vs code写代码,spyder看变量,只能这样了 Chrome禁用software_reporter_tool 【转载】Scarbee Pre-Bass 贝司的使用教程 罗兰管弦乐音色表【中英文对照】 ----转载 博文阅读密码验证 - 博客园 火狐无法启动,如何恢复数据、书签、密码 [转]C#获取窗口进程ID与句柄还有读写内存类 一个好用的C#抓图类 【转】WinSocket编程——原始套接字 【转】SHarpPcap(winpcap基于c#封转的库)网络嗅探程序核心 [心得] socket 多路复用误区 UDP 异步 收发 服务端例子 【转载】 UDPClient 的奇特现象,实在搞不懂 [转载]TCP打开端口数和打开连接数限制系列文章 UDP错误10054:远程主机强迫关闭的解决方法---转载 应用程式上屏蔽flash控件的右键菜单并获得左键消息_c# AJAX分段下载/读取HTML内容(有效节省带宽加快运行速度) 使用Fiddler2录制HTTP操作脚本 Subversion for Windows 安装配置
【转】C#写的170行俄罗斯方块
LT · 2012-02-20 · via 博客园 - LT

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;

namespace 俄罗斯方方块
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            this.Load += new System.EventHandler(load);
            InitializeComponent();
        }
        Point[,] ai ={{new Point(150,0),new Point(150,-30),new Point(120,-30),new Point(180,-30)},//T
                      {new Point(150,0),new Point(150,-30),new Point(150,-60),new Point(150,-90)},//长条
                      {new Point(150,0),new Point(150,-30),new Point(150,-60),new Point(120,-60)},//L
                      {new Point(150,0),new Point(180,0),new Point(150,-30),new Point(120,-30)},//Z
                      {new Point(150,0),new Point(180,0),new Point(150,-30),new Point(180,-30)}};//方块
        Label a = new Label();
        Label b = new Label();
        Label c = new Label();
        Label d = new Label();//用户控制的方块
        Random r = new Random();//随机种子
        int bi = 0;//方块的种类
        Timer tm = new Timer();//计时器
        private void load(object sender, EventArgs e)
        {
            Width = 300;
            Height = 360;
            Location = new Point(500150);
            Text = "俄罗斯方块";
            KeyPreview = true;
            KeyPress += new KeyPressEventHandler(press);//布局
            a.Size = b.Size = c.Size = d.Size = new Size(3030);
            a.BackColor = b.BackColor = c.BackColor = d.BackColor = Color.Red;
            Controls.Add(a);
            Controls.Add(b);
            Controls.Add(c);
            Controls.Add(d);//画方块    
            xuanze();
            //计时器
            tm.Enabled = true;
            tm.Interval = 700;
            tm.Tick += new EventHandler(jishi);
        }
        void xuanze()//选择种类
        {
            bi = r.Next(05);
            a.Location = ai[bi, 0];
            b.Location = ai[bi, 1];
            c.Location = ai[bi, 2];
            d.Location = ai[bi, 3];
            jieshu();
        }
        void press(object sender, KeyPressEventArgs e)
        {
            int i=0;
            string key = e.KeyChar.ToString();
            if (key == "a") i=xialuo(-300);
            if (key == "d") i=xialuo(300);
            if (key == "s") i = xialuo(030);//移动碰撞判断
            if (key == "s" && (a.Top == 300 || b.Top == 300 || c.Top == 300 || d.Top == 300)) i = 1;//移动出界判断
            if (key == "a" && (a.Left == 0 || b.Left == 0 || c.Left == 0 || d.Left == 0)) i = 1;
            if (key == "d" && (a.Left == 270 || b.Left == 270 || c.Left == 270 || d.Left == 270)) i = 1;
            if (key == "w") i = xuanzhuan();//旋转碰撞判断
            if(i==0)
            switch (key)
            {
                case "a": a.Left -= 30; b.Left -= 30; c.Left -= 30; d.Left -= 30break;
                case "d": a.Left += 30; b.Left += 30; c.Left += 30; d.Left += 30break;
                case "s": a.Top += 30; b.Top += 30; c.Top += 30; d.Top += 30break;
                case "w":
                    if (bi<4)//if里面是旋转的算法 
                    {
                        int m = b.Top - 30, n = b.Left - 30;
                        c.Location = new Point(c.Top - m + n, 60 - (c.Left - n) + m); 
                        a.Location = new Point(a.Top - m + n, 60 - (a.Left - n) + m);
                        d.Location = new Point(d.Top - m + n, 60 - (d.Left - n) + m);
                    }
                    break;
            }
        }
        void jishi(object sender, EventArgs e)
        {
            if (xialuo(030) == 1) huahua();
            panduan();
            a.Top += 30; b.Top += 30; c.Top += 30; d.Top += 30;

        }
        void panduan()////最下碰撞判断
        {                       
            if (a.Top == 300 || b.Top == 300 || c.Top == 300 || d.Top == 300)
                huahua();
        }
        void huahua()//画画- -!
        {
            Point[] pp={a.Location,b.Location,c.Location,d.Location};
            for (int i = 0; i < 4; i++)
            {
                Label s = new Label();
                s.BackColor = Color.Blue;
                s.Size = new Size(3030);
                s.Location = pp[i];
                Controls.Add(s);
            }
            xiaotu();
            xuanze();
        }
        int xialuo(int x,int y)//移动碰撞判断
        {
            foreach (Control i in Controls)
                if (i.BackColor == Color.Blue)
                    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))
                        return 1;
            return 0;           
        }
        int xuanzhuan()//旋转碰撞判断
        {
            Point[] pp = { a.Location, b.Location, c.Location, d.Location };
            int m = pp[1].Y - 30, n = pp[1].X - 30;
            pp[2]= new Point(pp[2].Y - m + n, 60 - (pp[2].X - n) + m);
            pp[0] = new Point(pp[0].Y - m + n, 60 - (pp[0].X - n) + m);
            pp[3] = new Point(pp[3].Y - m + n, 60 - (pp[3].X - n) + m);
            foreach (Control i in Controls)
                if (i.BackColor == Color.Blue)
                    if(i.Location==pp[1]||i.Location==pp[0]||i.Location==pp[2]||i.Location==pp[3])
                        return 1;
            return 0;
        }
        void xiaotu()//消图
        {
            int k = 0;
            for(int m=0;m<4;m++)
            for (int i = 0; i <= 300; i += 30)
            {
                k = 0;
                foreach (Control l in Controls)
                    if (l.BackColor == Color.Blue && l.Top == i)
                        k++;
                if (k > 9)
                    foreach (Control l in Controls)
                    {
                        if (l.Top == i)
                            l.Location = new Point(-100, -100);
                        if (l.Top <i)
                            l.Top += 30;
                    }
            }
            foreach(Control l in Controls)
            if(l.Location==new Point(-100,-100))
             Controls.Remove(l);
        }
        void jieshu()//游戏结束判断
        {
            foreach(Control i in Controls)
                if(i.BackColor==Color.Blue)
                    if (i.Location == a.Location || i.Location == b.Location || i.Location == c.Location || i.Location == d.Location)
                    {
                        tm.Stop();
                        MessageBox.Show("游戏结束");
                        Close();
                    }
        }
    }
}