?? gamefield.cs
字號:
?using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Media;
using System.IO;
namespace ChinaBlock
{
class GameField
{
public const int width = 20; //場景的寬,以方塊個數為單位
public const int height = 30;
public const int SquareSize = 15; //每個四分之一小方塊的邊長
public static Color BackColor; //場景的背景色
public static System.IntPtr winHandle; //場景的handle
public static Color[] BlockForeColor ={ Color.Blue, Color.Beige, Color.DarkKhaki, Color.DarkMagenta, Color.DarkOliveGreen, Color.DarkOrange, Color.DarkRed };
public static Color[] BlockBackColor ={ Color.LightCyan, Color.DarkSeaGreen, Color.Beige, Color.Beige, Color.Beige, Color.Beige, Color.Beige };
public static bool isChanged=false; //設置是否被更改的標志位
public static SoundPlayer sound = new SoundPlayer();
public static Square[,] arriveBlock = new Square[width, height]; //保存已經不能再下落了的方塊
public static int[] arrBitBlock=new int[height]; //位數組:當某個位置有方塊時,該行的該位為1
private const int bitEmpty = 0x0; //0000 0000 0000 0000 0000
private const int bitFull = 0xFFFFF; //1111 1111 1111 1111 1111
/*檢測是否為空*/
public static bool isEmpty(int x, int y)
{
//先檢測是否越界
if (y < 0 || y >= height)
return false;
if (x < 0 || x >= width)
return false;
//然后檢測是否為空
if ((arrBitBlock[y] & (1 << x)) != 0)
return false;
else
return true;
}
/*將方塊停住*/
public static void stopSquare(Square sq, int x, int y)
{
arriveBlock[x, y] = sq;
arrBitBlock[y]=arrBitBlock[y]|(1<<x);
}
/*檢測行是否滿
* 返回:成功消除的行數和 (方便統計分數)
*/
public static int CheckLines()
{
//從最下面一行往上檢測,當某行為空或到頂時結束
int lineFullCount = 0;
int y = height - 1;
while (y >= 0 && arrBitBlock[y] != bitEmpty)
{
if (arrBitBlock[y] == bitFull)
{
lineFullCount++; //消除一行記分
arrBitBlock[y] = bitEmpty;//消除該行的block
PlaySound("FinishOneLine"); //播放聲音
for (int x = 0; x < width; x++) //消除該行的block
arriveBlock[x, y] = null;
//將該行之上的block下移,如果到頂則不執行
for (int i = y; i - 1 >= 0; i--)
{
for (int x = 0; x < width; x++)
{
if ((arrBitBlock[i - 1] & (1 << x)) != 0) //如果上方有block
{
arriveBlock[x, i - 1].location = new Point(arriveBlock[x,i-1].location.X,arriveBlock[x,i-1].location.Y+SquareSize);
arriveBlock[x, i] = arriveBlock[x, i - 1];
}
}
arrBitBlock[i] = arrBitBlock[i - 1];
}
}
else //當消除一行后指針不下移,當沒有消除的時候指針才下移
y--;
}//while循環結束
return lineFullCount;
}
/*播放聲音*/
public static void PlaySound(string soundstr)
{
switch (soundstr)
{
case "FinishOneLine": //消除一行的聲音
if (!File.Exists("FinishOneLine.wav")) return;
sound.SoundLocation = "FinishOneLine.wav";
break;
case "CanNotDo": //當無法操作時
if (!File.Exists("CanNotDo.wav")) return;
sound.SoundLocation = "CanNotDo.wav";
break;
}
sound.Play();
}
/*重畫*/
public static void Redraw()
{
for(int y=height-1;y>=0;y--)
if(arrBitBlock[y]!=bitEmpty)
for(int x=0;x<width;x++)
if ((arrBitBlock[y] & (1 << x)) != 0)
arriveBlock[x, y].Draw(winHandle);
}
//結束
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -