1
1
using System ;
2
2
using Microsoft . Xna . Framework ;
3
+ using Microsoft . Xna . Framework . Audio ;
3
4
using Microsoft . Xna . Framework . Graphics ;
4
5
using Microsoft . Xna . Framework . Input ;
6
+ using Microsoft . Xna . Framework . Media ;
5
7
using MonoGameLibrary ;
6
- using MonoGameLibrary . Audio ;
7
8
using MonoGameLibrary . Graphics ;
8
9
using MonoGameLibrary . Input ;
9
10
10
11
namespace DungeonSlime ;
11
12
12
- public class Game1 : Game
13
+ public class Game1 : Core
13
14
{
14
- private GraphicsDeviceManager _graphics ;
15
- private SpriteBatch _spriteBatch ;
16
-
17
15
// Defines the slime animated sprite.
18
16
private AnimatedSprite _slime ;
19
17
@@ -26,64 +24,48 @@ public class Game1 : Game
26
24
// Speed multiplier when moving.
27
25
private const float MOVEMENT_SPEED = 5.0f ;
28
26
29
- // Tracks the input manager
30
- private InputManager _input ;
31
-
32
27
// Tracks the position of the bat.
33
28
private Vector2 _batPosition ;
34
29
35
30
// Tracks the velocity of the bat.
36
31
private Vector2 _batVelocity ;
37
32
38
- // Tracks the audio manager
39
- private AudioManager _audio ;
33
+ // The sound effect to play when the bat bounces off the edge of the screen.
34
+ private SoundEffect _bounceSoundEffect ;
35
+
36
+ // The sound effect to play when the slime eats a bat.
37
+ private SoundEffect _collectSoundEffect ;
40
38
41
39
// The SpriteFont Description used to draw text
42
40
private SpriteFont _font ;
43
41
44
42
// Tracks the players score.
45
43
private int _score ;
46
44
47
- public Game1 ( )
45
+ public Game1 ( ) : base ( "Dungeon Slime" , 1280 , 720 , false )
48
46
{
49
- _graphics = new GraphicsDeviceManager ( this ) ;
50
- Content . RootDirectory = "Content" ;
51
- IsMouseVisible = true ;
52
-
53
- // Create the instance of the input manager.
54
- _input = new InputManager ( this ) ;
55
-
56
- // Add it to the game's component collection
57
- Components . Add ( _input ) ;
58
-
59
- // Create the instance of the audio manager.
60
- _audio = new AudioManager ( this ) ;
61
47
62
- // Add it to the game's component collection
63
- Components . Add ( _audio ) ;
64
48
}
65
49
66
50
protected override void Initialize ( )
67
51
{
68
- // TODO: Add your initialization logic here
69
-
70
52
base . Initialize ( ) ;
71
53
72
- // Set the initial position of the bat to be 10px
73
- // to the right of the slime.
74
- _batPosition = new Vector2 ( _slime . Width + 10 , 0 ) ;
54
+ // Determine the height of the font
55
+ float textHeight = _font . MeasureString ( "A" ) . Y ;
56
+
57
+ // Place the slime at a position below where the score will be displayed
58
+ _slimePosition = new Vector2 ( 0 , textHeight + 10 ) ;
59
+
60
+ // Set the initial position of the bat to be 10px to the right of the slime.
61
+ _batPosition = _slimePosition + new Vector2 ( _slime . Width + 10.0f , 0.0f ) ;
75
62
76
63
// Assign the initial random velocity to the bat.
77
64
AssignRandomBatVelocity ( ) ;
78
-
79
- // Start playing the background music
80
- _audio . PlaySong ( "audio/theme" ) ;
81
65
}
82
66
83
67
protected override void LoadContent ( )
84
68
{
85
- _spriteBatch = new SpriteBatch ( GraphicsDevice ) ;
86
-
87
69
// Create the texture atlas from the XML configuration file
88
70
TextureAtlas atlas = TextureAtlas . FromFile ( Content , "images/atlas-definition.xml" ) ;
89
71
@@ -94,23 +76,27 @@ protected override void LoadContent()
94
76
_bat = atlas . CreateAnimatedSprite ( "bat-animation" ) ;
95
77
96
78
// Load the bounce sound effect
97
- _audio . AddSoundEffect ( "audio/bounce" ) ;
79
+ _bounceSoundEffect = Content . Load < SoundEffect > ( "audio/bounce" ) ;
98
80
99
81
// Load the collect sound effect
100
- _audio . AddSoundEffect ( "audio/collect" ) ;
82
+ _collectSoundEffect = Content . Load < SoundEffect > ( "audio/collect" ) ;
101
83
102
- // Load the theme song
103
- _audio . AddSong ( "audio/theme" ) ;
84
+ // Load the background theme music
85
+ Song theme = Content . Load < Song > ( "audio/theme" ) ;
86
+
87
+ // Start playing the background music
88
+ Audio . PlaySong ( theme ) ;
104
89
105
90
// Load the font
106
91
_font = Content . Load < SpriteFont > ( "fonts/gameFont" ) ;
92
+
93
+ base . LoadContent ( ) ;
107
94
}
108
95
109
96
protected override void Update ( GameTime gameTime )
110
97
{
111
- // base.Update first so that components are updated before we perform
112
- // additional game logic
113
- base . Update ( gameTime ) ;
98
+ if ( GamePad . GetState ( PlayerIndex . One ) . Buttons . Back == ButtonState . Pressed || Keyboard . GetState ( ) . IsKeyDown ( Keys . Escape ) )
99
+ Exit ( ) ;
114
100
115
101
// Update the slime animated sprite.
116
102
_slime . Update ( gameTime ) ;
@@ -204,8 +190,8 @@ protected override void Update(GameTime gameTime)
204
190
{
205
191
_batVelocity = Vector2 . Reflect ( _batVelocity , normal ) ;
206
192
207
- // Play bounce sound
208
- _audio . PlaySoundEffect ( "audio/bounce" ) ;
193
+ // Play the bounce sound effect
194
+ Audio . PlaySoundEffect ( _bounceSoundEffect ) ;
209
195
}
210
196
211
197
_batPosition = newBatPosition ;
@@ -228,12 +214,14 @@ protected override void Update(GameTime gameTime)
228
214
// Assign a new random velocity to the bat
229
215
AssignRandomBatVelocity ( ) ;
230
216
231
- // Play collect sound
232
- _audio . PlaySoundEffect ( "audio/collect" ) ;
217
+ // Play the collect sound effect
218
+ Audio . PlaySoundEffect ( _collectSoundEffect ) ;
233
219
234
- // Increase the player's score.
220
+ // Increase the player's score.
235
221
_score += 100 ;
236
222
}
223
+
224
+ base . Update ( gameTime ) ;
237
225
}
238
226
239
227
private void AssignRandomBatVelocity ( )
@@ -252,78 +240,71 @@ private void AssignRandomBatVelocity()
252
240
253
241
private void CheckKeyboardInput ( )
254
242
{
255
- // Exit the game if the escape key is pressed.
256
- if ( _input . Keyboard . IsKeyDown ( Keys . Escape ) )
257
- {
258
- Exit ( ) ;
259
- }
260
-
261
243
// If the space key is held down, the movement speed increases by 1.5
262
244
float speed = MOVEMENT_SPEED ;
263
- if ( _input . Keyboard . IsKeyDown ( Keys . Space ) )
245
+ if ( Input . Keyboard . IsKeyDown ( Keys . Space ) )
264
246
{
265
247
speed *= 1.5f ;
266
248
}
267
249
268
250
// If the W or Up keys are down, move the slime up on the screen.
269
- if ( _input . Keyboard . IsKeyDown ( Keys . W ) || _input . Keyboard . IsKeyDown ( Keys . Up ) )
251
+ if ( Input . Keyboard . IsKeyDown ( Keys . W ) || Input . Keyboard . IsKeyDown ( Keys . Up ) )
270
252
{
271
253
_slimePosition . Y -= speed ;
272
254
}
273
255
274
256
// if the S or Down keys are down, move the slime down on the screen.
275
- if ( _input . Keyboard . IsKeyDown ( Keys . S ) || _input . Keyboard . IsKeyDown ( Keys . Down ) )
257
+ if ( Input . Keyboard . IsKeyDown ( Keys . S ) || Input . Keyboard . IsKeyDown ( Keys . Down ) )
276
258
{
277
259
_slimePosition . Y += speed ;
278
260
}
279
261
280
262
// If the A or Left keys are down, move the slime left on the screen.
281
- if ( _input . Keyboard . IsKeyDown ( Keys . A ) || _input . Keyboard . IsKeyDown ( Keys . Left ) )
263
+ if ( Input . Keyboard . IsKeyDown ( Keys . A ) || Input . Keyboard . IsKeyDown ( Keys . Left ) )
282
264
{
283
265
_slimePosition . X -= speed ;
284
266
}
285
267
286
268
// If the D or Right keys are down, move the slime right on the screen.
287
- if ( _input . Keyboard . IsKeyDown ( Keys . D ) || _input . Keyboard . IsKeyDown ( Keys . Right ) )
269
+ if ( Input . Keyboard . IsKeyDown ( Keys . D ) || Input . Keyboard . IsKeyDown ( Keys . Right ) )
288
270
{
289
271
_slimePosition . X += speed ;
290
272
}
291
273
292
- // If the M key is pressed, toggle mute state for audio
293
- if ( _input . Keyboard . WasKeyJustPressed ( Keys . M ) )
274
+ // If the M key is pressed, toggle mute state for audio.
275
+ if ( Input . Keyboard . WasKeyJustPressed ( Keys . M ) )
294
276
{
295
- _audio . ToggleMute ( ) ;
277
+ Audio . ToggleMute ( ) ;
296
278
}
297
279
298
- // If the + button is pressed, increase the volume
299
- if ( _input . Keyboard . WasKeyJustPressed ( Keys . OemPlus ) )
280
+ // If the + button is pressed, increase the volume.
281
+ if ( Input . Keyboard . WasKeyJustPressed ( Keys . OemPlus ) )
300
282
{
301
- _audio . IncreaseVolume ( 0.1f ) ;
283
+ Audio . IncreaseVolume ( 0.1f ) ;
302
284
}
303
285
304
- // If the - key is pressed, decrease the volume
305
- if ( _input . Keyboard . WasKeyJustPressed ( Keys . OemMinus ) )
286
+ // If the - button was pressed, decrease the volume.
287
+ if ( Input . Keyboard . WasKeyJustPressed ( Keys . OemMinus ) )
306
288
{
307
- _audio . DecreaseVolume ( 0.1f ) ;
289
+ Audio . DecreaseVolume ( 0.1f ) ;
308
290
}
309
291
}
310
292
311
293
private void CheckGamePadInput ( )
312
294
{
313
- // Get the info for player one's gamepad.
314
- GamePadInfo gamePadOne = _input . GamePads [ ( int ) PlayerIndex . One ] ;
295
+ GamePadInfo gamePadOne = Input . GamePads [ ( int ) PlayerIndex . One ] ;
315
296
316
297
// If the A button is held down, the movement speed increases by 1.5
317
298
// and the gamepad vibrates as feedback to the player.
318
299
float speed = MOVEMENT_SPEED ;
319
300
if ( gamePadOne . IsButtonDown ( Buttons . A ) )
320
301
{
321
302
speed *= 1.5f ;
322
- gamePadOne . SetVibration ( 1.0f , TimeSpan . FromSeconds ( 0.5f ) ) ;
303
+ GamePad . SetVibration ( PlayerIndex . One , 1.0f , 1.0f ) ;
323
304
}
324
305
else
325
306
{
326
- gamePadOne . StopVibration ( ) ;
307
+ GamePad . SetVibration ( PlayerIndex . One , 0.0f , 0.0f ) ;
327
308
}
328
309
329
310
// Check thumbstick first since it has priority over which gamepad input
@@ -364,23 +345,24 @@ private void CheckGamePadInput()
364
345
365
346
protected override void Draw ( GameTime gameTime )
366
347
{
348
+ // Clear the back buffer.
367
349
GraphicsDevice . Clear ( Color . CornflowerBlue ) ;
368
350
369
351
// Begin the sprite batch to prepare for rendering.
370
- _spriteBatch . Begin ( samplerState : SamplerState . PointClamp ) ;
352
+ SpriteBatch . Begin ( samplerState : SamplerState . PointClamp ) ;
371
353
372
354
// Draw the slime sprite.
373
- _slime . Draw ( _spriteBatch , _slimePosition ) ;
355
+ _slime . Draw ( SpriteBatch , _slimePosition ) ;
374
356
375
357
// Draw the bat sprite.
376
- _bat . Draw ( _spriteBatch , _batPosition ) ;
358
+ _bat . Draw ( SpriteBatch , _batPosition ) ;
377
359
378
360
// Draw the score
379
- _spriteBatch . DrawString ( _font , $ "Score: { _score } ", Vector2 . Zero , Color . White ) ;
361
+ SpriteBatch . DrawString ( _font , $ "Score: { _score } ", Vector2 . Zero , Color . White ) ;
380
362
381
363
// Always end the sprite batch when finished.
382
- _spriteBatch . End ( ) ;
364
+ SpriteBatch . End ( ) ;
383
365
384
366
base . Draw ( gameTime ) ;
385
367
}
386
- }
368
+ }
0 commit comments