Автор работы: Пользователь скрыл имя, 19 Декабря 2011 в 20:41, лабораторная работа
Цель
Научится создавать простые 2D игры по средствам Microsoft XNA – набора инструментов с управляемой средой времени выполнения (.NET).
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_
exitGame.Click += newEventHandler(exitGame_
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>
protectedoverridevoidLoadConte
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = newSpriteBatch(GraphicsDevice)
blockTexture1 = Content.Load<Texture2D>("
blockTexture2 = Content.Load<Texture2D>("
idleTexture = Content.Load<Texture2D>("
runTexture = Content.Load<Texture2D>("
jumpTexture = Content.Load<Texture2D>("
gemTexture = Content.Load<Texture2D>("
MenuWall = Content.Load<Texture2D>("
Level1 = Content.Load<Texture2D>("
Level2 = Content.Load<Texture2D>("
Level3 = Content.Load<Texture2D>("
font = Content.Load<SpriteFont>("
enemyIdleTexture = Content.Load<Texture2D>("
enemyRunTexture = Content.Load<Texture2D>("
sound = Content.Load<SoundEffect>("
music = Content.Load<SoundEffect>("
soundjump = Content.Load<SoundEffect>("
newGame = Content.Load<SoundEffect>("
PlayerKilled = Content.Load<SoundEffect>("
JumpEnemy = Content.Load<SoundEffect>("
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>
protectedoverridevoidUnloadCon
{
// 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(Gam
{
KeyboardStatekeyState = Keyboard.GetState();
if
(keyState.IsKeyDown(Keys.
gameState = GameState.Menu;
if
(keyState.IsKeyDown(Keys.
{
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.
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.
if (heroScreenRect.Left> Width / 2)
Scroll(3 * gameTime.ElapsedGameTime.
oldState = keyState;
hero.Update(gameTime);
inti = 0;
RectangleboundingRect = hero.rect;
while (i<gems.Count)
{
if
(gems[i].Rect.Intersects(
{
gems.RemoveAt(i);
Score += 10;
sound.Play();
}
else i++;
}
Информация о работе Создание 2D игры по средствам XNAGameStudio 4.0