作为登陆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.Ythis.Y && b.X==this.X && b.deleteAble){
dropline+=1;
}
}
}
}
public void drop ()
{
y+=dropline;
dropline=0;
}
public int X {
set {
x=value;
}
get {
return x;
}
}
public int Y {
set {
y = value;
}
get {
return y;
}
}
public Box(int vx,int vy,ConsoleColor col){
x=vx;
y=vy;
deleteAble=false;
color=col;
dropline=0;
}
public Box (int vx,int vy)
{
x=vx;
y=vy;
deleteAble=false;
dropline=0;
}
public void moveL(){
x=x>0?x-1:x;
}
public void moveR(){
x=x0?y-1:y;
}
public void moveD(){
y=y= 100 && title.piece < 200) {
delay = 20;
} else {
delay=15;
}
}
protected bool can_move (string cmd)
{
bool re=true;
switch (cmd) {
case"R":
foreach (Box b in arr) {
if(b.X+1>=MainClass.MAXX || MainClass.onPos(b.X+1,b.Y)){
re=false;
break;
}
}
break;
case"L":
foreach (Box b in arr ) {
if(b.X-1<0 || MainClass.onPos(b.X-1,b.Y)){
re=false;
break;
}
}
break;
case"D":
foreach (Box b in arr) {
if(b.Y+1>=MainClass.MAXY || MainClass.onPos(b.X,b.Y+1)){
re=false;
break;
}
}
break;
}
return re;
}
public bool move (string cmd)
{
//return true :create new Style and put data in ArrayList
bool re = can_move (cmd);
if (re) {
switch (cmd) {
case"R":
foreach (Box b in arr) {
b.moveR ();
}
screen.update (this);
break;
case"L":
foreach (Box b in arr) {
b.moveL ();
}
screen.update (this);
break;
case"U":
rotate();
screen.update (this);
break;
case"D":
foreach (Box b in arr) {
b.moveD ();
}
screen.update (this);
break;
}
}
if (!re && cmd == "D") {
re = true;
} else {
re=false;
}
return re;
}
public bool haveBox (int x, int y)
{
bool re=false;
foreach (Box b in arr) {
if(b.onpos(x,y)){
re=true;
break;
}
}
return re;
}
}
}