Автор работы: Пользователь скрыл имя, 19 Декабря 2011 в 20:41, лабораторная работа
Цель
Научится создавать простые 2D игры по средствам Microsoft XNA – набора инструментов с управляемой средой времени выполнения (.NET).
if (timeElapsed>tempTime)
{
currentFrame = (currentFrame + 1) % Frames;
timeElapsed = 0;
}
if (isRunning)
{
int
dx = (int)((1.5f) * gameTime.ElapsedGameTime.
if (!isRunningRight)
dx = -dx;
RectanglenextPosition = rect;
nextPosition.Offset(dx, 0);
RectangleboudingRect = GetBoundingRect(nextPosition);
//
if (boudingRect.Left< 0 || boudingRect.Right>game.
// isRunningRight= !isRunningRight;
//
else if (game.CollidesWithLevel(
// isRunningRight= !isRunningRight;
//
else if (game.WillFallDown(
// isRunningRight= !isRunningRight;
// else rect = nextPosition;
if
(!game.CollidesWithLevel(
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.
if (timeForJump<= 0)
{
Jump();
game.JumpEnemy.Play();
timeForJump = (int)(2000 * random.NextDouble() + 3000);
}
ApplyGravity(gameTime);
}
publicvoid Update(GameTimegameTime)
{
timeElapsed += gameTime.ElapsedGameTime.
inttempTime = timeForFrame;
if (isSlowMode)
tempTime *= 4;
if (timeElapsed>tempTime)
{
currentFrame = (currentFrame + 1) % Frames;
timeElapsed = 0;
}
if (isRunning)
{
int
dx = 3 * gameTime.ElapsedGameTime.
if (!isRunningRight)
dx = -dx;
RectanglenextPosition = rect;
nextPosition.Offset(dx, 0);
RectangleboudingRect = GetBoundingRect(nextPosition);
RectanglescreenRect = Game1.GetScreenRect(
if (screenRect.Left> 0 &&screenRect.Right<game.Width
&& !game.CollidesWithLevel(
rect = nextPosition;
}
ApplyGravity(gameTime);
}
privateRectangleGetBoundingRec
{
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.
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.
usingSystem.Linq;
usingSystem.Text;
usingMicrosoft.Xna.Framework;
usingMicrosoft.Xna.Framework.
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.
usingSystem.Linq;
usingSystem.Text;
usingMicrosoft.Xna.Framework;
usingMicrosoft.Xna.Framework.
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.
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.
usingSystem.Linq;
usingSystem.Text;
usingLevelGame.MenuSystem;
usingMicrosoft.Xna.Framework.
usingMicrosoft.Xna.Framework.
usingMicrosoft.Xna.Framework.
usingMicrosoft.Xna.Framework;
namespaceLevelGame
{
classMenu
{
publicList<MenuItem> Items { get; set; }
SpriteFont font;
intcurrentItem;
KeyboardStateoldState;
public Menu()
{
Items = newList<MenuItem>();
Информация о работе Создание 2D игры по средствам XNAGameStudio 4.0