2007年5月21日月曜日

あなたの最初のゲーム(3)


前回に引き続き、ステップ5です。

Window 上のスプライト表示されている小さい画像が移動し、Window の枠にあたると跳ね返ります。





ステップ5: スプライトが移動し、飛び跳ねるようにしてください。
次のように、Updateメソッドの中と上にコードを変えてください:

C#

 // スプライトの動きに関する情報を格納してください。
 Vector2 spriteSpeed = new Vector2( 50.0f, 50.0f );

 protected override void Update( GameTime gameTime )
 {
  // デフォルトゲームがXbox360とWindowsで出るのを許容します。
  if (GamePad.GetState( PlayerIndex.One ).Buttons.Back ==
                            ButtonState.Pressed)
   this.Exit();

  // 周囲でスプライトを動かしてください。
  UpdateSprite( gameTime );


  base.Update( gameTime );
 }

 void UpdateSprite( GameTime gameTime )
 {
  // 経過時間で計測された速度に従って、スプライトを動かしてください。
  spritePosition +=
   spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

  int MaxX = graphics.GraphicsDevice.Viewport.Width
          - myTexture.Width;
  int MinX = 0;
  int MaxY = graphics.GraphicsDevice.Viewport.Height
          - myTexture.Height;
  int MinY = 0;

  // 弾みをチェックしてください。
  if (spritePosition.X > MaxX)
  {
   spriteSpeed.X *= -1;
   spritePosition.X = MaxX;
  }
  else if (spritePosition.X < x =" MinX;"> MaxY)
  {
   spriteSpeed.Y *= -1;
   spritePosition.Y = MaxY;
  }
  else if (spritePosition.Y < y =" MinY;">

これは、各フレームでスプライトを動かして、もし、スプライトがゲームウィンドウの縁に行くなら、スプライトの動く方向を変える少しのロジックを加えます。

0 件のコメント: