- XNA 4.0 Game Development by Example Beginner's Guide(Visual Basic Edition)
- Kurt Jaegers
- 289字
- 2021-08-20 15:50:35
Time for action – draw SquareChase!
- Alter the
GraphicsDevice.Clear(Color.CornflowerBlue)
call and replaceColor.CornflowerBlue
withColor.Gray
to make the game a bit easier on the eyes. - 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.
- 有趣的二進(jìn)制:軟件安全與逆向分析
- Microsoft SQL Server企業(yè)級(jí)平臺(tái)管理實(shí)踐
- Voice Application Development for Android
- 文本數(shù)據(jù)挖掘:基于R語(yǔ)言
- 大數(shù)據(jù)可視化
- 大話Oracle Grid:云時(shí)代的RAC
- WS-BPEL 2.0 Beginner's Guide
- MySQL 8.x從入門到精通(視頻教學(xué)版)
- PostgreSQL指南:內(nèi)幕探索
- Power BI智能數(shù)據(jù)分析與可視化從入門到精通
- SQL Server深入詳解
- 實(shí)用數(shù)據(jù)結(jié)構(gòu)
- 區(qū)域云計(jì)算和大數(shù)據(jù)產(chǎn)業(yè)發(fā)展:浙江樣板
- Visual Studio 2013 and .NET 4.5 Expert Cookbook
- openGauss數(shù)據(jù)庫(kù)核心技術(shù)