XNA Framework, , :
Microsoft.Xna.Framework.Game Component
Microsoft.Xna.Framework.Drawable Game Component.
, (Update), (Draw), (Load Content) (Initia lization) . .. , , XNA. , , , , .
, , () , , , , FPS (Frame Per Second) .
.
. . animation . , , , , - , . ; , .
(. 2) , ( ) .
. 2. ,
24 ( ) . (). , , () ( , ).
, . . , , , . , , . , , .. , .. . , , .
. . :
|
|
1. , ( gif-, , Microsoft GIF Animator).
2. , 2D (, Adobe Flash CS4, Synfig).
3. , 3D (, Autodesk 3ds Max, Blender).
2. 3, .. gif- . , . (tweening) - , , . , .
() , . , 1 , 100 . . (. 3). . , . , , ( , , ).
. 3. 1- () 100- ().
.
, (. specialeffect, . SPFX, SFX FX) , , , (, ).
, (, ). (, , ).
. ( ), . () . , , . .
, , .
2D . 4 , 4 .png, 4 , . 4 5.
|
|
. 4 .
. 5 .
. , Actor. .
. , , , , .
public Actor(string textureFolder, ContentManager cont, int countFrames, int hp, int speed, Physics gamePhysics)
{
this.content = cont;
this.DownWalk = cont.Load<Texture2D>(textureFolder + @"/downMove");
this.UpWalk = cont.Load<Texture2D>(textureFolder + @"/upMove");
this.LeftWalk = cont.Load<Texture2D>(textureFolder + @"/leftMove");
this.RightWalk = cont.Load<Texture2D>(textureFolder + @"/rightMove");
this.countFrames = countFrames;
Width = DownWalk.Width / countFrames; //
Height = DownWalk.Height; //
Position = Vector2.Zero;
Move = Vector2.Zero;
sourceRect = new Rectangle(0, 0, Width, Height);
this.hp = hp;
this.Speed = speed;
this.gamePhysics = gamePhysics;
//
StopMove = Vector4.Zero;
}
heroAnimation( ), MoveUp, MoveDown, MoveLeft, MoveRight 4 DrawMotion( ).
public void heroAnimation(float elapsedTime) //
{
elapsed += elapsedTime; //
if (elapsed >= delay) //
{
if (frame == countFrames - 1)
frame = 0;
else
frame++; //
elapsed = 0; //
}
sourceRect.X = frame * Width; //
}
public void MoveUp()
{
Move.Y = -1; //
View.X = 0; //
View.Y = -1;
if (StopMove.Z == 0)
{
StopMove.Y = 0;
Position.Y -= Speed; //
}
}
public void MoveDown()
{
Move.Y = 1; //
View.X = 0; //
View.Y = 1;
if (StopMove.Y == 0)
{
StopMove.Z = 0;
Position.Y += Speed; //
}
}
public void MoveLeft()
{
Move.X = -1; //
View.X = -1; //
View.Y = 0;
if (StopMove.W == 0)
{
StopMove.X = 0;
Position.X -= Speed; //
}
}
public void MoveRight()
{
Move.X = 1; //
View.X = 1; //
View.Y = 0;
if (StopMove.X == 0)
{
StopMove.W = 0;
Position.X += Speed; //
}
}
public void DrawMotion(SpriteBatch spriteBatch)
{
float layer = 0.8f + (Position.Y + Height) / 5400;
float scale = 1;
Rectangle zeroRec = new Rectangle(0, 0, Width, Height);
if (Move.Y < 0) //
spriteBatch.Draw(UpWalk, Position, sourceRect, Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, layer);
else if (Move.Y == 0 && View.Y < 0) //
spriteBatch.Draw(UpWalk, Position, zeroRec, Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, layer);
else if (Move.Y > 0) //
spriteBatch.Draw(DownWalk, Position, sourceRect, Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, layer);
else if ((Move.Y == 0 && View.Y > 0) || (View.X == 0 && View.Y == 0)) //
spriteBatch.Draw(DownWalk, Position, zeroRec, Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, layer);
else if (Move.X > 0) //
spriteBatch.Draw(RightWalk, Position, sourceRect, Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, layer);
else if (Move.X == 0 && View.X > 0) //
spriteBatch.Draw(RightWalk, Position, zeroRec, Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, layer);
|
|
else if (Move.X < 0) //
spriteBatch.Draw(LeftWalk, Position, sourceRect, Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, layer);
else if (Move.X == 0 && View.X < 0) //
spriteBatch.Draw(LeftWalk, Position, zeroRec, Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, layer);
foreach (Bullet b in listBullet) //
{ b.DrawBullet(spriteBatch); }
}
Game1. . 6 .
. 6 .
. , . ( - , , ) (, ), , , - ( ). - (CSS) (JavaScript) , Flash (-).
1. http://edugalaxy.intel.ru/?automodule=blog&blogid=25&showentry=4235
2. http://kaltan.ucoz.com/load/grafika/skachat_toon_boom_animation_ish_1_2_na_russkom/10-1-0-1029
3. www.klyaksa.net
4. www.it-n.ru
5. www.allbest.ru
6. www.alleng.ru
7. www.orakul.spb.ru
8. www.markbook.chat.ru
9. ru.wikipedia.org/wiki/_
10. .. . , : , 2008 .
11. .. . , .: , 2008 .
12. .. , .: , 2008 .
A - Actor:
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Game1
{
class Actor
{
public Texture2D DownWalk { get; set; }
public Texture2D RightWalk { get; set; }
public Texture2D LeftWalk { get; set; }
public Texture2D UpWalk { get; set; }
public int intersect;
public Vector2 Position; //
public List<Bullet> listBullet = new List<Bullet>(); //
public int countFrames; //
public int Width; //
public int Height; //
public Vector2 Move; //
public Vector2 View; //
public int Speed; //
public Vector4 StopMove; //
public Rectangle sourceRect; //
public float elapsed;
public float delay = 200f;
public int frame = 0; //
public int hp;
ContentManager content;
Physics gamePhysics;
public Rectangle zBounds // Z -
{
get { return new Rectangle((int)Position.X, (int)Position.Y, Width, Height); }
}
public Rectangle shadow //
{ get { return new Rectangle((int)Position.X, (int)Position.Y + Height / 2, Width, Height / 2); } }
|
|
public Point pointN
{ get { return new Point(shadow.Center.X, shadow.Top); } }
public Point pointS
{ get { return new Point(shadow.Center.X, shadow.Bottom); } }
public Point pointW
{ get { return new Point(shadow.Left, shadow.Center.Y); } }
public Point pointE
{ get { return new Point(shadow.Right, shadow.Center.Y); } }
public Point pointNE
{ get { return new Point(shadow.Right, shadow.Top); } }
public Point pointNW
{ get { return new Point(shadow.Left, shadow.Top); } }
public Point pointSE
{ get { return new Point(shadow.Right, shadow.Bottom); } }
public Point pointSW
{ get { return new Point(shadow.Left, shadow.Bottom); } }
public Actor(string textureFolder, ContentManager cont, int countFrames, int hp, int speed, Physics gamePhysics)
{
this.content = cont;
this.DownWalk = cont.Load<Texture2D>(textureFolder + @"/downMove");
this.UpWalk = cont.Load<Texture2D>(textureFolder + @"/upMove");
this.LeftWalk = cont.Load<Texture2D>(textureFolder + @"/leftMove");
this.RightWalk = cont.Load<Texture2D>(textureFolder + @"/rightMove");
this.countFrames = countFrames;
Width = DownWalk.Width / countFrames; //
Height = DownWalk.Height; //
Position = Vector2.Zero;
Move = Vector2.Zero;
sourceRect = new Rectangle(0, 0, Width, Height);
this.hp = hp;
this.Speed = speed;
this.gamePhysics = gamePhysics;
//
StopMove = Vector4.Zero;
}
public void heroAnimation(float elapsedTime) //
{
elapsed += elapsedTime; //
if (elapsed >= delay) //
{
if (frame == countFrames - 1)
frame = 0;
else
frame++; //
elapsed = 0; //
}
sourceRect.X = frame * Width; //
}
public void MoveUp()
{
Move.Y = -1; //
View.X = 0; //
View.Y = -1;
if (StopMove.Z == 0)
{
StopMove.Y = 0;
Position.Y -= Speed; //
}
}
public void MoveDown()
{
Move.Y = 1; //
View.X = 0; //
View.Y = 1;
if (StopMove.Y == 0)
{
StopMove.Z = 0;
Position.Y += Speed; //
}
}
public void MoveLeft()
{
Move.X = -1; //
View.X = -1; //
View.Y = 0;
if (StopMove.W == 0)
{
StopMove.X = 0;
Position.X -= Speed; //
}
}
public void MoveRight()
{
Move.X = 1; //
View.X = 1; //
View.Y = 0;
if (StopMove.X == 0)
{
StopMove.W = 0;
Position.X += Speed; //
}
}
public void DrawMotion(SpriteBatch spriteBatch)
{
float layer = 0.8f + (Position.Y + Height) / 5400;
float scale = 1;
Rectangle zeroRec = new Rectangle(0, 0, Width, Height);
if (Move.Y < 0) //
spriteBatch.Draw(UpWalk, Position, sourceRect, Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, layer);
else if (Move.Y == 0 && View.Y < 0) //
spriteBatch.Draw(UpWalk, Position, zeroRec, Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, layer);
else if (Move.Y > 0) //
spriteBatch.Draw(DownWalk, Position, sourceRect, Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, layer);
else if ((Move.Y == 0 && View.Y > 0) || (View.X == 0 && View.Y == 0)) //
spriteBatch.Draw(DownWalk, Position, zeroRec, Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, layer);
else if (Move.X > 0) //
spriteBatch.Draw(RightWalk, Position, sourceRect, Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, layer);
else if (Move.X == 0 && View.X > 0) //
spriteBatch.Draw(RightWalk, Position, zeroRec, Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, layer);
else if (Move.X < 0) //
spriteBatch.Draw(LeftWalk, Position, sourceRect, Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, layer);
else if (Move.X == 0 && View.X < 0) //
spriteBatch.Draw(LeftWalk, Position, zeroRec, Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, layer);
foreach (Bullet b in listBullet) //
{ b.DrawBullet(spriteBatch); }
}
public void Shoot(List<Bullet> listBullet)
{
//
Bullet newBullet = new Bullet(content.Load<Texture2D>(@"Textures/fireball"), this); // H
|
|
//
if (View.X == 0 && View.Y == 0)
newBullet.Velocity = new Vector2(0, 7);
else
newBullet.Velocity = View * 7f;
if (View.X > 0)
{
newBullet.Position.X = Position.X + Width;
newBullet.Position.Y = Position.Y + Height / 2;
}
else if (View.X < 0)
{
newBullet.Position.X = Position.X;
newBullet.Position.Y = Position.Y + Height / 2;
}
else if (View.Y > 0 || View.X == View.Y)
{
newBullet.Position.X = Position.X + Width / 2;;
newBullet.Position.Y = Position.Y + Height;
}
else if (View.Y < 0)
{
newBullet.Position.X = Position.X + Width / 2;
newBullet.Position.Y = Position.Y;
}
//
listBullet.Add(newBullet);
}
void moveBullet(List<Bullet> listBullet, Decor dekor, List<Actor> listActor)
{
foreach (Bullet b in listBullet) //
{
b.Position += b.Velocity; //
if (Vector2.Distance(b.Position, Position) > 400)
b.isAlive = false;
if (gamePhysics.contactBulletDekor(b, dekor))
b.isAlive = false;
gamePhysics.contactBulletActor(b, listActor);
}
for (int i = 0; i < listBullet.Count; i++)
{
if (!listBullet[i].isAlive && listBullet[i].isVisible)
{
listBullet.RemoveAt(i);
i--;
}
}
}
public void hunt(Actor a)
{
}
}
}
- Game1:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Game1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1: Microsoft.Xna.Framework.Game
{
private Menu menu;
private Physics physics;
private ContentManager contentManager;
private GraphicsDeviceManager graphics;
private SpriteBatch spriteBatch;
private Rectangle _viewPortRectangle; //
private Physics gamePhysics = new Physics();
private Actor Hero; //
private Field[] level = new Field[5];
private int currentLevel = 1;
private KeyboardState pastKey; //
private GameState gameState = GameState.Menu;
public int[,] Layer;
private Texture2D hp;
private Texture2D hp1;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 1080; //
graphics.PreferredBackBufferHeight = 640; //
//graphics.IsFullScreen = true;
}
/// <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>
protected override void Initialize()
{
menu = new Menu();
MenuItem newGame = new MenuItem("Start Game");
MenuItem resumeGame = new MenuItem("Resume Game");
MenuItem exitGame = new MenuItem("Exit");
resumeGame.Active = false;
newGame.Click += new EventHandler(newGame_Click);
resumeGame.Click += new EventHandler(resumeGame_Click);
exitGame.Click += new EventHandler(exitGame_Click);
menu.Items.Add(newGame);
menu.Items.Add(resumeGame);
menu.Items.Add(exitGame);
base.Initialize();
}
private void exitGame_Click(object sender, EventArgs e)
{
this.Exit();
}
private void resumeGame_Click(object sender, EventArgs e)
{
gameState = GameState.Game;
}
private void newGame_Click(object sender, EventArgs e)
{
menu.Items[1].Active = true;
gameState = GameState.Game;
Hero = new Actor("Hero", Content, 4, 2, 3, gamePhysics);
for (int i = 1; i < 5; i++)
{
level[i] = new Field(gamePhysics, Hero);
level[i].LoadField(Content, "level" + i.ToString());
}
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
hp = Content.Load<Texture2D>(@"Textures/hp");
hp1 = Content.Load<Texture2D>(@"Textures/hp1");
//
_viewPortRectangle = new Rectangle(0, 0,
graphics.GraphicsDevice.Viewport.Width,
graphics.GraphicsDevice.Viewport.Height);
menu.LoadContent(Content);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
}
/// <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>
protected override void Update(GameTime gameTime)
{
if (gameState == GameState.Game)
UpdateGameLogic(gameTime);
else menu.Update();
base.Update(gameTime);
}
private void UpdateGameLogic(GameTime gameTime)
{
if (level[currentLevel].fieldEmpty() == true)
{
Hero.Position.X = MathHelper.Clamp(Hero.Position.X, -Hero.Width,
_viewPortRectangle.Width + Hero.Width);
Hero.Position.Y = MathHelper.Clamp(Hero.Position.Y, -Hero.Height,
_viewPortRectangle.Height + Hero.Height);
if (currentLevel == 1)
{
currentLevel = 1;
}
/*1-->2
000*/
if (Hero.Position.X > 1080 && currentLevel == 1)
{
currentLevel = 3;
Hero.Position.X = 0;
}
/*1-->2
000*/
if (Hero.Position.X < -Hero.Width + 10 && currentLevel == 3)
{
currentLevel = 1;
Hero.Position.X = 1080 - Hero.Width;
}
/*1-->2
020*/
if (Hero.Position.Y > 640 && currentLevel == 1)
{
currentLevel = 4;
Hero.Position.Y = 0;
}
/*1-->2
010*/
if (Hero.Position.Y < -Hero.Height + 10 && currentLevel == 4)
{
currentLevel = 1;
Hero.Position.Y = 640 - Hero.Height;
}
/*1-->2
012*/
if (Hero.Position.X > 1080 && currentLevel == 4)
{
currentLevel = 2;
Hero.Position.X = 0;
}
/*1-->2
021*/
if (Hero.Position.X < -Hero.Width + 10 && currentLevel == 2)
{
currentLevel = 4;
Hero.Position.X = 1080 - Hero.Width;
}
/*1-->2
002*/
if (Hero.Position.Y > 640 && currentLevel == 3)
{
currentLevel = 2;
Hero.Position.Y = 0;
}
/*1-->2
001*/
if (Hero.Position.Y < -Hero.Height + 10 && currentLevel == 2)
{
currentLevel = 3;
Hero.Position.Y = 640 - Hero.Height;
}
}
else
{
Hero.Position.X = MathHelper.Clamp(Hero.Position.X, 0, _viewPortRectangle.Width - Hero.Width);
Hero.Position.Y = MathHelper.Clamp(Hero.Position.Y, 0, _viewPortRectangle.Height - Hero.Height);
}
level[currentLevel].UpdateField(gameTime); //
control(); //
if (Hero.hp == 0)
gameState = GameState.Menu;
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
if (gameState == GameState.Game)
DrawGame();
else menu.Draw(spriteBatch);
base.Draw(gameTime);
}
private void DrawGame()
{
spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
level[currentLevel].DrawField(spriteBatch); //
if (Hero.hp == 2)
spriteBatch.Draw(hp, new Vector2(50, 50), null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
else if (Hero.hp == 1)
spriteBatch.Draw(hp1, new Vector2(50, 50), null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
spriteBatch.End();
}
public void control()
{
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
gameState = GameState.Menu;
if (Keyboard.GetState().IsKeyUp(Keys.W) || Keyboard.GetState().IsKeyUp(Keys.S))
Hero.Move.Y = 0;
if (Keyboard.GetState().IsKeyUp(Keys.D) || Keyboard.GetState().IsKeyUp(Keys.A))
Hero.Move.X = 0;
if (Keyboard.GetState().IsKeyDown(Keys.D)) //
{
Hero.MoveRight();
}
if (Keyboard.GetState().IsKeyDown(Keys.A)) //
{
Hero.MoveLeft();
}
if (Keyboard.GetState().IsKeyDown(Keys.W)) //
{
Hero.MoveUp();
}
if (Keyboard.GetState().IsKeyDown(Keys.S)) //
{
Hero.MoveDown();
}
if (Keyboard.GetState().IsKeyDown(Keys.E) && pastKey.IsKeyUp(Keys.E)) //W -
Hero.Shoot(Hero.listBullet);
if (Keyboard.GetState().IsKeyDown(Keys.Q) && pastKey.IsKeyUp(Keys.Q))
{
Hero.hp--;
hp = null;
}
if (Keyboard.GetState().IsKeyDown(Keys.Space) && pastKey.IsKeyUp(Keys.Space))
{
level[currentLevel].AddEnemy();
}
pastKey = Keyboard.GetState();
}
private enum GameState
{
Game,
Menu
}
}
}