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

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

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

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

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

Отчет.doc

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

if (timeElapsed>tempTime)

            {

currentFrame = (currentFrame + 1) % Frames;

timeElapsed = 0;

            } 

if (isRunning)

            {

int dx = (int)((1.5f) * gameTime.ElapsedGameTime.Milliseconds / 15);

if (!isRunningRight)

dx = -dx; 

RectanglenextPosition = rect;

nextPosition.Offset(dx, 0); 

RectangleboudingRect = GetBoundingRect(nextPosition); 

//       if (boudingRect.Left< 0 || boudingRect.Right>game.levelLength)

//         isRunningRight= !isRunningRight;

//      else if (game.CollidesWithLevel(boudingRect))

//          isRunningRight= !isRunningRight;

//      else if (game.WillFallDown(boundingRect))

//          isRunningRight= !isRunningRight;

//      else rect = nextPosition;

if (!game.CollidesWithLevel(boudingRect))

rect = nextPosition; 
 

            }

if (game.GetHeroRect().Left <rect.Left)

StartRun(false);

if (game.GetHeroRect().Left >rect.Left)

StartRun(true);

if (game.GetHeroRect().Left == rect.Left)

Stop(); 

timeForJump -= gameTime.ElapsedGameTime.Milliseconds;

if (timeForJump<= 0)

                    {

Jump();

game.JumpEnemy.Play();

timeForJump = (int)(2000 * random.NextDouble() + 3000);

} 

ApplyGravity(gameTime); 
 
 

        }

publicvoid Update(GameTimegameTime)

        {

timeElapsed += gameTime.ElapsedGameTime.Milliseconds;

inttempTime = timeForFrame;

if (isSlowMode)

tempTime *= 4;

if (timeElapsed>tempTime)

            {

currentFrame = (currentFrame + 1) % Frames;

timeElapsed = 0;

            } 

if (isRunning)

            {

int dx = 3 * gameTime.ElapsedGameTime.Milliseconds / 10;

if (!isRunningRight)

dx = -dx; 

RectanglenextPosition = rect;

nextPosition.Offset(dx, 0); 

RectangleboudingRect = GetBoundingRect(nextPosition);

RectanglescreenRect = Game1.GetScreenRect(boudingRect); 

if (screenRect.Left> 0 &&screenRect.Right<game.Width

&& !game.CollidesWithLevel(boudingRect))

rect = nextPosition;

            } 

ApplyGravity(gameTime);

        } 

privateRectangleGetBoundingRect(Rectangle rectangle)

        {

int width = (int)(rectangle.Width * 0.4f);

int x = rectangle.Left + (int)(rectangle.Width * 0.2f); 

returnnewRectangle(x, rectangle.Top, width, rectangle.Height);

        } 

publicvoid Draw(SpriteBatchspriteBatch)

        {

Rectangle r = newRectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);

SpriteEffects effects = SpriteEffects.None;

if (isRunningRight)

effects = SpriteEffects.FlipHorizontally; 

RectanglescreenRect = Game1.GetScreenRect(rect);

spriteBatch.Begin();

if (isJumping)

            {

spriteBatch.Draw(jump, screenRect, r, Color.White, 0, Vector2.Zero, effects, 0);

            }

else

if (isRunning)

            {

spriteBatch.Draw(run, screenRect, r, Color.White, 0, Vector2.Zero, effects, 0);

            }

else

            {

spriteBatch.Draw(idle, screenRect, Color.White);

            }

spriteBatch.End();

        } 

publicboolboundingRect { get; set; } 
 

    }

} 
 
 

МодульBlock.cs

using System;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingMicrosoft.Xna.Framework;

usingMicrosoft.Xna.Framework.Graphics; 

namespaceLevelGame

{

classBlock

    {

publicRectanglerect;

Texture2D texture; 

public Block(Rectanglerect, Texture2D texture)

        {

this.rect = rect;

this.texture = texture;

        } 

publicvoid Draw(SpriteBatchspriteBatch)

        {

RectanglescreenRect = Game1.GetScreenRect(rect);

spriteBatch.Draw(texture, screenRect, Color.White);

        }

    }

} 
 

МодульGem.cs

using System;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingMicrosoft.Xna.Framework;

usingMicrosoft.Xna.Framework.Graphics; 

namespaceLevelGame

{

classGem

    { 

publicRectangleRect{get; set;}

Texture2D texture;

Game1 game;

intdy; 

public Gem(Rectanglerect, Texture2D texture, Game1 game)

        {

this.Rect = rect;

this.texture = texture;

this.game = game;

        } 

publicvoid Update(GameTimegameTime)

        {

float t = (float)gameTime.TotalGameTime.TotalSeconds * 3+Rect.X;

dy = (int)(Math.Sin(t) * 10); 

        } 

publicvoid Draw(SpriteBatchspriteBatch)

        {

Rectangle r = newRectangle(Rect.X, Rect.Y+dy, Rect.Width, Rect.Height);

RectanglescreenRect = Game1.GetScreenRect(r);

spriteBatch.Draw(texture, screenRect, Color.White); 

        }

    }

} 
 

МодульProgram.cs

using System; 

namespaceLevelGame

{

staticclassProgram

    {

///<summary>

///The main entry point for the application.

///</summary>

staticvoid Main(string[] args)

        {

using (Game1 game = newGame1())

            {

game.Run();

            }

        }

    }

} 
 
 

МодульMenu.cs

using System;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingLevelGame.MenuSystem;

usingMicrosoft.Xna.Framework.Graphics;

usingMicrosoft.Xna.Framework.Content;

usingMicrosoft.Xna.Framework.Input;

usingMicrosoft.Xna.Framework; 

namespaceLevelGame

{

classMenu

    {

publicList<MenuItem> Items { get; set; }

SpriteFont font; 

intcurrentItem;

KeyboardStateoldState; 

public Menu()

        {

            Items = newList<MenuItem>();

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