官术网_书友最值得收藏!

Time for action – drawing the screen – the play screen

  1. Update the Draw() method of the Game1 class to add the code to draw the game board after the code that draws the title screen:
    if (gameState == GameStates.Playing)
    {
        spriteBatch.Begin();
    
        spriteBatch.Draw(backgroundScreen,
            new Rectangle(0, 0,
                this.Window.ClientBounds.Width,
                this.Window.ClientBounds.Height),
            Color.White);
    
        for (int x = 0; x < GameBoard.GameBoardWidth; x++)
            for (int y = 0; y < GameBoard.GameBoardHeight; y++)
            {
                int pixelX = (int)gameBoardDisplayOrigin.X + 
                    (x * GamePiece.PieceWidth);
                int pixelY = (int)gameBoardDisplayOrigin.Y + 
                    (y * GamePiece.PieceHeight);
    
                spriteBatch.Draw(
                    playingPieces,
                    new Rectangle(
                      pixelX, 
                      pixelY, 
                      GamePiece.PieceWidth, 
                      GamePiece.PieceHeight),
                    EmptyPiece,
                    Color.White);
    
                spriteBatch.Draw(
                    playingPieces, new Rectangle(
                        pixelX, 
                        pixelY, 
                        GamePiece.PieceWidth, 
                        GamePiece.PieceHeight),
                    gameBoard.GetSourceRect(x, y),
                    Color.White);
            }
    
        this.Window.Title = playerScore.ToString();
    
        spriteBatch.End();
    }

What just happened?

As you can see, the code to draw the game board begins exactly like the code to draw the title screen. Since we are using a background image that takes up the full screen, we draw it exactly the same way as the title screen.

Next, we simply loop through gameBoard to draw the squares. The pixelX and pixelY variables are calculated to determine where on the screen each of the game pieces will be drawn. Since both x and y begin at 0, the (x * GamePiece.PieceWidth) and (y * GamePiece.PieceHeight) will also be equal to zero, resulting in the first square being drawn at the location specified by the gameBoardDisplayOrigin vector.

As x increments, each new piece is drawn 40 pixels further to the right than the previous piece. After a row has been completed, the y value increments, and a new row is started 40 pixels below the previous row.

The first spriteBatch.Draw() call uses Rectangle(pixelX, pixelY, GamePiece.PieceWidth, GamePiece.PieceHeight) as the destination rectangle and EmptyPiece as the source rectangle. Recall that we added this Rectangle to our declarations area as a shortcut to the location of the empty piece on the sprite sheet.

The second spriteBatch.Draw() call uses the same destination rectangle, overlaying the playing piece image onto the empty piece that was just drawn. It asks the gameBoard to provide the source rectangle for the piece it needs to draw.

The player's score is displayed in the window title bar, and spriteBatch.End() is called to finish up the Draw() method.

Keeping score

Longer chains of filled water pipes score the player more points. However, if we were to simply assign a single point to each piece in the pipe chain, there would be no scoring advantage to making longer chains versus quickly making shorter chains.

主站蜘蛛池模板: 博湖县| 辉南县| 大田县| 志丹县| 四平市| 绥德县| 怀宁县| 肇东市| 梅河口市| 卫辉市| 襄樊市| 北海市| 都昌县| 永胜县| 海阳市| 麻栗坡县| 老河口市| 长丰县| 岳普湖县| 元朗区| 克东县| 郴州市| 凉山| 临海市| 日喀则市| 宁强县| 安图县| 庄河市| 阿克| 讷河市| 博客| 卢龙县| 黄大仙区| 濮阳市| 安图县| 民勤县| 石首市| 林甸县| 遵义市| 东乌珠穆沁旗| 湖口县|