作为登陆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;