Microsoft XNA:段階的なアルカノイド

数日前、偉大で強力なインターネットの広大な広がりを耕し、Microsoft XNA Studioに出会いました。 初めてこのフレームワークについて聞いたわけではありませんが、以前のすべての時間はどういうわけか通り過ぎましたが、理解する時間はまったくありませんでした。

今回、何かが私をより深く掘り下げた。 ライブラリを知るために何かを実装するよりも良い方法はないと判断し、自由な夜を過ごすために、私は単純なものを書くことにしました。例えば、自分の興味のためではなく、知り合いのために、子供時代の私のお気に入りのアルカノイド(ブリックアウト)を書くことにしました。



これはハーバーに関する私の最初の記事です。



この記事の意味は、XNAを使用してゲームの作成を開始することがいかに簡単かを示すことであり、実際、このプラットフォームをさらに研究するためのインセンティブを提供することです。 したがって、それは本格的なゲームのふりをするものではなく、グラフィックは私からのアーティストのようなものではありません。 物理学はより現実的かもしれませんが、これはこの記事の範囲を超えています。ゲーム開発に興味がある人は、ネットワーク上で多くの価値のある(そうではない)資料を見つけると確信しています。



ソースはここからダウンロードできます







それでは、Microsoft XNAとは正確には何ですか?



Microsoft XNAは、Microsoft Managed Runtime Environmentでマルチプラットフォームの2Dおよび3Dゲームを開発するためのツールキットおよびライブラリです。 サポートされるプラットフォームは、Windows、Microsoft Xbox 360、およびMicrosoft Zuneです。 理論的には、任意の.Net言語、任意のIDEで記述できますが、正式にサポートされているのはC#とXNA Game Studio ExpressおよびVisual Studio 2005以降のすべてのバージョンのみです。 XNAは、最小限の変更でサポートされているプラ​​ットフォームにゲームを移植する機能も提供します。



執筆時点での最新バージョンは、 Microsoft XNA Game Studio 3.1(73.2 MB)です。



プロジェクト作成





新しいプロジェクトを作成しましょう-XNA Game Studio 3.1-Windowsゲーム(3.1)



フォトバケット



ウィザードはゲームのスケルトンを作成します:







私たちにとって最大の関心事は、ゲームを開発するMicrosoft.Xna.Framework.Gameから継承されたGame1クラスを定義するGame1.csファイルです。

次のGameメソッドは、Game1クラスでオーバーライドされます。



void Initialize()-ゲームが開始する前にリソースを初期化するために一度呼び出されます

void LoadContent()-一度呼び出され、コンテンツ(スプライトなど)のロードに使用されます

void UnloadContent()-一度呼び出され、コンテンツのアップロードに使用されます

void Update(GameTime gameTime)-このメソッドは、ゲームの実際のロジック、衝突処理、キーボードまたはジョイスティックイベントの処理、オーディオの再生などを実装します。

void Draw(GameTime gameTime)-競技場を描くために呼び出されます。



現時点では、コンパイルされたゲームは次のようになります







コンテンツを追加する





ゲームリソースを追加します。この場合、レンガ、ラケット、ボールの背景画像-コンテンツ(右クリック)->追加->既存のアイテム...







Asset Nameプロパティに注意してください。これを使用して、さらにアニメーションを作成するために必要なTexture2Dオブジェクトを作成します。



競技場の背景を描く





競技場の背景の画像をダウンロードします。



private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .



  1. private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .



  2. private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .



  3. private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .



  4. private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .



  5. private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .



  6. private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .



  7. private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .



  8. private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .



  9. private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .



  10. private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .



  11. private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .



  12. private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .



private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .







背景の描画







  1. 保護された オーバーライド void Draw(GameTime gameTime)
  2. {
  3. GraphicsDevice.Clear(Color.CornflowerBlue);
  4. spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
  5. //背景を描きます
  6. spriteBatch.Draw(_background、_viewPortRectangle、Color.White);
  7. spriteBatch.End();
  8. base .Draw(gameTime);
  9. }
*このソースコードは、 ソースコードハイライターで強調表示されました。




SpriteBatch.Beginメソッドは、スプライトをレンダリングするためにグラフィックデバイスを準備し、SpriteBatch.Endは描画プロセスを完了し、デバイスを初期状態に戻します。 すべてのSpriteBatch.Drawメソッドは、SpriteBatch.Begin-SpriteBatch.Endで囲む必要があります。



ゲームオブジェクトを作成する





