I am trying to make Pong in XNA/C# using a class for the Paddle and Ball
Game1.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Pong
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Paddle Paddle1 = new Paddle();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
Paddle1.Draw();
base.Draw(gameTime);
}
}
}
Paddle.cs:
namespace Pong
{
class Paddle
{
SpriteBatch spriteBatch;
ContentManager Content;
Texture2D paddle1;
Texture2D paddle2;
Vector2 Paddle1;
Vector2 Paddle2;
public void LoadContent()
{
paddle1 = Content.Load<Texture2D>("pongpaddle1");
Paddle1 = new Vector2();
Paddle1.X = 50;
Paddle1.Y = 50;
}
public void Draw()
{
spriteBatch.Begin(); //Causes NullReferenceException was unhandled, Object reference not set to an instance of an object.
spriteBatch.Draw(paddle1, Paddle1, Color.White);
spriteBatch.End();
}
}
}
I don't have anything in the Ball class yet, but it will use similar methods to Paddle.cs
Every time I've ran the code, I keep getting a System.StackOverFlow exception whenever it hits this line of code in Game1.cs:
Paddle Paddle1 = new Paddle();
How do I fix this? I don't see how it's run out of memory already.
EDIT: Updated code.
What's happening here is that Paddle inherits Game1. Game1 creates new Paddles:
Paddle Paddle1 = new Paddle();
Paddle Paddle2 = new Paddle();
Those Paddles are Games that need to initialize their own set of Paddles. Infinite recursion! I'm not sure how XNA works, but if that's how the inheritance should be, just move your initializations to Initialize():
// TODO: Add your initialization logic here
base.Initialize();
this.Paddle1 = new Paddle();
this.Paddle2 = new Paddle();
I kind of doubt that a game object should inherit from the game itself, though. That would seem like a rather poor design decision.
public class Game1 : Microsoft.Xna.Framework.Game
{
Paddle Paddle1 = new Paddle();
Paddle Paddle2 = new Paddle();
...
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
Paddle1.LoadContent();
}
...
}
class Paddle : Game1
{
...
protected override void LoadContent()
{
Paddle1 = new Vector2();
Paddle1.X = 50;
Paddle1.Y = 50;
base.LoadContent();
}
...
}
Two big problems here, there is a recursive LoadContent call. Not to mention your paddles have paddles which have paddles... Why is your paddle inheriting from Game1? It almost definitely shouldn't be.
Also your paddle instances instantiate other paddle instances, so you're in a loop of instantiating other paddle classes.
It seems like you might want to take a step back and just get used to some basic code first? For what it's worth, I wrote pong in xna for fun a few years back, it's a bit messy, but it might give you some starting help.
Here is an example of a paddle class based off the DrawableGameComponent class (drawn in primatives so it's a bit verbose):
public class Paddle : DrawableGameComponent
{
private readonly VertexPositionColor[] _vertices = new VertexPositionColor[6];
private readonly float _width;
private readonly float _height;
private IndexBuffer _indexbuffer;
private VertexBuffer _vertexbuffer;
public Vector3 Position { get; set; }
public Vector3 Direction { get; set; }
public float Speed { get; set; }
public Paddle(Game game, float width, float height)
: base(game)
{
_width = width;
_height = height;
}
protected override void LoadContent()
{
base.LoadContent();
_vertices[0].Position = new Vector3(0, 0, 0);
_vertices[0].Color = Color.Red;
_vertices[1].Position = new Vector3(_width, _height, 0);
_vertices[1].Color = Color.Green;
_vertices[2].Position = new Vector3(0, _height, 0);
_vertices[2].Color = Color.Blue;
_vertices[3].Position = new Vector3(_width, 0, 0);
_vertices[3].Color = Color.Green;
_vertexbuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), _vertices.Length, BufferUsage.WriteOnly);
_vertexbuffer.SetData(_vertices);
var indices = new short[6];
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
indices[3] = 0;
indices[4] = 3;
indices[5] = 1;
_indexbuffer = new IndexBuffer(GraphicsDevice, typeof(short), 6, BufferUsage.WriteOnly);
_indexbuffer.SetData(indices);
}
public BoundingBox GetBoundingBox()
{
return new BoundingBox(Position, Position + new Vector3(_width, _height, 0));
}
public override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
GraphicsDevice.SetVertexBuffer(_vertexbuffer);
GraphicsDevice.Indices = _indexbuffer;
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);
}
}
Related
How do I clone an object then pick a random position, then draw it.
This is the code I have for the object:
public class Trash : ICloneable
{
private Texture2D _texture;
private float _rotation;
public Vector2 Position;
public Vector2 Origin;
public float RotationVelocity = 3f;
public float LinearVelocity = 4f;
public Trash(Texture2D texture)
{
_texture = texture;
}
public void Update()
{
// Do epic stuff here
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(_texture, Position, null, Color.White, _rotation, Origin, 1, SpriteEffects.None, 0f);
}
public object Clone()
{
return this.MemberwiseClone();
}
And this is code I have in Game1.cs so far:
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private SeaJam.Objects.Trash Trash;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
var texture = Content.Load<Texture2D>("Prototype");
Trash = new Objects.Trash(texture)
{
Position = new Vector2(100, 100),
Origin = new Vector2(texture.Width / 2, texture.Height - 25),
};
}
protected override void Update(GameTime gameTime)
{
Trash.Update();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
Trash.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
private void AddTrash()
{
var rnd = new System.Random();
var NewTrash = Trash.Clone();
}
The problem is Whenever I'd try to give a random position for the clone in the AddTrash() Method, I'd only get errors, such as "'object' does not contain a definition for 'Position' and no accessible extension method 'Position' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)"
your constructor:
public Trash(Texture2D texture)
{
_texture = texture;
}
Needs to be extended with the desired changeable parameters. In your case, it needs to add Position and Origin as parameter, and then apply it as a value.
Like this:
public Trash(Texture2D texture, Vector2 position, Vector2 origin)
{
_texture = texture;
Position = position;
Origin = origin;
}
And change the way you call it in the game1.cs as well, they need to work similair like texture:
var texture = Content.Load<Texture2D>("Prototype");
var position = new Vector2(100, 100),
var origin = new Vector2(texture.Width / 2, texture.Height - 25),
Trash = new Objects.Trash(texture, position, origin);
And as a tip: keep consistency in your field names, mixing in underscore and lowercase in one field, and uppercase in an other field will get confusing to understand. especially when the parameters needs a name different from the fields as well. I prefer to keep them all with the first letter uppercase.
I'm just starting with Monogame and I'm trying to make a simple sprite, which later is meant to be a button. I've searched all around and done several tutorials, but I can't make it work. I just keep getting the blank, blue screen. Here's my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
namespace Test_Game
{
class Main_Menu
{
//setting the variables
public Texture2D button1;
public Vector2 button1Pos;
public GraphicsDevice graphicsDevice;
GraphicsDeviceManager graphics;
public void initialize(Texture2D texture, Vector2 position, ContentManager Content)
{
//Getting the initialized stuff
button1 = Content.Load<Texture2D>("button_temp");
button1Pos.X = 30;
button1Pos.Y = 30;
}
public void Draw(SpriteBatch spriteBatch)
{
graphics.GraphicsDevice.Clear(Color.Black);
spriteBatch = new SpriteBatch(graphicsDevice);
//Just drawing the Sprite
spriteBatch.Begin();
spriteBatch.Draw(button1, new Rectangle(30, 30, 214, 101), Color.White);
spriteBatch.End();
}
}
}
Hope you can find an answer.
I can see many mistakes in your code, I would left a comment pointing all of the, but its too long.
Most of the mistakes come from this: you're not inheriting from Game class. Your line class Main_Menu should be class Main_Menu : Game. Always use this template for a game class:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace MyGame
{
public class MyGame : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public MyGame()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
}
}
}
From here on, you must fill this template with the following in mind:
Create your memory-only objects in the Initialize method;
Load and create file-related objects in the LoadContent method;
Add your game logic in the Update method;
Add your drawing logic in the Draw method;
Usually, do not bother with the constructor or the UnloadContent method.
Connecting your existing code with the template, we get the following:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Test_Game
{
public class Main_Menu : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Vector2 buttonPos; // our button position
Texture2D button; // our button texture
public MyGame()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
buttonPos = new Vector2(30, 30); // X=30, Y=30
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
button = Content.Load<Texture2D>("button_temp"); // load texture
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
// here we would add game logic
// things like moving game objects, detecting collisions, etc
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
// draw our button
int buttonWidth = 214;
int buttonHeight = 101;
spriteBatch.Draw(button, new Rectangle(buttonPos.X, buttonPos.Y, buttonWidth, buttonHeight), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
This template is used in every game for the MonoGame and XNA frameworks, so you should find a LOT of content on the web about what each method of the Game class does.
In the game logic part of my game, where you check for input, why is the state my object had before is not being used in further evaluations of my function?
My game class:
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using System.Diagnostics;
namespace MonoRPG
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Game1()
: base()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
Player playerObject = new Player(this, this.Content, this.spriteBatch);
KeyboardState oldState;
KeyboardState newState = Keyboard.GetState();
if (newState.IsKeyDown(Keys.Right))
{
Debug.WriteLine("right key pressed");
}
oldState = newState;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
renderMap createMap = new renderMap(this, this.Content, this.spriteBatch);
Player playerObject = new Player(this, this.Content, this.spriteBatch);
createMap.RenderMap();
playerObject.drawPlayer();
playerObject.positionX = playerObject.positionX + 10;
base.Draw(gameTime);
}
}
}
And my player class:
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using System.Diagnostics;
namespace MonoRPG
{
public class Player
{
public int positionX = 0;
public int positionY = 0;
Game1 draw = new Game1();
ContentManager gameContent;
SpriteBatch playerSprites;
KeyboardState oldState;
public Player(Game1 canvas, ContentManager content, SpriteBatch spriteBatch)
{
draw = canvas;
gameContent = content;
playerSprites = spriteBatch;
}
public Player()
{
}
public void drawPlayer()
{
Texture2D playerTexture = gameContent.Load<Texture2D>("player/player.png");
playerSprites.Begin();
playerSprites.Draw(playerTexture, new Vector2(positionX, positionY), Color.Red);
playerSprites.End();
}
public void playerMove(KeyboardState keyState, KeyboardState oldState)
{
positionX = positionX + 1;
}
}
}
The drawing is working fine, but the position of the rectangle sprite I'm using for the player won't change. Is the problem related to how I'm creating the objects? I tried declaring outside the functions, but then I can't use the this keyword. How can I call the functions on my existing objects?
You should initialize (create) objects in the Initialize or LoadContent methods. The object should be created only once, not every update of the game. The Initialize and LoadContent happens only once, at the startup of the game (the Initialize first, the LoadContent when you call base.Initialize()).
When you put code in the Update method, it is executed at every update (usually, every frame) of the game. Your code should look more like this
Player player;
KeyboardState oldState; // player and oldState belongs to the whole game
protected override void Initialize() {
// remember that spriteBatch is still null here
player = new Player(this, Content, spriteBatch);
// the line below initializes the spriteBatch
// check the comments to see why this exact code won't work
base.Initialize();
}
protected override void Update(GameTime gameTime) {
KeyboardState newState = Keyboard.GetState(); // newState belongs to this exact update only
if (newState.IsKeyDown(Keys.Right) && oldState.IsKeyUp(Keys.Right)) {
player.playerMove(newState, oldState); // not sure why you wish to pass these arguments?
}
oldState = newState;
}
protected override void Draw(GameTime gameTime) {
GraphicsDevice.Clear(Color.CornflowerBlue);
renderMap createMap = new renderMap(this, this.Content, this.spriteBatch);
createMap.RenderMap();
// the player here is the same player in the Update method
player.drawPlayer();
base.Draw(gameTime);
}
I'm trying to animate a character and make him walk left and right. While I've learned how to make a basic animation, I can't figure out how to switch between them.
When the character walks left I'd like him to switch from the 'idle' (animatedSprite) animation to the 'walking left'(animatedSprite2) animation.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace WalkingAnimation
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private AnimatedSprite animatedSprite, animatedSprite2;
private Vector2 position = new Vector2(350f, 200f);
private KeyboardState keyState;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Texture2D texture = Content.Load<Texture2D>("Idle");
Texture2D texture2 = Content.Load<Texture2D>("Run");
animatedSprite = new AnimatedSprite(texture, 1, 11);
animatedSprite2 = new AnimatedSprite(texture2, 1, 10);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.Q))
{
position.X -= 3;
}
if (keyState.IsKeyDown(Keys.P))
{
position.X += 3;
}
base.Update(gameTime);
animatedSprite.Update();
animatedSprite2.Update();
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
animatedSprite.Draw(spriteBatch, position);
}
}
}
Like you so clearly expressed in your question, all you need to do is store a Boolean which describes whether the character is idle or not:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace WalkingAnimation
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private AnimatedSprite animatedSprite, animatedSprite2;
private Vector2 position = new Vector2(350f, 200f);
private KeyboardState keyState;
private bool idle;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
// Start of as idle
idle = true;
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
// Textures
Texture2D texture = Content.Load<Texture2D>("Idle");
Texture2D texture2 = Content.Load<Texture2D>("Run");
// Animations
animatedSprite = new AnimatedSprite(texture, 1, 11);
animatedSprite2 = new AnimatedSprite(texture2, 1, 10);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
keyState = Keyboard.GetState();
idle = true; // If the character doesn't move this will stay true
if (keyState.IsKeyDown(Keys.Q))
{
position.X -= 3;
idle = false;
}
if (keyState.IsKeyDown(Keys.P))
{
position.X += 3;
idle = false;
}
base.Update(gameTime);
animatedSprite.Update();
animatedSprite2.Update();
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
if(idle)
animatedSprite.Draw(spriteBatch, position);
else
animatedSprite2.Draw(spriteBatch, position);
}
}
}
And that should do what you want.
Also, if animations get a bit clunky I would recommend using sprite sheets and creating your own Animation class which can store all the player variables. This is extra-useful if you decide to make your game multiplayer.
Lets say left and right walking anmation uses 5 frame. And it's on single texture.
Function Update
if State = Left
currentFrame +=1;
if currentFrame > 5 then currentFrame = 0
texureSource = new rectangle(32*currentFrame,0,32,23);
end if
if State = Right
currentFrame +=1;
if currentFrame > 5 then currentFrame = 0
texureSource = new rectangle(32*currentFrame,32,32,23);
end if
End Function
I have a cloud and a man sprite, these are both drawn separately but for some reason they are drawing together and overlapping and then positioning themselves at the defined places for both of the sprites.
My sprites class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace MarioFake
{
class Sprites
{
public Vector2 MarioPosition = new Vector2(200, 200);
private Texture2D MarioStill;
public Vector2 LargeCloudPosition = new Vector2(100, 100);
private Texture2D LargeCloud;
public void LoadContent(ContentManager theContentManager, string theAssetName)
{
MarioStill = theContentManager.Load<Texture2D>(theAssetName);
LargeCloud = theContentManager.Load<Texture2D>(theAssetName);
}
public void Draw(SpriteBatch theSpriteBatch)
{
theSpriteBatch.Draw(MarioStill, MarioPosition, Color.White);
theSpriteBatch.Draw(LargeCloud, LargeCloudPosition, Color.White);
}
}
}
and my drawing method in my Game class:
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
MarioStill.Draw(this.spriteBatch);
LargeCloud.Draw(this.spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
The sprite class doesn't need to hold information for both Mario and the cloud, if you create a generic sprite class like this ..
public class Sprite
{
public Vector2 Location;
public Texture2D Texture;
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, Location, Color.White);
}
}
you can create your mario and clouds like this.
Sprite Mario = new Sprite() { Location = new Vector2(200, 200), Texture = Content.Load<Texture2D>("MarioTexture") };
Sprite Cloud = new Sprite() { Location = new Vector2(100, 100), Texture = Content.Load<Texture2D>("CloudTexture") };
and draw them as you previously did.
Mario.Draw(spriteBatch);
Cloud.Draw(spriteBatch);
Here is an example of a full game class that demonstrates loading and rendering both sprites.
public class Sprite
{
public Vector2 Location;
public Texture2D Texture;
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, Location, Color.White);
}
}
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
List<Sprite> sprites;
Sprite mario, cloud;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
// Create the sprites.
sprites = new List<Sprite>();
mario = new Sprite() { Location = new Vector2(100, 100), Texture = Content.Load<Texture2D>("MarioTexture") };
cloud = new Sprite() { Location = new Vector2(200, 200), Texture = Content.Load<Texture2D>("CloudTexture") };
sprites.Add(mario);
sprites.Add(cloud);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
foreach (var sprite in sprites)
sprite.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}