C#命令行俄罗斯方块

这篇博客介绍了作者用C#语言编写的命令行版俄罗斯方块游戏,适合学习C#编程和游戏开发的读者。作者计划分享更多关于AS、H5和C#的学习笔记。
摘要由CSDN通过智能技术生成

作为登陆csdn第一篇内容,这里贴出很久以前学c#时写过的命令行俄罗斯方块游戏;有兴趣的朋友可自行编译一个玩玩;
往后我还将发布更多关于as h5 c#相关的学习笔记;
在这里插入图片描述

using System;
using System.Threading;
using System.Collections;
using System.Diagnostics;
namespace box
{
    class MainClass
    {
        public static string cmd;
        public const int MAXX=20;
        public const int MAXY=21;
        static Block bn;
        public static ArrayList boxcore;
        protected static bool pressed;
        public static bool endAble;
        public static ConsoleColor currentColor;
    public static void unlockpress ()
    {
        pressed=false;
    }
        public static void Main (string[] args)
        {
        Start:initGame();
        Conf:while(true){
                Thread.Sleep(40);
                if(!endAble){
                bn.run();
                pressed=false;
                if(Console.KeyAvailable && !pressed){
                    ConsoleKeyInfo inf=Console.ReadKey ();
                    if(inf.Key==ConsoleKey.RightArrow){
                        cmd="R";                
                        pressed=true;
                        break;
                    }else if(inf.Key==ConsoleKey.LeftArrow){
                        cmd="L";
                        pressed=true;
                        break;
                    }else if(inf.Key==ConsoleKey.UpArrow){
                        cmd="U";
                        pressed=true;
                        break;
                    }else if(inf.Key==ConsoleKey.DownArrow){
                        cmd="D";
                        pressed=true;
                        break;
                    }
                }
                }else{
                    break;
                }
            }
            bn.move (cmd);
            cmd="";
            if(endAble){
                goto End;
            }
            goto Conf;
        End:    Console.Clear ();
            Console.WriteLine ("     □□□□□□□□□□□□□□□□□□□□");
            Console.WriteLine ("             Game Over       ");
 
        Console.WriteLine ("            Score:" + title.score);
        Console.WriteLine ("            Piece:" + title.piece);
        Console.WriteLine ("            Replay? Y/N");
        Console.WriteLine ("     □□□□□□□□□□□□□□□□□□□□");    
        if (Console.ReadLine()=="Y") {
            goto Start;
        }
    }

    protected static void initGame ()
    {//init game
    //  ConsoleColor col=Block.RandomColor ();
        bn=new Block(new Box[4]{new Box((int)MAXX/2,0),new Box((int)MAXX/2,1),new Box((int)MAXX/2+1,0),new Box((int)MAXX/2+1,1)});
        if(boxcore!=null){
            boxcore.Clear ();
        }
        boxcore=new ArrayList();
        pressed=false;
        currentColor=ConsoleColor.Cyan;
        Console.ForegroundColor=currentColor;
        title.init ();
        screen.update(bn);
        endAble=false;

    }
    public static bool isOver (Block b)
    {
        bool re=false;
        foreach(Box box in b.Arr){
            if(box.Y==0){
                re=true;
                break;
            }
        }
        return re;
    }

    public static void releaseBlock (Block b)
    {
        //release all box of this block to box pool.
        if (!isOver (b)) {
            Box[] arr = b.Arr;

            foreach (Box box in arr) {
                boxcore.Add (box);
            }
            title.piece += 1;
            b.ca_delay();
            //then must check the line that whether will be clear
            ArrayList checkline = b.getCheckArr ();
            //Debug.Print (debugline (checkline));
            int dropline = 0;
            foreach (int i in checkline) {
                dropline += check (i);
            }
            if (dropline > 1) {
                title.addScore (25 * dropline);
            }

            foreach (Box box in boxcore) {
                box.ca_dropline (boxcore);
            }
            ArrayList narr=new ArrayList();

            foreach(Box box in boxcore){
                if(!box.deleteAble){
                    narr.Add (box);
                }
            }
            boxcore=narr;
            foreach (Box box in boxcore) {
                box.drop ();
            }

            ca_color(); 
        } else {
            MainClass.endAble=true;
        }
    }
    public static void ca_color ()
    {
        //According to minY transfrom color 
        float pre =(float)getMINY ()/MainClass.MAXY ;//
        ConsoleColor col=ConsoleColor.Cyan;
        //Debug.Print (pre.ToString ());
        if (pre <= 0.2f) {
            col = ConsoleColor.Red;
        } else if (pre > 0.2f && pre <= 0.4f) {
            col = ConsoleColor.Magenta;
        } else if (pre > 0.4f && pre <= 0.6f) {
            col = ConsoleColor.Yellow;
        } else if (pre > 0.6f && pre <= 0.8f) {
            col = ConsoleColor.Green;
        } else {
            col=ConsoleColor.Cyan;
        }
        if(currentColor!=col){
            currentColor=col;
            Console.ForegroundColor=currentColor;
        }
    }
    protected static int getMINY ()
    {
        int re=MAXY;
        foreach(Box b in boxcore){
            if(b.Y<re){
                re=b.Y;
            }
        }
        return re;
    }
    public static string debugline (ArrayList arr)
    {
        string re="";
        for(int i=0;i<arr.Count;i++){
            re+=arr[i].ToString ();
        }
        return re;
    }
    public static int check (int line)
    {
        int x;
        int re=0;
        bool clear=true;
        for(x=0;x<MAXX;x++){
            if(!findPos(x,line)){
                clear=false;
                break;
            }
        }
        if(clear){
            Box box=null;
            for(x=0;x<MAXX;x++){
                box=findBox (x,line);
                box.deleteAble=true;
            }
            title.addScore (100);
            //
            re=1;
        }
        return re;
    }
    public static Box findBox(int vx,int vy){
        Box re=null;
     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值