ゲームオブジェクトのいずれかをカプセル化するGameObjectクラスを作成しましょう。







  1. Microsoft.Xna.Framework を使用
  2. Microsoft.Xna.Framework を使用します。 グラフィックス
  3. 名前空間アルカノイド
  4. {
  5. パブリック クラス GameObject
  6. {
  7. public Texture2D Sprite { 取得 ; セット ; } //スプライト
  8. パブリック Vector2の位置。 //位置
  9. パブリック Vector2 Velocity; //速度
  10. public int Width { get { return Sprite.Width; }} //幅
  11. public int Height { get { return Sprite.Height; }} //高さ
  12. パブリック ブール IsAlive { get ; セット ; } //オブジェクトは生きているか
  13. public Rectangle Bounds //オブジェクト境界
  14. {
  15. 得る
  16. {
  17. 新しい Rectangle(( int )Position.X、( int )Position.Y、Width、Height);
  18. }
  19. }
  20. //水平軸に沿って動きを拡大
  21. public void ReflectHorizo​​ntal()
  22. {
  23. Velocity.Y = -Velocity.Y;
  24. }
  25. //垂直軸に沿って動きを拡大します
  26. public void ReflectVertical()
  27. {
  28. Velocity.X = -Velocity.X;
  29. }
  30. パブリック GameObject(Texture2Dスプライト)
  31. {
  32. スプライト=スプライト;
  33. IsAlive = true ;
  34. 位置= Vector2.Zero;
  35. 速度= Vector2.Zero;
  36. }
  37. }
  38. }
*このソースコードは、 ソースコードハイライターで強調表示されました。




ラケットのレンダリングとアニメーション





最初に、ラケットを表すオブジェクトを作成し、それを競技場の中央から下端から少し高く配置します。







  1. プライベート GameObject _paddle; //ラケット
  2. 保護された オーバーライド void LoadContent()
  3. {
  4. <...スキップ...>
  5. //ラケットを作成します。開始位置は競技場の中央、下端より高くなります
  6. _paddle = new GameObject(Content.Load <Texture2D>( @ "paddle" ));
  7. _paddle.Position = new Vector2((_ viewPortRectangle.Width-_paddle.Width)/ 2、
  8. _viewPortRectangle.Height-_paddle.Height-20);
  9. <...スキップ...>
  10. }
*このソースコードは、 ソースコードハイライターで強調表示されました。




画面にラケットを描く







  1. 保護された オーバーライド void Draw(GameTime gameTime)
  2. {
  3. <...スキップ...>
  4. spriteBatch.Draw(_paddle.Sprite、_paddle.Position、Color.White);
  5. <...スキップ...>
  6. }
*このソースコードは、 ソースコードハイライターで強調表示されました。




この段階で、アプリケーションをコンパイルすると、次のようになります。







ラケットをキーストロークに応答させると便利です。このため、次のコードをUpdateメソッドに追加します







  1. 保護された オーバーライド void更新(GameTime gameTime)
  2. {
  3. <...スキップ...>
  4. KeyboardState keyboardState = Keyboard.GetState();
  5. //ラケットを右に移動
  6. if (keyboardState.IsKeyDown(Keys.Right))
  7. _paddle.Position.X + = 6f;
  8. //ラケットを左に移動します
  9. if (keyboardState.IsKeyDown(Keys.Left))
  10. _paddle.Position.X-= 6f;
  11. //ラケットの動きを競技場に制限する
  12. _paddle.Position.X = MathHelper.Clamp(_paddle.Position.X、0、_viewPortRectangle.Width-_paddle.Width);
  13. <...スキップ...>
  14. }
*このソースコードは、 ソースコードハイライターで強調表示されました。




レンガの描画





実際に壊すブリックを表すGameObject配列を作成します







  1. private int _brickPaneWidth = 10; //幅で描画する糸杉の数
  2. private int _brickPaneHeight = 5; //高さで描画する糸杉の数
  3. プライベート Texture2D _brickSprite; //レンガのスプライト
  4. プライベート GameObject [、] _bricks; //レンガの配列
*このソースコードは、 ソースコードハイライターで強調表示されました。




LoadContent()メソッドに次のコードを追加します







  1. 保護された オーバーライド void LoadContent()
  2. {
  3. <...スキップ...>
  4. //ブリックの配列を作成します
  5. _brickSprite = Content.Load <Texture2D>( @ "brick" );
  6. _bricks = 新しい GameObject [_brickPaneWidth、_brickPaneHeight];
  7. forint i = 0; i <_brickPaneWidth; i ++)
  8. {
  9. forint j = 0; j <_brickPaneHeight; j ++)
  10. {
  11. _bricks [i、j] = 新しい GameObject(_brickSprite)
  12. {
  13. 位置= 新しい Vector2(i * 55 + 120、j * 25 + 100)
  14. };
  15. }
  16. }
  17. <...スキップ...>
  18. }
*このソースコードは、 ソースコードハイライターで強調表示されました。




レンガの配列を描画し、レンガが「生きている」場合、つまりレンダリングが行われます。 ボールで壊れない







  1. 保護された オーバーライド void Draw(GameTime gameTime)
  2. {
  3. <...スキップ...>
  4. //レンガを描く
  5. foreach (_bricksのvar brick)
  6. if (brick.IsAlive)
  7. spriteBatch.Draw(brick.Sprite、brick.Position、Color.White);
  8. <...スキップ...>
  9. }
*このソースコードは、 ソースコードハイライターで強調表示されました。




この段階での競技場は次のとおりです







ボール描画





