Exemple #1
0
        // (C)
        /// <summary>
        /// Positionne la pièce de jeu au centre de la surface de jeu et en haut de celle-ci.
        /// </summary>
        ///
        /// <param name="_type">
        /// Le type de bloc qui sera placé.
        /// </param>
        public Piece(PieceType _type = PieceType.Dead)
        {
            // Debug
            if (_type == PieceType.Dead)
            {
                // Chooses a random piece.
                type = (PieceType)Enum.GetValues(typeof(PieceType)).GetValue(RandomTetris.rnd.Next(7));
            }
            else
            {
                type = _type;
            }

            pieceArray = StaticVars.GetPieceArray(type);

            // Centers the piece and place it at the top of the grid.
            if (type != PieceType.O)
            {
                position = new Vector2i(5 - (int)Math.Ceiling((decimal)pieceArray.GetLength(1) / 2), 0);
            }
            else
            {
                position = new Vector2i(5 - (int)Math.Ceiling((decimal)pieceArray.GetLength(1) / 2), -1);
            }
        }
Exemple #2
0
        /// <summary>
        /// Draw code of the program
        /// </summary>
        private void Draw()
        {
            window.Clear();

            //TODO: make this change the textures of the sprites that has moved
            if (gameState == GameState.Playing || gameState == GameState.Pause)
            {
                //Draw the background first, so it's in the back
                window.Draw(StaticVars.backDrop);
                window.Draw(StaticVars.holdSprite);
                window.Draw(StaticVars.queueSprite);
                window.Draw(StaticVars.drawGridSprite);

                #region Draw main grid

                //Get a temp grid to stop calling a function in a class, more FPS
                PieceType[,] drawArray = grid.GetDrawable();

                //Loop through the drawable grid elements
                for (int i = 0; i < drawArray.GetLength(0); i++)
                {
                    for (int j = 0; j < drawArray.GetLength(1); j++)
                    {
                        //Slower code?

                        /*
                         * switch (drawArray[i, j])
                         * {
                         *
                         *      case PieceType.Dead:
                         *              //drawGrid[i, j].Texture = Color.White;
                         *              break;
                         *
                         *      default:
                         *              drawGrid[i, j].Texture = blockTextures[(int)drawArray[i, j]];
                         *              break;
                         *
                         * }
                         */

                        //Update the textures except dead pieces, since we want them to keep their original colors
                        if (drawArray[i, j] != PieceType.Dead)
                        {
                            //Associated the blockTextures with the same indexes as the Enum
                            //Look at both and you will understand
                            StaticVars.drawGrid[i, j].Texture = StaticVars.blockTextures[(int)drawArray[i, j]];
                        }

                        //Finally draw to the screen the final results
                        window.Draw(StaticVars.drawGrid[i, j]);
                    }
                }

                #endregion

                //Draw the queue

                #region Queue

                List <PieceType> queueArray = pieceQueue.GetList();

                for (int i = 0; i < queueArray.Count; i++)
                {
                    PieceType[,] queuePieceToDraw = StaticVars.GetPieceArray(queueArray[i]);

                    if (queueArray[i] == PieceType.I)
                    {
                        for (int j = 0; j < queueSpriteArray1x4[0].GetLength(0); j++)
                        {
                            for (int k = 0; k < queuePieceToDraw.GetLength(1); k++)
                            {
                                if (queuePieceToDraw[j + 1, k] != PieceType.Empty)
                                {
                                    queueSpriteArray1x4[i][j, k].Texture = StaticVars.blockTextures[(int)queuePieceToDraw[j + 1, k]];

                                    window.Draw(queueSpriteArray1x4[i][j, k]);
                                }
                            }
                        }
                    }
                    else if (queuePieceToDraw.GetLength(1) == 4)
                    {
                        for (int j = 0; j < queuePieceToDraw.GetLength(0); j++)
                        {
                            for (int k = 0; k < queuePieceToDraw.GetLength(1); k++)
                            {
                                if (queuePieceToDraw[j, k] != PieceType.Empty)
                                {
                                    queueSpriteArray4x4[i][j, k].Texture = StaticVars.blockTextures[(int)queuePieceToDraw[j, k]];

                                    window.Draw(queueSpriteArray4x4[i][j, k]);
                                }
                            }
                        }
                    }
                    else
                    {
                        for (int j = 0; j < queuePieceToDraw.GetLength(0); j++)
                        {
                            for (int k = 0; k < queuePieceToDraw.GetLength(1); k++)
                            {
                                if (queuePieceToDraw[j, k] != PieceType.Empty)
                                {
                                    queueSpriteArray3x3[i][j, k].Texture = StaticVars.blockTextures[(int)queuePieceToDraw[j, k]];
                                    window.Draw(queueSpriteArray3x3[i][j, k]);
                                }
                            }
                        }
                    }
                }

                #endregion

                #region Hold

                PieceType[,] pieceToDraw = StaticVars.GetPieceArray(holdManager.currentPiece);

                if (holdManager.currentPiece == PieceType.I)
                {
                    for (int j = 0; j < holdSprite1x4.GetLength(0); j++)
                    {
                        for (int k = 0; k < pieceToDraw.GetLength(1); k++)
                        {
                            if (pieceToDraw[j + 1, k] != PieceType.Empty)
                            {
                                holdSprite1x4[j, k].Texture = StaticVars.blockTextures[(int)pieceToDraw[j + 1, k]];

                                window.Draw(holdSprite1x4[j, k]);
                            }
                        }
                    }
                }
                else if (pieceToDraw.GetLength(1) == 4)
                {
                    for (int j = 0; j < pieceToDraw.GetLength(0); j++)
                    {
                        for (int k = 0; k < pieceToDraw.GetLength(1); k++)
                        {
                            if (pieceToDraw[j, k] != PieceType.Empty)
                            {
                                holdSprite4x4[j, k].Texture = StaticVars.blockTextures[(int)pieceToDraw[j, k]];

                                window.Draw(holdSprite4x4[j, k]);
                            }
                        }
                    }
                }
                else
                {
                    for (int j = 0; j < pieceToDraw.GetLength(0); j++)
                    {
                        for (int k = 0; k < pieceToDraw.GetLength(1); k++)
                        {
                            if (pieceToDraw[j, k] != PieceType.Empty)
                            {
                                holdSprite3x3[j, k].Texture = StaticVars.blockTextures[(int)pieceToDraw[j, k]];
                                window.Draw(holdSprite3x3[j, k]);
                            }
                        }
                    }
                }

                #endregion

                window.Draw(score);

                window.Draw(level);

                window.Draw(realTime);

                window.Draw(StaticVars.statsSprite);

                window.Draw(StaticVars.controlsText);

                foreach (var item in stats.GetDrawable())
                {
                    window.Draw(item);
                }

                if (gameState == GameState.Pause)
                {
                    window.Draw(StaticVars.pauseText);
                }
            }
            else if (gameState == GameState.End)
            {
                //Draw the background first, so it's in the back
                window.Draw(StaticVars.backDrop);
                window.Draw(StaticVars.holdSprite);
                window.Draw(StaticVars.queueSprite);
                window.Draw(StaticVars.drawGridSprite);
                window.Draw(endText);
                window.Draw(score);
                window.Draw(level);
                window.Draw(realTime);
                window.Draw(StaticVars.statsSprite);
                window.Draw(StaticVars.controlsText);

                foreach (var item in stats.GetDrawable())
                {
                    window.Draw(item);
                }
            }
            else if (gameState == GameState.Menu)
            {
                //window.Draw(StaticVars.menuBackdrop);

                foreach (var item in menu.GetDrawable())
                {
                    window.Draw(item);
                }
            }
            else if (gameState == GameState.Options)
            {
                foreach (var item in optionsMenu.GetDrawable())
                {
                    window.Draw(item);
                }
            }

            window.Display();
        }