Предыдущая Следующая
namespace MoveSpriteArray
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
122 Движение спрайтов в пространстве
Проект MoveSpriteArray 123
ContentManager content; KeyboardState keyboardState; SpriteBatch spriteBatch; Sprite[] sprite = new Sprite[5];
private Texture2D background1; private Texture2D background2; Random rand = new Random(); int j;
/// <summary> /// Конструктор /// <summary> public Game1()
{
graphics = new GraphicsDeviceManager(this); content = new ContentManager(Services); graphics.PreferredBackBufferWidth = 1024; graphics.PreferredBackBufferHeight = 7 68; graphics.PreferMultiSampling = false; graphics.IsFullScreen = true; for (int i = 0; sprite.Length > i; {
sprite[i] = new Sprite(12, 10);
}
}
/// <summary> /// Инициализация /// <summary>
protected override void Initialize()
{
j = 0;
for (int i = 0; sprite.Length > i;
{
sprite[i].spritePosition = new Vector2(rand.Next(10, Window.ClientBounds.Width - 500), j = j - 300);
}
base.Initialize();
}
/// <summary>
/// Загрузка компонентов игры /// <summary>
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
background1 = content.Load<Texture2D>("Content\\Textures\\background1"); background2 = content.Load<Texture2D>("Content\\Textures\\background2"); sprite[0].Load(content, "Content\\Textures\\0"); sprite[1].Load(content, "Content\\Textures\\1"); sprite[2].Load(content, "Content\\Textures\\2"); sprite[3].Load(content, "Content\\Textures\\3");
sprite[4].Load(content, "Content\\Textures\\4");
} }
/// <summary>
/// Освобождаем ресурсы
/// <summary>
protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent == true)
{
content.Unload();
}
}
/// <summary>
/// Обновляем состояние игры /// <summary>
protected override void Update(GameTime gameTime)
{
keyboardState = Keyboard.GetState(); if (keyboardState.IsKeyDown(Keys.Escape)) this.Exit();
double elapsed = gameTime.ElapsedGameTime.TotalSeconds; for (int i = 0; sprite.Length > i;
{
sprite[i].UpdateFrame(elapsed);
}
MoveSprite();
base.Update(gameTime);
}
/// <summary>
/// Движение спрайта по вертикали
/// <summary>
public void MoveSprite()
{
for (int i = 0; sprite.Length > i;
{
sprite[i].spritePosition += sprite[i].speedSprite; if (sprite[i].spritePosition.Y > Window.ClientBounds.Height) Предыдущая Следующая
|