protected override void Update(GameTime gameTime) { timeSinceRotation += gameTime.ElapsedGameTime.TotalMilliseconds; timeElapsed += gameTime.ElapsedGameTime.TotalMilliseconds; timeSinceHardDrop += gameTime.ElapsedGameTime.TotalMilliseconds; timeSinceHold += gameTime.ElapsedGameTime.TotalMilliseconds; timeSincePause += gameTime.ElapsedGameTime.TotalMilliseconds; if(Keyboard.GetState().IsKeyDown(Keys.P) && timeSincePause > 1000) { timeSincePause = 0; //allows pausing if (isRunning) isRunning = false; else isRunning = true; } if (isRunning) { KeyboardState state = Keyboard.GetState(); if (state.IsKeyDown(Keys.Left)) { if (Translate(-1, 0)) activeAnchorX--; } if (state.IsKeyDown(Keys.Right)) { if (Translate(1, 0)) activeAnchorX++; } if (state.IsKeyDown(Keys.Down)) { if (Translate(0, 1)) activeAnchorY++; } if (state.IsKeyDown(Keys.A)) { Rotate(0); } if (state.IsKeyDown(Keys.D)) { Rotate(1); } if(state.IsKeyDown(Keys.H) && timeSinceHold > 1000 && !usedHold) { usedHold = true; timeSinceHold = 0; HoldShape(); } if (state.IsKeyDown(Keys.Space) && timeSinceHardDrop > 1000) { timeSinceHardDrop = 0; for(int i = 0; i < 24; i++) { if (!Translate(0, 1)) break; } } if (timeElapsed > 350) { timeElapsed = 0; if (!Translate(0, 1)) { activeBlocks.Clear(); for (int i = 0; i < 24; i++) { bool rowComplete = true; for (int j = 0; j < 10; j++) { if (!board[j, i].hasBlock) { rowComplete = false; } } if (rowComplete) { //if row is complete, move all rows above it downwards for (int k = i - 1; k >= 0; k--) for (int l = 0; l < 10; l++) { board[l, k + 1] = board[l, k]; board[l, k + 1].y++; } for (int l = 0; l < 10; l++) board[l, 0] = new Square(l, 0); } } //if the block is at the top of the board and can't move down, game over if (activeAnchorY == -1) this.Exit(); //gameOver else { usedHold = false; CreateShape(); } } else activeAnchorY++; } } // Allows the game to exit // TODO: Add your update logic here base.Update(gameTime); }
private void Rotate(int direction) { //if the block is at the side of the board, move it towards the middle; avoids any index errors if(activeAnchorX < 0) { Translate(1, 0); activeAnchorX++; } if(activeAnchorX > 6) { Translate(-1, 0); activeAnchorX--; } if (activeAnchorY >= 0 && activeAnchorY < 21 && timeSinceRotation > 250) { timeSinceRotation = 0; List<Square> nonActive = new List<Square>(); Square[,] rotate = new Square[4, 4]; //take all blocks that are currently active and put them into a rotations array. Take non active blocks and put them into a list for (int i = activeAnchorX; i < activeAnchorX + 4; i++) { for (int j = activeAnchorY; j < activeAnchorY + 4; j++) { if (board[i, j].hasBlock && !activeBlocks.Contains(new Vector2(i, j))) { nonActive.Add(board[i, j]); rotate[i - activeAnchorX, j - activeAnchorY].Update(0); } else rotate[i - activeAnchorX, j - activeAnchorY] = board[i, j]; } } var rotateNew = new Square[4, 4]; for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) rotateNew[i, j] = new Square(i + activeAnchorX, j + activeAnchorY); if (direction == 0)//counterclockwise { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { rotateNew[i, j] = rotate[j, 4 - i - 1]; //transpose array, reverse every column rotateNew[i, j].x = activeAnchorX + i; rotateNew[i, j].y = activeAnchorY + j; } } } if (direction == 1)//clockwise { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { rotateNew[i, j] = rotate[4 - j - 1, i]; //transpose array, reverse every row rotateNew[i, j].x = activeAnchorX + i; rotateNew[i, j].y = activeAnchorY + j; } } } bool isValid = true; //check if any block will prevent rotation foreach (Square s in nonActive) { if (rotateNew[s.x - activeAnchorX, s.y - activeAnchorY].hasBlock) { isValid = false; break; } } //if rotation is valid if (isValid) { activeBlocks.Clear(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { //place the rotated blocks if (rotateNew[i, j].hasBlock) { board[activeAnchorX + i, activeAnchorY + j] = rotateNew[i, j]; activeBlocks.Add(new Vector2(activeAnchorX + i, activeAnchorY + j)); } else if (rotate[i, j].hasBlock) { board[activeAnchorX + i, activeAnchorY + j] = new Square(activeAnchorX + i, activeAnchorY + j); } } } } //place any blocks that were not active back into the board foreach (Square s in nonActive) { board[s.x, s.y] = s; } } }
protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); Square.blocks = Content.Load<Texture2D>("blocks"); blockImages = Content.Load<Texture2D>("blockImages"); for(int i = 0; i < 10; i++) { for(int j = 0; j < 24; j++) { board[i, j] = new Square(i,j); } } //plays Tetris song; highly essential korobeiniki = Content.Load<SoundEffect>("korobeiniki"); SoundEffectInstance instance = korobeiniki.CreateInstance(); instance.IsLooped = true; korobeiniki.Play(); blockQueue.Enqueue(randy.Next(1,7)); blockQueue.Enqueue(randy.Next(1, 7)); blockQueue.Enqueue(randy.Next(1, 7)); CreateShape(); }