Создание 2D игры по средствам XNAGameStudio 4.0

Автор работы: Пользователь скрыл имя, 19 Декабря 2011 в 20:41, лабораторная работа

Краткое описание

Цель
Научится создавать простые 2D игры по средствам Microsoft XNA – набора инструментов с управляемой средой времени выполнения (.NET).

Содержимое работы - 1 файл

Отчет.doc

— 1.57 Мб (Скачать файл)

enemies.Add(enemy);

enemy.StartRun(false);

                    }

                    x += 40;

                }

                x = 0;

                y += 40;

            }

// if (musicInstance != null)

// musicInstance.Stop(true);

// musicInstance = music.Play(1, 0, 1);

music.Play(1, 0, 0);

        }

///<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>

protectedoverridevoid Initialize()

        {

// TODO: Add your initialization logic here

menu = newMenu();

MenuItemnewGame = newMenuItem("New Game");

MenuItemresumeGame = newMenuItem("Resume Game");

MenuItemexitGame = newMenuItem("Exit"); 

resumeGame.Active = false; 

newGame.Click += newEventHandler(newGame_Click);

resumeGame.Click += newEventHandler(resumeGame_Click);

exitGame.Click += newEventHandler(exitGame_Click); 

menu.Items.Add(newGame);

menu.Items.Add(resumeGame);

menu.Items.Add(exitGame); 

base.Initialize();

        } 

voidexitGame_Click(object sender, EventArgs e)

        {

this.Exit();

        } 

voidresumeGame_Click(object sender, EventArgs e)

        {

gameState = GameState.Game;

        } 

voidnewGame_Click(object sender, EventArgs e)

        {

menu.Items[1].Active = true;

gameState = GameState.Game;

Rectanglerect = newRectangle(60, 60, 60, 60);

hero = newAnimatedSprite(rect, idleTexture, runTexture, jumpTexture, this);

currentLevel = 1;

CreateLevel();

            Score = 0; 

        } 
 

///<summary>

///LoadContent will be called once per game and is the place to load

/// all of your content.

///</summary>

protectedoverridevoidLoadContent()

        {

// Create a new SpriteBatch, which can be used to draw textures.

spriteBatch = newSpriteBatch(GraphicsDevice); 

            blockTexture1 = Content.Load<Texture2D>("Textures/block");

            blockTexture2 = Content.Load<Texture2D>("Textures/block2"); 

idleTexture = Content.Load<Texture2D>("Textures/idle");

runTexture = Content.Load<Texture2D>("Textures/run");

jumpTexture = Content.Load<Texture2D>("Textures/jump");

gemTexture = Content.Load<Texture2D>("Textures/gem"); 

MenuWall = Content.Load<Texture2D>("Textures/wall/menu");

            Level1 = Content.Load<Texture2D>("Textures/wall/Level1");

            Level2 = Content.Load<Texture2D>("Textures/wall/Level2");

            Level3 = Content.Load<Texture2D>("Textures/wall/Level3"); 

font = Content.Load<SpriteFont>("SpriteFont1"); 

enemyIdleTexture = Content.Load<Texture2D>("Textures/enemy/idle");

enemyRunTexture = Content.Load<Texture2D>("Textures/enemy/run"); 

sound = Content.Load<SoundEffect>("Sound/GemCollected");

music = Content.Load<SoundEffect>("Sound/Music");

soundjump = Content.Load<SoundEffect>("Sound/PlayerJump");

newGame = Content.Load<SoundEffect>("Sound/NewGame");

PlayerKilled = Content.Load<SoundEffect>("Sound/PlayerKilled");

JumpEnemy = Content.Load<SoundEffect>("Sound/JumpEnemy"); 

menu.LoadContent(Content); 

Rectanglerect = newRectangle(0, Height - idleTexture.Height - 40, 60, 60);

hero = newAnimatedSprite(rect, idleTexture, runTexture, jumpTexture, this); 

CreateLevel();

// TODO: use this.Content to load your game content here

        } 

///<summary>

///UnloadContent will be called once per game and is the place to unload

/// all content.

///</summary>

protectedoverridevoidUnloadContent()

        {

// 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>

protectedoverridevoid Update(GameTimegameTime)

        {

// Allows the game to exit 

// TODO: Add your update logic here

if (gameState == GameState.Game)

UpdateGameLogic(gameTime);

elsemenu.Update();

base.Update(gameTime); 
 

        } 

privatevoidUpdateGameLogic(GameTimegameTime)

        {

KeyboardStatekeyState = Keyboard.GetState();

if (keyState.IsKeyDown(Keys.Escape))

gameState = GameState.Menu;

if (keyState.IsKeyDown(Keys.Space) &&oldState.IsKeyUp(Keys.Space))

{

countdownTimer = timeLevel;

Score = 0;

CreateLevel();

            } 

foreach (Gemgemin gems)

{

gem.Update(gameTime);

            } 

foreach (AnimatedSprite enemy in enemies)

            {

enemy.UpdateAI(gameTime);

            } 

if (keyState.IsKeyDown(Keys.Left))

hero.StartRun(false);

elseif (keyState.IsKeyDown(Keys.Right))

hero.StartRun(true);

elsehero.Stop(); 

if (keyState.IsKeyDown(Keys.Up))

hero.Jump();

if (keyState.IsKeyDown(Keys.Up) &&oldState.IsKeyUp(Keys.Up))

soundjump.Play(); 

RectangleheroScreenRect = GetScreenRect(hero.rect); 

if (heroScreenRect.Left< Width / 2)

Scroll(-3 * gameTime.ElapsedGameTime.Milliseconds / 10);

if (heroScreenRect.Left> Width / 2)

Scroll(3 * gameTime.ElapsedGameTime.Milliseconds / 10); 

oldState = keyState; 

hero.Update(gameTime);

inti = 0;

RectangleboundingRect = hero.rect;

while (i<gems.Count)

            {

if (gems[i].Rect.Intersects(boundingRect))

                {

gems.RemoveAt(i);

                    Score += 10;

sound.Play();

} 

else i++;

            } 

Информация о работе Создание 2D игры по средствам XNAGameStudio 4.0