CHƯƠNG II: XÂY DỰNG GAME XẾP GẠCH SỬ DỤNG THƯ VIỆN
2.2.3. Xây dựng lớp GameSize
Các phương thức, thuộc tính được sử dụng trong lớp:
phương thức, thuộc tính Ý nghĩa
NodoID [] Lưu danh sách các nút đang hoạt động
trong phòng.
Boll Kill Xác nhận hủy một hàng trên trục X, Y, XOZ.
ResetTheRoom() Thiết lập lại căn phòng.
ResetNodes() Thiết lập lại các ô vuông trong căn phòng.
ResetAlphaColor() Thiết lập lại độ mờ cho các đối tượng.
ResetObjectColor() Thiết lập lại màu vật thể.
ResetLightPoint() Thiết lập lại tọa độ điểm sáng.
CreateNewObject(uint type) Kiểm tra vùng trong để tạo đối tượng.
CheckEnd() Kiểm tra trò chơi đã kết thúc hay chưa.
EraseX(int y, int z) Hủy một hàng trên trục X.
EraseZ(int y, int x) Hủy một hàng trên trục Z.
EraseXOZ(int y) Hủy một hàng trên trục XOZ.
EnableLighting() Thiết lập lại đèn chiếu.
ObjectFall() Xử lý trường hợp các đối tượng rơi xuống.
MoveRight() Xử lý trường hợp vật thể di chuyển qua trái.
MoveLeft() Xử lý trường hợp vật thể di chuyển
qua phải.
MoveFront() Xử lý trường hợp vật thể di chuyển ra trước.
MoveBack() Xử lý trường hợp vật thể di chuyển ra sau.
RotateX() Xoay vật thể theo trục X.
RotateY() Xoay vật thể theo trục Y.
RotateZ() Xoay vật thể theo trục Z.
public frmMain Parent;
public int X, Y, Z;
private MouseEventArgs preMouse = new MouseEventArgs(MouseButtons.Left, 0, 0, 0, 0);
public float dX = 0.0f, dY = 0.0f;
public NodeState[, ,] Room;
public CCube[, ,] Nodes;
public NodeID[] ctrlNodes;
public uint CurrObject;
public bool OnMoveObj = false;
public bool EndGame = false;
public int Score = 0;
public bool OnErase = false;
public bool EnableTexture = false;
public float AlphaColor = 0.5f;
public float[] ObjectColor = new float[3] { 0.5f, 0.5f, 0.5f };
public bool KillX = true, KillZ = false, KillXOZ = false;
public OpenGLTexture2D texObject = new OpenGLTexture2D("..//..//bmpObject.bmp");
public OpenGLTexture2D texFloor = new OpenGLTexture2D("..//..//bmpFloor.bmp");
public OpenGLTexture2D texOut = new OpenGLTexture2D("..//..//bmpButton.bmp");
public float[] light_position = new float[4] { 0.0f, 1.0f, 0.0f, 1.0f };
// ****************** CONSTRUCTOR ************************
public GameSize() : base()
{
X = 15; Z = 10; Y = 20;
Room = new NodeState[X + 2, Y + 2, Z + 2];
ResetTheRoom();
Nodes = new CCube[X + 2, Y + 2, Z + 2];
ResetNodes();
ResetAlphaColor();
}
Ngoài ra, còn định nghĩa một enum và struct NodeID // Tap hop cac trang thai cua nut.
public enum NodeState { Empty, Full, Out } // Luu chi so cua mot nut.
public struct NodeID {
public int X, Y, Z;
public void SetID(int x, int y, int z) {
X = x; Y = y; Z = z;
} } NodeState với:
- Empty: Nút đó chưa được vẽ hay chính xác là không gian tại đó không có hình khối nào.
- Full: Có chứa hình khối.
- Out: Vượt khỏi biên của không gian trò chơi được tạo ra.
NodeID dùng để lưu chỉ số của một nút.
// ****************** VARIABLE ***************************
public frmMain Parent;
public int X, Y, Z;
private MouseEventArgs preMouse = new
MouseEventArgs(MouseButtons.Left, 0, 0, 0, 0);
private float dX = 0.0f, dY = 0.0f;
public NodeState[, ,] Room;
public CCube[, ,] Nodes;
// Luu danh sach cac nut dang hoat dong trong phong.
public NodeID[] ctrlNodes;
public uint CurrObject;
public bool OnMoveObj = false;
public bool EndGame = false;
public int Score = 0;
public bool OnErase = false;
public bool EnableTexture = false;
public float AlphaColor = 0.5f;
public float[] ObjectColor = new float[3] { 0.5f, 0.5f, 0.5f };
// Xac nhan: huy tren truc, X, Z, mat XOZ.
public bool KillX = true, KillZ = false, KillXOZ = false;
public OpenGLTexture2D texObject = new OpenGLTexture2D("..//..//bmpObject.bmp");
public OpenGLTexture2D texFloor = new OpenGLTexture2D("..//..//bmpFloor.bmp");
public OpenGLTexture2D texOut = new OpenGLTexture2D("..//..//bmpButton.bmp");
public float[] light_position = new float[4] { 0.0f, 1.0f, 0.0f, 1.0f };
// ****************** CONSTRUCTOR ************************
public GameSize()
// ****************** HANDLE EVENT ***********************
protected override void InitGLContext()
protected override void OnSizeChanged(EventArgs e) public override void glDraw()
protected override void OnKeyDown(KeyEventArgs e)
// ****************** ADDING FUNCTION ********************
// Thiet lap lai can phong.
public void ResetTheRoom() {
for (int ix = 0; ix < X + 2; ix++) for (int iy = 0; iy < Y + 2; iy++) for (int iz = 0; iz < Z + 2; iz++)
if (ix == 0 || ix == X + 1 || iy == 0 || iy == Y + 1 || iz == 0 ||
iz == Z + 1)
Room[ix, iy, iz] = NodeState.Out;
else Room[ix, iy, iz] = NodeState.Empty;
}
// Thiet lap lai do mo cho cac doi tuong.
public void ResetAlphaColor() {
for (int ix = 1; ix < X + 1; ix++) for (int iy = 1; iy < Y + 1; iy++) for (int iz = 1; iz < Z + 1; iz++) {
Nodes[ix, iy, iz].AlphaColor = AlphaColor;
} }
// Thiet lap lai mau vat the.
public void ResetObjectColor()
{
Nodes[ix, iy, iz].color = ObjectColor;
} }
// Thiet lap lai toa do diem sang.
public void ResetLightPoint()
{
GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_position);
}
// Kiem tra vung trong de tao doi tuong.
public void CreateNewObject(uint type) // Xu ly truong hop di chuyen qua trai.
public void MoveRight()
// Xu ly truong hop di chuyen qua phai.
public void MoveLeft()
// Xu ly truong hop di chuyen toi truoc.
public void MoveFront()
// Xu ly truong hop di chuyen ra sau.
public void MoveBack()
// xu ly truong hop xoay theo truc Y.
public void RotateY()
// Xu ly truong hop xoay theo truc Z.
public void RotateZ()
// Kiem tra tro choi co ket thuc chua.
public bool CheckEnd() {
for (int ix = 1; ix < X + 1; ix++) for (int iz = 1; iz < Z + 1; iz++) {
if (Room[ix, Y, iz] == NodeState.Full) {
Parent.Message(4);
//* tu dong luu diem cao nhat.
Parent.PlayerList[10] = Score.ToString() + " "
+ Parent.CurrPlayer;
Array.Sort(Parent.PlayerList);
Array.Reverse(Parent.PlayerList);
Parent.SaveHightScore();
EndGame = true;
return true;
} }
return false;
}
// Kiem tra de huy & tinh diem.
public void CheckErase() {
bool result;
if (KillX) {
for (int iy = 1; iy < Y + 1; iy++)
if (Room[ix, iy, iz] != NodeState.Full) result = false;
if (result) { OnErase = true; EraseX(iy, iz); iy--; OnErase = false; }
} }
if (KillZ) {
for (int iy = 1; iy < Y + 1; iy++) for (int ix = 1; ix < X + 1; ix++) {
result = true;
for (int iz = 1; iz < Z + 1; iz++)
if (Room[ix, iy, iz] != NodeState.Full) result = false;
if (result) { OnErase = true; EraseZ(iy, ix); iy--; OnErase = false; }
} }
if (KillXOZ) {
for (int iy = 1; iy < Y + 1; iy++) {
result = true;
for (int iz = 1; iz < Z + 1; iz++) for (int ix = 1; ix < X + 1; ix++)
if (Room[ix, iy, iz] != NodeState.Full) result = false;
if (result) { OnErase = true; EraseXOZ(iy); iy--; OnErase = false; }
} } }
// Huy mot hang tren truc Ox.
public void EraseX(int y, int z) {
float alphacolor = 1.0f;
// Tao hieu ung mo dan cho hang.
while (alphacolor > 0.0f) {
alphacolor -= 0.1f;
for (int ix = 1; ix < X + 1; ix++) {
Nodes[ix, y, z].AlphaColor = alphacolor;
}
Refresh();
}
// Thiet lap lai mau sac cho hang.
for (int ix = 1; ix < X + 1; ix++) {
Nodes[ix, y, z].AlphaColor = AlphaColor;
}
// Xoa hang.
for (int iy = y; iy < Y; iy++)
// Cap nhat lai diem so.
Score += Parent.trkSpeed.Value * 5;
}
// Huy mot hang tren truc Ox.
public void EraseX(int y, int z) {
float alphacolor = 1.0f;
// Tao hieu ung mo dan cho hang.
while (alphacolor > 0.0f) {
alphacolor -= 0.1f;
for (int ix = 1; ix < X + 1; ix++) {
Nodes[ix, y, z].AlphaColor = alphacolor;
}
Refresh();
}
// Thiet lap lai mau sac cho hang.
for (int ix = 1; ix < X + 1; ix++) {
Nodes[ix, y, z].AlphaColor = AlphaColor;
}
// Xoa hang.
for (int iy = y; iy < Y; iy++) {
for (int ix = 1; ix < X + 1; ix++)
Room[ix, iy, z] = Room[ix, iy + 1, z];
}
// Cap nhat lai diem so.
Score += Parent.trkSpeed.Value * 5;
}
// Huy mot mat tren truc XOZ.
public void EraseXOZ(int y) {
float alphacolor = 1.0f;
// Tao hieu ung mo dan cho hang.
while (alphacolor > 0.0f) {
alphacolor -= 0.1f;
for (int iz = 1; iz < Z + 1; iz++) for (int ix = 1; ix < X + 1; ix++)
Nodes[ix, y, iz].AlphaColor = alphacolor;
Refresh();
}
for (int iz = 1; iz < Z + 1; iz++) for (int ix = 1; ix < X + 1; ix++)
Nodes[ix, y, iz].AlphaColor = alphacolor;
// Xoa hang.
for (int iy = y; iy < Y; iy++) {
// Cap nhat lai diem so.
Score += ((frmMain)Parent).trkSpeed.Value * 5;
}
// huy mot hang tren truc oz public void EraseZ(int y, int x) {
float alphacolor = 1.0f;
// Tao hieu ung mo dan cho hang.
while (alphacolor > 0.0f) {
alphacolor -= 0.1f;
for (int iz = 1; iz < Z + 1; iz++) {
Nodes[x, y, iz].AlphaColor = alphacolor;
}
Refresh();
}
// Thiet lap lai mau sac cho hang.
for (int iz = 1; iz < Z + 1; iz++) {
Nodes[x, y, iz].AlphaColor = AlphaColor;
}
// Xoa hang.
for (int iy = y; iy < Y; iy++) {
for (int iz = 1; iz < Z + 1; iz++)
Room[x, iy, iz] = Room[x, iy + 1, iz];
}
// Cap nhat lai diem so.
Score += ((frmMain)Parent).trkSpeed.Value * 5;
}
// Thiet lap den chieu.
public void EnableLighting() {
GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_position);
GL.glEnable(GL.GL_LIGHTING);
GL.glEnable(GL.GL_LIGHT0);
}
// Tat den chieu.
public void DisableLighting() {
GL.glDisable(GL.GL_LIGHT0);
GL.glDisable(GL.GL_LIGHTING);
}
// Thiet lap blend.
public void EnableBlending()
{
GL.glEnable(GL.GL_BLEND);
GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
}
// Ve toan bo man hinh public void DrawScreen()
for (int iz = 0; iz < Z + 2; iz++) {
GL.glPushMatrix();
if (ix == 0 || ix == X + 1 || iz == 0 || iz == Z + 1) Nodes[ix, 0, iz].DrawTexture(texOut);
else
Nodes[ix, 0, iz].DrawTexture(texFloor);
GL.glPopMatrix();
}
// Ve diem sinh ra cac doi tuong.
GL.glPushMatrix();
Nodes[1, Y + 1, 1].DrawTexture(texOut);
GL.glPopMatrix();
// Ve cac doi tuong...
for (int iy = 1; iy < Y + 1; iy++) for (int ix = 1; ix < X + 1; ix++) for (int iz = 1; iz < Z + 1; iz++) {
if (Room[ix, iy, iz] == NodeState.Full) {
GL.glPushMatrix();
Nodes[ix, iy, iz].DrawTexture(texObject);
GL.glPopMatrix();