Автор работы: Пользователь скрыл имя, 19 Декабря 2011 в 20:41, лабораторная работа
Цель
Научится создавать простые 2D игры по средствам Microsoft XNA – набора инструментов с управляемой средой времени выполнения (.NET).
if (gems.Count< 1)
{
currentLevel++;
if (currentLevel> 3)
currentLevel = 1;
CreateLevel();
}
foreach (AnimatedSprite enemy in enemies)
{
RectangleenemyBoundingRect = enemy.rect;
if
(enemyBoundingRect.Intersects(
{
Score = 0;
countdownTimer = timeLevel;
PlayerKilled.Play();
CreateLevel();
}
}
}
///<summary>
///This is called when the game should draw itself.
///</summary>
///<param name="gameTime">Provides a snapshot of timing values.</param>
protectedoverridevoid Draw(GameTimegameTime)
{
GraphicsDevice.Clear(Color.
if (currentLevel == 1)
{
spriteBatch.Begin(); //Начинаем прорисовку (рисование) изображений на экране.
spriteBatch.Draw(Level1, newVector2(0, 0), Color.White); //Рисуемпервыйслой
spriteBatch.End();//
}
if (currentLevel == 2)
{
spriteBatch.Begin(); //Начинаем прорисовку (рисование) изображений на экране.
spriteBatch.Draw(Level2, newVector2(0, 0), Color.White); //Рисуемпервыйслой
spriteBatch.End();//
}
if (currentLevel == 3)
{
spriteBatch.Begin(); //Начинаем прорисовку (рисование) изображений на экране.
spriteBatch.Draw(Level3, newVector2(0, 0), Color.White); //Рисуемпервыйслой
spriteBatch.End();//
}
if (gameState == GameState.Menu)
{
spriteBatch.Begin(); //Начинаем прорисовку (рисование) изображений на экране.
spriteBatch.Draw(MenuWall, newVector2(0, 0), Color.White); //Рисуемпервыйслой
spriteBatch.End();//
}
if (gameState == GameState.Game)
{
spriteBatch.Begin();
spriteBatch.DrawString(font, "Time: " + (countdownTimer), newVector2(700.0f, 0f), Color.White);
countdownTimer -= (gameTime.ElapsedGameTime.
if (countdownTimer< 0)
{
CreateLevel();
countdownTimer = timeLevel;
}
spriteBatch.End();
}
// TODO: Add your drawing code here
if
(gameState == GameState.Game)
DrawGame();
elsemenu.Draw(spriteBatch);
base.Draw(gameTime);
}
privatevoidDrawGame()
{
spriteBatch.Begin();
foreach (Blockblockin blocks)
{
block.Draw(spriteBatch);
}
foreach (Gemgemin gems)
{
gem.Draw(spriteBatch);
}
spriteBatch.End();
foreach (AnimatedSprite enemy in enemies)
{
enemy.Draw(spriteBatch);
}
hero.Draw(spriteBatch);
spriteBatch.Begin();
spriteBatch.DrawString(font, "Score: " + Score, Vector2.Zero, Color.White);
spriteBatch.End();
}
}
enumGameState
{
Game,
Menu
}
}
Модуль AnimatedSprite.cs
using System;
usingSystem.Collections.
usingSystem.Linq;
usingSystem.Text;
usingMicrosoft.Xna.Framework;
usingMicrosoft.Xna.Framework.
usingMicrosoft.Xna.Framework.
usingMicrosoft.Xna.Framework.
namespaceLevelGame
{
classAnimatedSprite
{
publicRectanglerect;
Texture2D idle;
Texture2D run;
Texture2D jump;
boolisRunning;
boolisSlowMode;
boolisRunningRight;
boolisJumping;
floatyVelocity;
floatmaxYVelocity = 10;
float
g = 0.3f;
intframeWidth;
intframeHeight;
inttimeForJump
= (int)(2000*random.NextDouble()
staticRandomrandom
= newRandom();
publicint Frames
{
get
{
returnrun.Width / frameWidth;
}
}
intcurrentFrame;
inttimeElapsed;
inttimeForFrame
= 100;
Game1
game;
publicAnimatedSprite(Rectangle
{
this.rect = rect;
this.idle = idle;
this.run = run;
this.jump
= jump;
frameWidth = frameHeight = run.Height;
this.game = game;
}
publicvoidSwitchModes()
{
isSlowMode = !isSlowMode;
}
publicvoidStartRun(boolisRight
{
if (!isRunning)
{
isRunning = true;
currentFrame = 0;
timeElapsed = 0;
}
isRunningRight = isRight;
}
publicvoid Stop()
{
isRunning = false;
}
publicvoid Jump()
{
if (!isJumping&&yVelocity == 0.0f)
{
isJumping = true;
currentFrame = 0;
timeElapsed = 0;
yVelocity = maxYVelocity;
}
}
publicvoidApplyGravity(GameTim
{
yVelocity = yVelocity - g * gameTime.ElapsedGameTime.
floatdy
= yVelocity * gameTime.ElapsedGameTime.
RectanglenextPosition = rect;
nextPosition.Offset(0, -(int)dy);
RectangleboudingRect = GetBoundingRect(nextPosition);
if
(boudingRect.Top> 0 &&boudingRect.Bottom<game.
&& !game.CollidesWithLevel(
rect = nextPosition;
boolcollideOnFallDown
= (game.CollidesWithLevel(
if
(boudingRect.Bottom>game.
{
yVelocity = 0;
isJumping = false;
}
}
publicvoidUpdateAI(GameTimegam
{
timeElapsed += gameTime.ElapsedGameTime.
inttempTime = timeForFrame;
if (isSlowMode)
tempTime *= 4;
Информация о работе Создание 2D игры по средствам XNAGameStudio 4.0