using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework; namespace TetrisPrac { class TetrisBlocks { Color color; Texture2D block; const int size = 4; bool[,] configuration; int i, j; public void randomBlock(int a, int b, int c, int d, int e, int f, int g, int h) { this.configuration = new bool[size, size]; for (i = 0; i < size; i++) for (j = 0; j < size; j++) this.configuration[i, j] = false; configuration[a, b] = true; configuration[c, d] = true; //coordinates of occupied cells in array configuration[e, f] = true; configuration[g, h] = true; //" } public void OBlock() //O-shaped Tetrisblock { this.color = Color.Blue; randomBlock(1, 1, 1, 2, 2, 1, 2, 2); } public void TBlock() //T-shaped Tetrisblock { this.color = Color.Orange; randomBlock(1, 2, 2, 1, 2, 2, 2, 3); } public void IBlock() //I-shaped Tetrisblock { this.color = Color.Red; randomBlock(0, 1, 1, 1, 2, 1, 3, 1); } public void LBlock() //L-shaped Tetrisblock { this.color = Color.Green; randomBlock(0, 1, 1, 1, 2, 1, 2, 2); } public void JBlock() //J-shaped Tetrisblock { this.color = Color.Green; randomBlock(0, 2, 1, 2, 2, 2, 2, 1); } public void ZBlock() //Z-shaped Tetrisblock { this.color = Color.Pink; randomBlock(1, 1, 1, 2, 2, 2, 2, 3); } public void SBlock() //S-shaped Tetrisblock { this.color = Color.Pink; randomBlock(1, 2, 1, 3, 2, 1, 2, 2); } public void RotateLeft() { bool[,] rotatedL = new bool[size, size]; for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) rotatedL[size - j - 1, i] = configuration[i, j]; this.configuration = rotatedL; } public void RotateRight() { bool[,] rotatedR = new bool[size, size]; for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) rotatedR[j, size - i - 1] = configuration[i, j]; this.configuration = rotatedR; } public void createBlock() //creates new tetrisblock { //? //hier moeten we nieuwe blokken aanmaken wanneer we een nieuw stukje spawnen. } public void drawBlock(Vector2 blockPosition) //draws tetrisblock { spriteBatch.Begin(); for (i = 0; i < size; i++) for (j = 0; j < size; j++) if (configuration[i, j] == true) spriteBatch.Draw(block, blockPosition, this.Color); //actual tetrisblock else spriteBatch.Draw(block, blockPosition, Color.White); //rest of array (= transparent) spriteBatch.End(); } public void collidingBlock() //attaches tetrisblock to tetrisgrid when colliding { for (i = 0; i < size; i++) for (j = 0; j < size; j++) if (isColliding) { TetrisGrid.Occupied[y + i, x + j] = true; //waar wijzen x en y naar? ze zijn nergens gedefinieerd drawBlock(TetrisGrid.GridPosition[y + i, x + j]); } } public void fallingBlock(GameTime gameTime) //makes tetrisblock drop one spot per second, if not colliding { TimeSpan timeSpan = TimeSpan.FromSeconds(1); //sets falling timer for tetrisblock timeSpan -= gameTime.ElapsedGameTime; if (timeSpan == TimeSpan.Zero) { GraphicsDevice.Clear(TetrisGrid.GridPosition[y + i, x + j]); drawBlock(TetrisGrid.GridPosition[y + i - 1, x + j]); timeSpan = TimeSpan.FromSeconds(1); } else drawBlock(TetrisGrid.gridPosition[y + i, x + j]); } public bool isColliding() //detects if tetrisblock is colliding with other block(s) or field bottom { for (i = 0; i < size; i++) for (j = 0; j < size; j++) if (randomBlock[i, j] == true && (randomBlock[i + 1, j] == false || i == size - 1)) //if cell is potential collision point if (TetrisGrid.Occupied[y + i + 1, x + j] == true) //if cell, directly below potential collision point, is occupied return true; return false; } } }