作者: 乔克斯
查看: 3677|回复: 7

主题标签Tag

打印 上一主题 下一主题

[游戏] 【俄罗斯方块】120行代码编写俄罗斯方块

[复制链接]
跳转到指定楼层
楼主
乔克斯 发表于 2015-1-20 10:16:28 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
查看: 3677|回复: 7
【俄罗斯方块】120行代码编写俄罗斯方块
说明:
本案例展示,如何只用120行代码,编写一个俄罗斯方块游戏!代码在于精简并实现功能!

效果图:


代码:
[C#] 纯文本查看 复制代码
using System;
using System.Drawing;
using System.Windows.Forms;

namespace 俄罗斯方方块
{
    public partial class FrmMain : Form
    {
        public FrmMain() {
            InitializeComponent();
            xuanze();//选择种类
        }
        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)}};//方块
        Random r = new Random();//随机种子
        int bi = 0;//方块的种类
        private void load(object sender, EventArgs e) {
        }
        void xuanze() {//选择种类
            bi = r.Next(0, 5);
            a.Location = ai[bi, 0];
            b.Location = ai[bi, 1];
            c.Location = ai[bi, 2];
            d.Location = ai[bi, 3];
            jieshu();//游戏结束判断
        }
        private void FrmMain_KeyUp(object sender, KeyEventArgs e) {
            int i = 0;
            int key = e.KeyValue;//37左38上39右40下
            i = key == 37 ? xialuo(-30, 0) : key == 39 ? xialuo(30, 0) : key == 40 ? xialuo(0, 30) : i;
            if (key == 40 && (a.Top == 300 || b.Top == 300 || c.Top == 300 || d.Top == 300)) i = 1;//移动出界判断
            if (key == 37 && (a.Left == 0 || b.Left == 0 || c.Left == 0 || d.Left == 0)) i = 1;
            if (key == 39 && (a.Left == 270 || b.Left == 270 || c.Left == 270 || d.Left == 270)) i = 1;
            if (key == 38) i = xuanzhuan();//旋转碰撞判断
            if (i == 0)
                switch (key) {
                    case 37: a.Left -= 30; b.Left -= 30; c.Left -= 30; d.Left -= 30; break;
                    case 39: a.Left += 30; b.Left += 30; c.Left += 30; d.Left += 30; break;
                    case 40: a.Top += 30; b.Top += 30; c.Top += 30; d.Top += 30; break;
                    case 38:
                        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(0, 30) == 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(30, 30);
                s.Location = pp;
                s.BorderStyle = BorderStyle.FixedSingle;
                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; ((k = 0) == 0) && i <= 300; i += 30) {
                    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();
                        if (MessageBox.Show("游戏结束,是否重新开始?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK) {
                        } else {
                            Close();
                        }
                    }
        }
    }
}


案例源码下载:
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏4 转播转播
回复 论坛版权

使用道具 举报

沙发
CastleDrv 发表于 2015-1-20 11:37:03 | 只看该作者
之前看过深入体验C#项目开发。里面有这个。算法类似(还能怎样- -)
话说——这种书真的很垃圾,书太浅显,只说控件的使用不说原理实现,简直坑爹。
光盘视频就是念一遍书里的文字,强烈不建议新手看这本书
板凳
古调不弹 发表于 2015-1-30 18:30:21 | 只看该作者
学习学习
回复

使用道具 举报

地板
psetpsetpset 发表于 2015-2-5 12:30:24 | 只看该作者
太厉害了。。学习一下。
5#
tjf0303 发表于 2015-2-9 12:50:37 | 只看该作者
经典的游戏,不错,120行不行吧,看起来不错啊
6#
简单就是幸福 发表于 2016-4-12 14:53:25 | 只看该作者
看一下看一下
7#
雨天 发表于 2017-10-22 21:40:03 | 只看该作者
学习一下
回复

使用道具 举报

8#
喵喵喵? 发表于 2017-11-12 22:23:18 | 只看该作者
感谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入CSkin博客

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|CSkin ( 粤ICP备13070794号

Powered by Discuz! X3.2  © 2001-2013 Comsenz Inc.  Designed by ARTERY.cn
GMT+8, 2024-11-16 13:50, Processed in 0.678676 second(s), 37 queries , Gzip On.

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