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

Time for action – draw SquareChase!

  1. Alter the GraphicsDevice.Clear(Color.CornflowerBlue) call and replace Color.CornflowerBlue with Color.Gray to make the game a bit easier on the eyes.
  2. Add the following code after the call to clear the display:
    spriteBatch.Begin()
    spriteBatch.Draw(
      squareTexture,
      currentSquare,
      colors(playerScore Mod 3))
    spriteBatch.End()

What just happened?

Any time you use a SpriteBatch object to draw to the display, you need to wrap the calls inside a Begin() and End() pair. Any number of calls to spriteBatch.Draw() can be included in a single batch, and it is common practice to simply start a Begin() at the top of your Draw() code, use it for all of your drawing, and then End() it right before the Draw() method exits. While not benefiting our SquareChase game, batching sprite drawing calls greatly speeds up the process of drawing a large number of images, by submitting them to the rendering system all at once instead of processing each image individually.

The SpriteBatch.Draw() method is used to draw a Texture2D object to the screen. There are a number of different options for how to specify what will be drawn. In this case, the simplest call requires a Texture2D object (squareTexture), a destination Rectangle (currentSquare), and a tint color to apply to the sprite. The expression "playerScore Mod 3" takes the player's score, divides it by 3, and returns the remainder. The result will always be 0, 1, or 2. This fits perfectly as an index to the elements in the colors array, allowing us to easily change the color of the square each time the player catches one.

Finally, the spriteBatch.End() tells XNA that we have finished queuing up sprites to draw and it should actually push them all out to the graphics card.

主站蜘蛛池模板: 达日县| 阳谷县| 大荔县| 牟定县| 历史| 塘沽区| 治多县| 韶山市| 都兰县| 德江县| 梅河口市| 广德县| 丹阳市| 庄浪县| 井冈山市| 香港 | 濉溪县| 洪江市| 衢州市| 马鞍山市| 天峻县| 阿拉善左旗| 简阳市| 隆子县| 攀枝花市| 凤阳县| 丰镇市| 香港| 平顶山市| 盐边县| 东安县| 松溪县| 开化县| 二连浩特市| 景宁| 泸定县| 白城市| 莫力| 中宁县| 北票市| 桐城市|