- XNA 4.0 Game Development by Example: Beginner's Guide
- Kurt Jaegers
- 491字
- 2021-04-13 16:52:01
- Add the following to
Update()
right before the call tobase.Update(gameTime);
if (timeRemaining == 0.0f) { currentSquare = new Rectangle( rand.Next(0, this.Window.ClientBounds.Width - 25), rand.Next(0, this.Window.ClientBounds.Height - 25), 25, 25); timeRemaining = TimePerSquare; } MouseState mouse = Mouse.GetState(); if ((mouse.LeftButton == ButtonState.Pressed) && (currentSquare.Contains(mouse.X, mouse.Y))) { playerScore++; timeRemaining = 0.0f; } timeRemaining = MathHelper.Max(0, timeRemaining - (float)gameTime.ElapsedGameTime.TotalSeconds); this.Window.Title = "Score : " + playerScore.ToString();
The first thing the Update()
routine does is check to see if the current square has expired by checking to see if timeRemaining
has been reduced to zero. If it has, a new square is generated using the Next()
method of the rand object. In this form, Next()
takes two parameters: an (inclusive) minimum value and a (non-inclusive) maximum value. In this case, the minimum is set to 0
, while the maximum is set to the size of the this.Window.ClientBounds
property minus 25
pixels. This ensures that the square will always be fully within the game window.
Next, the current position and button state of the mouse is captured into the "mouse" variable via Mouse.GetState()
. Both the Keyboard
and the GamePad
classes also use a GetState()
method that captures all of the data about that input device when the method is executed.
If the mouse reports that the left button is pressed, the code checks with the currentSquare
object by calling its Contains()
method to determine if the mouse's coordinates fall within its area. If they do, then the player has "caught" the square and scores a point. The timeRemaining
counter is set to 0, indicating that the next time Update()
is called it should create a new square.
After dealing with the user input, the MathHelper.Max()
method is used to decrease timeRemaining
by an amount equal to the elapsed game time since the last call to Update()
. Max()
is used to ensure that the value does not go below zero.
Finally, the game window title bar is updated to display the player's score.
Tip
The Microsoft.Xna.Framework
namespace provides a class called MathHelper that contains lots of goodies to make your life easier when dealing with numeric data, including converting degrees to and from radians, clamping values between a certain range, and generating smooth arcs between a starting and ending value.
The final method in the default Game1.cs
file is responsible, not surprisingly, for drawing the current game state to the display. Draw()
is normally called once after each call to Update()
unless something is happening to slow down the execution of your game. In that case, Draw()
calls may be skipped in order to call Update()
more frequently. There will always be at least one call to Update()
between calls to Draw()
, however, as sequential Draw()
calls would provide no benefit—nothing in the game state will have changed.
The default Draw()
method simply clears the display window in the Cornflower Blue color.
- Painter 現代服裝效果圖表現技法
- 剪映短視頻制作全流程:剪輯、調色、字幕、音效
- Photoshop CS5平面設計入門與提高
- 24小時全速學會Photoshop 2021
- 中文版Illustrator CC 2018基礎培訓教程
- Android User Interface Development: Beginner's Guide
- AutoCAD 2019中文版計算機輔助繪圖全攻略
- Photoshop CS6實戰從入門到精通(超值版)
- iPad+Procreate室內設計手繪表現技法
- Seam 2 Web Development: LITE
- 平面設計制作標準教程(微課版 第2版)
- Photoshop CS6中文版基礎與實例教程(第6版)
- 中文版Flash CS6動畫制作(慕課版)
- Plone 3.3 Site Administration
- 攝影師的后期必修課(RAW格式篇)