Спонсор: Microsoft
Санкт-Петербургский государственный политехнический университет
Опубликован: 09.12.2013 | Доступ: свободный | Студентов: 2049 / 29 | Длительность: 06:23:00
Специальности: Программист
Самостоятельная работа 2:

Практика. Основы 2Д. Работа со SpriteBatch. Масштабирование, повороты, отражения

< Самостоятельная работа 1 || Самостоятельная работа 2: 123

Реализация:

В следующем листинге приводится реализация задания с использованием игровых состояний и обработкой пользовательского ввода.

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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Lab2
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D sprite;

        int width;
        int height;

        /// <summary>
        /// Текущее игровое состояние
        /// </summary>
        int currentState = 0;

        KeyboardState oldState;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            width = graphics.PreferredBackBufferWidth = 800;
            height = graphics.PreferredBackBufferHeight = 600;
        }

        /// <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();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            sprite = Content.Load<Texture2D>("hero");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            KeyboardState state = Keyboard.GetState();

            
            if (state.IsKeyDown(Keys.Space) && oldState.IsKeyUp(Keys.Space))
            {
                // смена текущего игрового состояния при нажатии на пробел
                // значение этой переменной изменяется от 0 до 7
                currentState = (currentState + 1) % 8;
            }

            oldState = state;

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            if (currentState == 0)
            {
                spriteBatch.Begin();
                // Центр изображения совпадает с центром экрана
                spriteBatch.Draw(sprite, new Vector2(width / 2 - sprite.Width / 2, height / 2 - sprite.Height / 2), Color.White);
                spriteBatch.End();
            }
            if (currentState == 1)
            {
                float scale = 2;
                spriteBatch.Begin();
                // Центр изображения совпадает с центром экрана с учетом масштаба изображения
                spriteBatch.Draw(sprite, new Vector2(width / 2 - scale * sprite.Width / 2, height / 2 - (scale * sprite.Height / 2)), null, Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                spriteBatch.End();
            }
            if (currentState == 2)
            {
                // Масштаб изображения меняется от 1/2 до 2 в зависимости от времени
                float scale = MathHelper.Lerp(0.5f, 2, (float)Math.Abs(Math.Sin(gameTime.TotalGameTime.TotalSeconds)));
                spriteBatch.Begin();
                spriteBatch.Draw(sprite, new Vector2(width / 2 - (scale * sprite.Width / 2), height / 2 - (scale * sprite.Height / 2)), null, Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                spriteBatch.End();
            }
            if (currentState == 3)
            {
                float scale = 2;
                float rotation = (float)gameTime.TotalGameTime.TotalSeconds;
                spriteBatch.Begin();
                // Центр вращения совпадает с центром экрана
                spriteBatch.Draw(sprite, new Vector2(width / 2, height / 2), null, Color.White, rotation, Vector2.Zero, scale, SpriteEffects.None, 0);
                spriteBatch.End();
            }
            if (currentState == 4)
            {
                float scale = 2;
                float rotation = (float)gameTime.TotalGameTime.TotalSeconds;
                // Центр вращения изображения находится в середине изображения
                Vector2 origin = new Vector2(sprite.Width/2, sprite.Height/2);
                spriteBatch.Begin();
                // Центр вращения совпадает с центром экрана
                spriteBatch.Draw(sprite, new Vector2(width / 2, height / 2), null, Color.White, rotation, origin, scale, SpriteEffects.None, 0);
                spriteBatch.End();
            }
            if (currentState == 5)
            {
                float scale = 2;
                float rotation = (float)gameTime.TotalGameTime.TotalSeconds;
                // Центр вращения изображения находится в правом нижнем углу изображения
                Vector2 origin = new Vector2(sprite.Width, sprite.Height);
                spriteBatch.Begin();
                // Центр вращения совпадает с центром экрана
                spriteBatch.Draw(sprite, new Vector2(width / 2, height / 2), null, Color.White, rotation, origin, scale, SpriteEffects.None, 0);
                spriteBatch.End();
            }
            if (currentState == 6)
            {
                float scale = 2;
                spriteBatch.Begin();
                // Отражения изображения по горизонтали
                spriteBatch.Draw(sprite, new Vector2(width / 2 - scale * sprite.Width / 2, height / 2 - (scale * sprite.Height / 2)), null, Color.White, 0, Vector2.Zero, scale, SpriteEffects.FlipHorizontally, 0);
                spriteBatch.End();
            }
            if (currentState == 7)
            {
                float scale = 2;
                spriteBatch.Begin();
                // Отражения изображения по вертикали
                spriteBatch.Draw(sprite, new Vector2(width / 2 - scale * sprite.Width / 2, height / 2 - (scale * sprite.Height / 2)), null, Color.White, 0, Vector2.Zero, scale, SpriteEffects.FlipVertically, 0);
                spriteBatch.End();
            }
            

            base.Draw(gameTime);
        }
    }
}
< Самостоятельная работа 1 || Самостоятельная работа 2: 123
Сергей Попов
Сергей Попов

Компелятор говорит что у StorageContainer нет свойства TotalStorage, как решить эту проблему ?

ost dem
ost dem

не работает 

        AudioEngine engine;
        WaveBank waveBank;
        SoundBank soundBank;

пишет

Ошибка    2    Не удалось найти имя типа или пространства имен "AudioEngine" (пропущена директива using или ссылка на сборку?)    

 

так же не работает

using Microsoft.Xna.Framework.Xact;

пишет 

Ошибка    1    Имя типа или пространства имен "Xact" отсутствует в пространстве имен "Microsoft.Xna.Framework" (пропущена ссылка на сборку?)   

как это исправить?