Skip to content

Add Chapter 08: The AnimatedSprite Class #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions articles/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@
href: tutorials/building_2d_games/06_optimizing_texture_rendering/
- name: "07: The Sprite Class"
href: tutorials/building_2d_games/07_the_sprite_class/
- name: "07: The AnimatedSprite Class"
href: tutorials/building_2d_games/08_the_animatedsprite_class/
- name: Console Access
href: console_access.md
- name: Help and Support
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public Sprite CreateSprite(string regionName)
}
```

## Using the `Sprite` Class
## Using the Sprite Class

Let's adjust our game now to use the `Sprite` class instead of just the texture regions. Replace the contents of *Game1.cs* with the following:

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGameLibrary.Graphics;

namespace MonoGameSnake;

public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private AnimatedSprite _slime;
private AnimatedSprite _bat;

public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}

protected override void Initialize()
{
// TODO: Add your initialization logic here

base.Initialize();
}

protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);

// Create the texture atlas from the XML configuration file
TextureAtlas atlas = TextureAtlas.FromFile(Content, "images/atlas-definition.xml");

// Create the slime animated sprite
_slime = atlas.CreateAnimatedSprite("slime-animation");

// Create the bat animated sprite
_bat = atlas.CreateAnimatedSprite("bat-animation");
}

protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();

// Update the slime and bat animated sprites
_slime.Update(gameTime);
_bat.Update(gameTime);

base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

_spriteBatch.Begin(samplerState: SamplerState.PointClamp);

// Draw the slime animated sprite
_slime.Draw(_spriteBatch, Vector2.One);

// Draw the bat animated sprite 10px to the right of the slime.
_bat.Draw(_spriteBatch, new Vector2(_slime.Width + 10, 0));

_spriteBatch.End();

base.Draw(gameTime);
}
}
1 change: 1 addition & 0 deletions articles/tutorials/building_2d_games/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This documentation will introduce game development concepts using the MonoGame f
| [05: Working with Textures](05_working_with_textures/index.md) | Learn how to load and render textures using the MonoGame content pipeline and [**SpriteBatch**](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch). | |
| [06: Optimizing Texture Rendering](06_optimizing_texture_rendering/index.md) | Explore optimization techniques when rendering textures using a texture atlas. | |
| [07: The Sprite Class](07_the_sprite_class/index.md) | Explore creating a reusable Sprite class to efficiently sprites and their rendering properties, including position, rotation, scale, and more. | |
| [08: The AnimatedSprite Class](07_the_sprite_class/index.md) | Create an AnimatedSprite class that builds upon our Sprite class to support frame-based animations. | |

In additional to the chapter documentation, supplemental documentation is also provided to give a more in-depth look at different topics with MonoGame. These are provided through the Appendix documentation below:

Expand Down