protected void SaveTetromino() { // Saves the currentTetromino and spawns a new one. // If there is one already saved, swaps the saved one and the current one. if (savedTetromino == null) { savedTetromino = Tetromino.Copy(currentTetromino); SpawnNewTetromino(); } else { Tetromino temp = Tetromino.Copy(currentTetromino); currentTetromino = Tetromino.Copy(savedTetromino); savedTetromino = Tetromino.Copy(temp); currentTetromino.X = 4; currentTetromino.Y = 0; //currentTetromino.Phase = 0; } }
protected void Rotate(int direction) { int oldX = currentTetromino.X; // Save old X position, in case the simulated Tetromino is overlaping a frozen piece. int newX = currentTetromino.X; // Shifted X position, in case the new orientation is out of bounds. int oldOrientation = currentTetromino.Phase; // Save old orientation, in case the new one is overlaping a frozen piece. int newOrientation = currentTetromino.Phase; // New orientation // Simulated Tetromino, to see if after rotation, the current tetromino is overlaping or out of bounds. Tetromino sim = Tetromino.Copy(currentTetromino); //If direction is 1, it's clockwise, if it's -1 it's counter clock wise, so we do bounds checking. newOrientation = newOrientation + direction; if (newOrientation > currentTetromino.PhaseCount - 1) // -1 because if there are 4 orientations, the last one will have position 3 in the List. { newOrientation = 0; } else if (newOrientation < 0) { newOrientation = currentTetromino.PhaseCount - 1; } //If the oriented piece is out of bounds, push it back. if (newX > w - 1 - currentTetromino.Orientations[newOrientation].Padding) { newX = w - 1 - currentTetromino.Orientations[newOrientation].Padding; } //Assign our simulated Tetromino it's new position and orientation. sim.X = newX; sim.Phase = newOrientation; //Check if the simulated piece overlaps a frozen piece, if it's not overlaping, make the current piece, same as the simulated one. if (CheckForOverlap(sim) == false) { currentTetromino.X = newX; currentTetromino.Phase = newOrientation; } }
protected void RandomNextTetromino() { // "Randomly" generates nextTetromino. Random rng = new Random(); switch (rng.Next(6)) { case 0: { nextTetromino = Tetromino.Copy(sc); break; } case 1: { nextTetromino = Tetromino.Copy(si); break; } case 2: { nextTetromino = Tetromino.Copy(sz); break; } case 3: { nextTetromino = Tetromino.Copy(sn); break; } case 4: { nextTetromino = Tetromino.Copy(st); break; } case 5: { nextTetromino = Tetromino.Copy(sl); break; } case 6: { nextTetromino = Tetromino.Copy(sj); break; } default: { break; } } }
private void UpdateInput(KeyboardState keyState, GameTime gameTime) { if (keyState.IsKeyDown(Keys.Escape)) { gameState = GameState.Menu; Reset(); return; } if (keyState.IsKeyDown(Keys.Left)) { if (!prevKeyState.IsKeyDown(Keys.Left)) { newpiece = false; keyDownTime[Keys.Left] = 0; keyDownTime[Keys.Right] = 0; if (!well.Collision(piece, -1, 0)) { piece.X--; } } else if (!newpiece) { // key was down on last tick as well if (++keyDownTime[Keys.Left] % Math.Max(4, 20 - keyDownTime[Keys.Left] / 2) == 0) { if (!well.Collision(piece, -1, 0)) { piece.X--; } } } } else if (keyState.IsKeyDown(Keys.Right)) { if (!prevKeyState.IsKeyDown(Keys.Right)) { newpiece = false; keyDownTime[Keys.Left] = 0; keyDownTime[Keys.Right] = 0; if (!well.Collision(piece, 1, 0)) { piece.X++; } } else if (!newpiece) { if (++keyDownTime[Keys.Right] % Math.Max(4, 20 - keyDownTime[Keys.Right] / 2) == 0) { if (!well.Collision(piece, 1, 0)) { piece.X++; } } } } // Soft drop else if (keyState.IsKeyDown(Keys.Down)) { if (!prevKeyState.IsKeyDown(Keys.Down)) { newpiece = false; keyDownTime[Keys.Down] = 0; if (!well.Collision(piece, 0, 1)) { piece.Y++; } else { gravityTime = gravityTimer; } } else if (!newpiece) { if (++keyDownTime[Keys.Down] % Math.Max(4, 20 - keyDownTime[Keys.Down] / 2) == 0) { if (!well.Collision(piece, 0, 1)) { piece.Y++; } else { gravityTime = gravityTimer; } } } } if (keyState.IsKeyDown(Keys.Z) && !prevKeyState.IsKeyDown(Keys.Z)) { Tetromino rotated = piece.Copy(); rotated.Tiles = piece.Rotate(); // attempt rotation as-is if (!well.Collision(rotated)) { piece.Tiles = rotated.Tiles; } // attempt wall-kick-right before rotation else if (!well.Collision(rotated, 1, 0)) { piece.X++; piece.Tiles = rotated.Tiles; } // attempt wall-kick-left before rotation else if (!well.Collision(rotated, -1, 0)) { piece.X--; piece.Tiles = rotated.Tiles; } } // Hard drop if (keyState.IsKeyDown(Keys.X) && !prevKeyState.IsKeyDown(Keys.X)) { while (!well.Collision(piece)) { piece.Y++; } piece.Y--; gravityTime = gravityTimer; } }
protected void SpawnNewTetromino() { // Makes currentTetromino to be the Next one and generates a new nextTetromino. currentTetromino = Tetromino.Copy(nextTetromino); RandomNextTetromino(); }