private void ApplyGravity(GameObject[,] go, Texture2D[] boxColors) { //x axis is [1], y axis is [0] //i.e. go[y,x] for actual game objects for (int y = go.GetLength(1) - 2; y >= 0; y--) { for (int x = go.GetLength(0) - 1; x >= 0; x--) { if (go[y + 1, x].Texture == boxColors[0]) //if the box below is black { go[y + 1, x].Texture = go[y, x].Texture; go[y, x].Texture = boxColors[0]; } } } }
private void SetGameFieldPositions() { for (int row = 0; row < boxes.GetLength(0); row++) { for (int column = 0; column < boxes.GetLength(1); column++) { Vector2 v = new Vector2((float)((GraphicsDevice.Viewport.Width / (gameFieldDimensions)) * (column)), (GraphicsDevice.Viewport.Height / (gameFieldDimensions + 1)) * (row + 1)); boxes[row, column] = new GameObject(boxColors[0], v); } } }
private void SetTopRowPositions() { for (int i = 0; i < topBoxes.Length; i++) //positioning topBoxes in top row { Vector2 v = new Vector2((float)((GraphicsDevice.Viewport.Width / gameFieldDimensions) * i), 0); topBoxes[i] = new GameObject(boxColors[0], v); } }
private void RotateClockWise(GameObject[,] go, Texture2D[] boxColors) { int n = go.GetLength(0); int m = go.GetLength(1); Texture2D[,] matrix = new Texture2D[n, m]; for (int i = 0; i < go.GetLength(0); i++) { for (int j = 0; j < go.GetLength(1); j++) { matrix[i,j] = go[i, j].Texture; } } Texture2D[,] temp1 = new Texture2D[n, m]; Texture2D[,] temp2 = new Texture2D[n, m]; Texture2D[,] temp3 = new Texture2D[n, m]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { temp1[i, j] = matrix[n - j - 1, i]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { temp2[i, j] = temp1[n - j - 1, i]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { temp3[i, j] = temp2[n - j - 1, i]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { go[i, j].Texture = temp3[i, j]; } } }
private void getRandomColor(GameObject go) { Random r = new Random(); go.Texture = boxColors[(int)r.Next(1,4)]; //gets random non-black color }
private void DropBoxFromTop(GameObject topBox, GameObject[,] boxes) { if (boxes[0, topRowPointer].Texture == boxColors[0]) //if the top space is empty, enter the main method { if (boxes[0, topRowPointer].Texture == boxColors[0] && boxes[1,topRowPointer].Texture != boxColors[0]) { boxes[0, topRowPointer].Texture = topBoxes[topRowPointer].Texture; } else { for (int row = 0; row < boxes.GetLength(1) - 1; row++) { if (boxes[row + 1, topRowPointer].Texture == boxColors[0]) //if the space below is empty { boxes[row, topRowPointer].Texture = boxColors[0]; boxes[row + 1, topRowPointer].Texture = topBoxes[topRowPointer].Texture; } else { topBoxes[topRowPointer].Texture = boxColors[0]; getRandomColor(topBoxes[topRowPointer]); } } } } else { } //do nothing; the box shouldn't be dropped }
private void CheckForMatches(GameObject[,] box, Texture2D[] boxColors) { for (int i = 0; i < box.GetLength(0); i++) { for (int j = 0; j < box.GetLength(1); j++) { try { if (box[i, j].Texture == box[i - 1, j].Texture && box[i, j].Texture== box[i - 2, j].Texture) //if the three boxes have the same color - 1 { box[i, j].Texture = boxColors[0]; box[i - 1, j].Texture = boxColors[0]; box[i - 2, j].Texture = boxColors[0]; } } catch { } try { if (box[i, j].Texture == box[i - 1, j].Texture && box[i, j].Texture == box[i + 1, j].Texture) //if the three boxes have the same color - 2 { box[i, j].Texture = boxColors[0]; box[i - 1, j].Texture = boxColors[0]; box[i + 1, j].Texture = boxColors[0]; } } catch { } try { if (box[i, j].Texture == box[i + 1, j].Texture && box[i, j].Texture == box[i + 2, j].Texture) //if the three boxes have the same color - 3 { box[i, j].Texture = boxColors[0]; box[i + 1, j].Texture = boxColors[0]; box[i + 2, j].Texture = boxColors[0]; } } catch { } try { if (box[i, j].Texture == box[i, j - 1].Texture && box[i, j].Texture == box[i, j - 2].Texture) //if the three boxes have the same color - 4 { box[i, j].Texture = boxColors[0]; box[i, j - 1].Texture = boxColors[0]; box[i, j - 2].Texture = boxColors[0]; } } catch { } try { if (box[i, j].Texture == box[i , j - 1].Texture && box[i, j].Texture == box[i, j + 1].Texture) //if the three boxes have the same color - 5 { box[i, j].Texture = boxColors[0]; box[i, j - 1].Texture = boxColors[0]; box[i, j + 1].Texture = boxColors[0]; } } catch { } try { if (box[i, j].Texture == box[i , j + 1].Texture && box[i, j].Texture == box[i, j + 2].Texture) //if the three boxes have the same color - 6 { box[i, j].Texture = boxColors[0]; box[i, j + 1].Texture = boxColors[0]; box[i, j + 2].Texture = boxColors[0]; } } catch { } } } }
private void boxPositionChange(KeyboardState newState, Keys[] myKeys, Keys dropButton, GameObject[] topRow, GameObject[,] gameField) { //myKeys: up down left right if (newState.IsKeyDown(dropButton)) { //drop the block DropBoxFromTop(topRow[topRowPointer], boxes); //check for matches } if (newState.IsKeyDown(myKeys[0])) { //reflect CCW RotateClockWise(boxes, boxColors); RotateClockWise(boxes, boxColors); RotateClockWise(boxes, boxColors); //gravity happens } if (newState.IsKeyDown(myKeys[1])) { //rotate clockwise RotateClockWise(boxes, boxColors); } if (newState.IsKeyDown(myKeys[2])) //if left is pressed { try { topRow[topRowPointer - 1].Texture = topRow[topRowPointer].Texture; topRow[topRowPointer].Texture = boxColors[0]; topRowPointer--; } catch { } } if (newState.IsKeyDown(myKeys[3])) //if right is pressed { try { topRow[topRowPointer + 1].Texture = topRow[topRowPointer].Texture; topRow[topRowPointer].Texture = boxColors[0]; topRowPointer++; } catch { } } }