public void Draw(SpriteBatch spriteBatch) { //Draw the game board spriteBatch.Draw(LoadGraphics.BoardGraphic, Vector2.Zero, Color.White); //Draw the active piece if (ActivePiece != null) { ActivePiece.Draw(spriteBatch); } if (NextPiece != null) { NextPiece.DrawNext(spriteBatch); } //Draw all the pieces on the board for (int x = 0; x < Lines.Length; x++) { for (int y = 0; y < Lines[x].Length; y++) { if (Lines[x][y] != null) { spriteBatch.Draw(Lines[x][y], GetBlockRect(x, y), Color.White); } } } //Draw the score Vector2 scoresize = LoadGraphics.TetrisFont.MeasureString(Score.ToString()); Vector2 levelsize = LoadGraphics.TetrisFont.MeasureString(LevelNum.ToString()); Vector2 linesize = LoadGraphics.TetrisFont.MeasureString(TotalLinesCleared.ToString()); spriteBatch.DrawString(LoadGraphics.TetrisFont, Score.ToString(), new Vector2((int)((BoardSize.X * 1.66f) - (scoresize.X / 2)), 40), Color.Black, 0f, Vector2.Zero, .85f, SpriteEffects.None, 1f); spriteBatch.DrawString(LoadGraphics.TetrisFont, LevelNum.ToString(), new Vector2((int)((BoardSize.X * 1.66f) - (levelsize.X / 2)), 105), Color.Black, 0f, Vector2.Zero, .8f, SpriteEffects.None, 1f); spriteBatch.DrawString(LoadGraphics.TetrisFont, TotalLinesCleared.ToString(), new Vector2((int)((BoardSize.X * 1.66f) - (linesize.X / 2)), 153), Color.Black, 0f, Vector2.Zero, .8f, SpriteEffects.None, 1f); //Say "Game Over" if it's game over if (Main.GameOver == true && GameOverLine < 0) { Vector2 stringsize = LoadGraphics.TetrisFont.MeasureString("Game Over"); spriteBatch.DrawString(LoadGraphics.TetrisFont, "Game Over", new Vector2((int)((BoardSize.X / 2) - (stringsize.X / 4)), (int)((BoardSize.Y / 2) - (stringsize.Y / 2))), Color.White); } }
static void Main() { int level = 0; int linesCount = 0; int score = 0; int playfieldWidth = 16; int playfieldHeigth = 30; field = new byte[playfieldHeigth, playfieldWidth, 2]; RemoveScrollBars(playfieldWidth * 2, playfieldHeigth); //Remove the scroll bars Console.Title = "Tetris"; Piece currentPiece = new Piece(playfieldWidth / 2 - 2); //Create the first piece while (true) { Console.Clear(); //Clear the field DrawField(); //Redraw the field; currentPiece.Draw(); //Draw the piece while (Console.KeyAvailable) //Move if possible the piece left/right or rotate if a proper key is pressed { ConsoleKeyInfo pressedKey = Console.ReadKey(true); currentPiece.Clean(); currentPiece.Move(pressedKey); currentPiece.Draw(); }; currentPiece.Move(); //Check if possible and move down or stop moving if (currentPiece.isFinal) //If stop moving check lines and score { score += 4; for ( int i = 0; i < field.GetLength(0); i++) { int line = 1; for (int j = 1; j < field.GetLength(1) - 1; j++) { line *= field[i, j, 0]; } if (line != 0) { linesCount++; level = level < 5 ? linesCount / 20 : 5; score += 40; for (int c = 0; c < field.GetLength(1); c++) { for (int r = i; r > 0; r--) { field[r, c, 0] = field[r - 1, c, 0]; field[r, c, 1] = field[r - 1, c, 1]; } field[0, c, 0] = 0; field[0, c, 1] = 0; } } } currentPiece = new Piece(playfieldWidth / 2 - 2); } DrawInfo(level, score, linesCount); //Draw info if (GameOver()) //Check if game over { DrawStringOnPosition(field.GetLength(1) - 6, field.GetLength(0) / 2, "!!!GAME OVER!!!", ConsoleColor.Red); Console.WriteLine(); break; } Console.Beep(); //Play sound Thread.Sleep(600 - 100 * level); //Slow the program } }
static void Main() { int level = 0; int linesCount = 0; int score = 0; int playfieldWidth = 16; int playfieldHeigth = 30; field = new byte[playfieldHeigth, playfieldWidth, 2]; RemoveScrollBars(playfieldWidth * 2, playfieldHeigth); //Remove the scroll bars Console.Title = "Tetris"; Piece currentPiece = new Piece(playfieldWidth / 2 - 2); //Create the first piece while (true) { Console.Clear(); //Clear the field DrawField(); //Redraw the field; currentPiece.Draw(); //Draw the piece while (Console.KeyAvailable) //Move if possible the piece left/right or rotate if a proper key is pressed { ConsoleKeyInfo pressedKey = Console.ReadKey(true); currentPiece.Clean(); currentPiece.Move(pressedKey); currentPiece.Draw(); } ; currentPiece.Move(); //Check if possible and move down or stop moving if (currentPiece.isFinal) //If stop moving check lines and score { score += 4; for (int i = 0; i < field.GetLength(0); i++) { int line = 1; for (int j = 1; j < field.GetLength(1) - 1; j++) { line *= field[i, j, 0]; } if (line != 0) { linesCount++; level = level < 5 ? linesCount / 20 : 5; score += 40; for (int c = 0; c < field.GetLength(1); c++) { for (int r = i; r > 0; r--) { field[r, c, 0] = field[r - 1, c, 0]; field[r, c, 1] = field[r - 1, c, 1]; } field[0, c, 0] = 0; field[0, c, 1] = 0; } } } currentPiece = new Piece(playfieldWidth / 2 - 2); } DrawInfo(level, score, linesCount); //Draw info if (GameOver()) //Check if game over { DrawStringOnPosition(field.GetLength(1) - 6, field.GetLength(0) / 2, "!!!GAME OVER!!!", ConsoleColor.Red); Console.WriteLine(); break; } Console.Beep(); //Play sound Thread.Sleep(600 - 100 * level); //Slow the program } }