ボールオブジェクトを作成する







  1. プライベート GameObject _ball; //ボール
  2. 保護された オーバーライド void LoadContent()
  3. {
  4. <...スキップ...>
  5. //ラケットの中央の初期位置であるボールを作成し、
  6. //方向を開始-右、上
  7. _ball = new GameObject(Content.Load <Texture2D>( @ "ball" ));
  8. _ball.Position = new Vector2((_ viewPortRectangle.Width-_ball.Width)/ 2、
  9. _viewPortRectangle.Height-_paddle.Height-_ball.Height-20);
  10. _ball.Velocity = new Vector2(3、-3);
  11. <...スキップ...>
  12. }
*このソースコードは、 ソースコードハイライターで強調表示されました。




ボールをアニメートするには、新しいUpdateBall()メソッドを追加し、Update()メソッドで呼び出します。 レンガとラケットとのボールの衝突を処理するために、今後このメソッドが必要になります







  1. プライベート void UpdateBall()
  2. {
  3. _ball.Position + = _ball.Velocity;
  4. }
  5. 保護された オーバーライド void更新(GameTime gameTime)
  6. {
  7. <...スキップ...>
  8. //ボールを移動します
  9. UpdateBall();
  10. <...スキップ...>
  11. }
*このソースコードは、 ソースコードハイライターで強調表示されました。




ボールを描くには、次のコードをDraw()メソッドに追加します







  1. 保護された オーバーライド void Draw(GameTime gameTime)
  2. {
  3. <...スキップ...>
  4. //ボールを描く
  5. spriteBatch.Draw(_ball.Sprite、_ball.Position、Color.White);
  6. <...スキップ...>
  7. }
*このソースコードは、 ソースコードハイライターで強調表示されました。




現時点では、ほぼ完全に終了した競技場がありますが、衝突処理がなければ、ボールはすぐに競技場から飛び出します。 競技場、レンガ、ラケットにボール衝突処理を追加します



衝突処理





オブジェクトの衝突の場所を決定し、ボールの飛行方向を変更する新しいメソッドを作成します。







  1. //衝突の側面を特定し、ボールの飛行の方向を反映します
  2. public void Collide(GameObject gameObject、Rectangle rect2)
  3. {
  4. //オブジェクトは上下に衝突し、水平方向の飛行方向を反映します
  5. if (rect2.Left <= gameObject.Bounds.Center.X && gameObject.Bounds.Center.X <= rect2.Right)
  6. gameObject.ReflectHorizo​​ntal();
  7. //左右に衝突したオブジェクト、飛行方向を垂直に反映
  8. else if (rect2.Top <= gameObject.Bounds.Center.Y && gameObject.Bounds.Center.Y <= rect2.Bottom)
  9. gameObject.ReflectVertical();
  10. }
*このソースコードは、 ソースコードハイライターで強調表示されました。




UpdateBall()メソッドに次のコードを追加します







  1. プライベート void UpdateBall()
  2. {
  3. //ボールの将来の位置。オブジェクトの表面にボールが「くっつく」のを防ぐ必要があります
  4. Rectangle nextRect = 新しい Rectangle(( int )(_ ball.Position.X + _ball.Velocity.X)、
  5. int )(_ ball.Position.Y + _ball.Velocity.Y)、
  6. _ball.Width、_ball.Height);
  7. //競技場の上端との衝突
  8. if (nextRect.Y <= 0)
  9. _ball.ReflectHorizo​​ntal();
  10. //ボールが競技場の下端に当たると、ボールは「死ぬ」
  11. if (nextRect.Y> = _viewPortRectangle.Height-nextRect.Height)
  12. {
  13. _ball.IsAlive = false ;
  14. }
  15. //競技場の左端または右端とボールの衝突
  16. if ((nextRect.X> = _viewPortRectangle.Width-nextRect.Width)|| nextRect.X <= 0)
  17. {
  18. _ball.ReflectVertical();
  19. }
  20. //ラケットとボールの衝突
  21. if (nextRect.Intersects(_paddle.Bounds))
  22. 衝突(_ball、_paddle.Bounds);
  23. //レンガと衝突ボール
  24. foreach (_bricksのvar brick)
  25. {
  26. if (nextRect.Intersects(brick.Bounds)&& brick.IsAlive)
  27. {
  28. brick.IsAlive = false ;
  29. 衝突(_ball、brick.Bounds);
  30. }
  31. }
  32. _ball.Position + = _ball.Velocity;
  33. }
*このソースコードは、 ソースコードハイライターで強調表示されました。




どうしたの







もちろん、ゲームには物理学はありません。控えめに言っても、ボールが動いているラケットと衝突すると、スティックすることがあります。 ただし、この記事の意味はMicrosoft XNA Game Studio環境に精通していることであり、このルーチンに対応しているため、開発者はゲームのロジックに集中することができます。



ソースはここからダウンロードできます



追加のリソース:

creators.xna.com

XNAWiki



PS。 招待してくれてありがとう。

PPS XNAに転送されたカルマをありがとう



テキストはHabraで準備されます



All Articles