2016-10-14 9 views
0

"クリーンな"コードに沿って何かを書こうとしています...今はフォームをベースにして、Pongゲームを作りたいと思います。 ゲームをきれいにクラスに分けたいと思っています。PictureBox(C#フォーム)のプロパティを制御するためのクラス

私はボールクラス、AIクラスを継承したいと思っています。私はFormプロパティ(幅など)を設定するためにpremade Formクラスを使いたいと思います。

私はプレーヤークラスを作成しました。命名方法、ゲッター、セッターと一般的な考え方が正しいかどうかをお聞きしたいと思います。特定のビット(すべてではないにしても)が冗長であるか悪いと書かれているのではないか、私は「プロジェクト」全体を悪い前提にして、コード全体で同じ誤りを掛けたくない。

namespace Pong 
{ 
    public class Player 
    { 
     protected PictureBox PaddleBox { get; set; } 
     protected Size PlayerSize 
     { 
      get 
      { 
       return PlayerSize; 
      } 
      set 
      { 
       if (PlayerSize.Height > 0 && PlayerSize.Width > 0) 
       { 

        PlayerSize = new Size(value.Width, value.Height); 
        PaddleBox.Size = PlayerSize; 
       } 

      } 


     } 
     protected Point Location 
     { 
      get 
      { 
      return Location; 
     } 
     set 
     { 
      PaddleBox.Location = new Point(value.X, value.Y); 
     } 


    } 
    protected Color BackColor 
    { 
     get 
     { 
      return BackColor; 
     } 
     set 
     { 
      PaddleBox.BackColor = value; 

     } 
    } 
    public Player() 
    { 

     PaddleBox = new PictureBox(); 

    } 
} 

}

FORMクラスは今のところ、こののに沿って何かを探します、多分私はコンストラクタで、このような大きさ、位置や色などのパラメータを渡す必要がありますか?一番良いのは何ですか?

protected Point Location 
{ 
    get 
    { 
     return Location; // <--- this is a circular reference.. 
          //  meaning, it will recall this getter again. 

    } 
    set 
    { 
     PaddleBox.Location = new Point(value.X, value.Y); 
    } 
} 

使用この代わりに::

protected Point Location 
{ 
    get 
    { 
     return PaddleBox.Location; 
    } 
    set 
    { 
     PaddleBox.Location = value; 
    } 
} 

S

namespace Pong 
{ 
    public partial class Form1 : Form 
    { 
     public Timer gameTime; 
     const int screenWidth = 1248; 
     const int screenHeight = 720; 

     public Form1() 
     { 

      InitializeComponent(); 
      this.Height= screenHeight; 
      this.Width=screenWidth; 
      this.StartPosition=FormStartPosition.CenterScreen; 
      Player player = new Player(); 
      player.PaddleBox.Size = new Size(20, 50); 
      player.PaddleBox.Location = new Point(player.PaddleBox.Width/2, ClientSize.Height/2-player.PaddleBox.Height/2); 
      player.PaddleBox.BackColor = Color.Blue; 
      this.Controls.Add(player.PaddleBox); 
     gameTime = new Timer(); 
     gameTime.Enabled = true; 



    } 

    void gameTime_Tick(object sender, EventArgs e) 
    { 



    } 
    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 
} 

}

+0

プレイヤークラスは 'PlayerSize'プロパティを公開しますが、それをバイパスして直接ピクチャボックスのサイズを設定します' player.PaddleBox.Size =新しいサイズ(20,5 0); ' –

+0

PictureBoxはGUI要素です。私はそれをクラスの中に入れるだろうと確信していません。一般的にGUIには接続しないでください。 – nivs1978

答えて

0

あなたがここでの問題を得ましたここでprotected Color BackColor


と雨は一例であり、私はそれを(メモ帳を搭載し、あなたの現在のプログラミングスタイル()内)

namespace Pong 
{ 
    public partial class Form1 : Form 
    { 
     public Timer gameTime; 
     const int screenWidth = 1248; 
     const int screenHeight = 720; 

     public Form1() 
     { 

      InitializeComponent(); 
      this.Height= screenHeight; 
      this.Width=screenWidth; 
      this.StartPosition=FormStartPosition.CenterScreen; 

      Player player = new Player(this); 
      player.PlayerSize = new Size(20, 50); 
      player.Location = new Point(player.PaddleBox.Width/2, ClientSize.Height/2-player.PaddleBox.Height/2); // <-- the location is always the upperleft point. don't do this... 
      player.BackColor = Color.Blue; 

      gameTime = new Timer(); 
      gameTime.Enabled = true; 
     } 

     private void gameTime_Tick(object sender, EventArgs e) 
     { 

     } 
     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 
    } 

    public class Player 
    { 
     private PictureBox _paddleBox; 

     protected Size PlayerSize 
     { 
      get 
      { 
       return _paddleBox.Size; 
      } 
      set 
      { 
       if (PlayerSize.Height == 0 || PlayerSize.Width == 0) 
        throw new ArgumentException("Size must be greater than 0"); 

       _paddleBox.Size = value; 
      } 
     } 

     protected Point Location 
     { 
      get { return PaddleBox.Location; } 
      set { PaddleBox.Location = value; } 
     } 

     protected Color BackColor 
     { 
      get { return PaddleBox.BackColor; } 
      set { PaddleBox.BackColor = value; } 
     } 

     public Player(Form form) 
     { 
      PaddleBox = new PictureBox(); 
      form.Controls.Add(PaddleBox); 
     } 
    } 
} 

を実装する方法をあなたにピクチャを隔離するようにしてくださいプレーヤクラスでは、これはフォームとピクチャボックスの機能を分離します。

+0

あなたは、フォームクラスのプレーヤーに関して、すべての行を一行ずつ設定することは、プレーヤコンストラクタを使用して設定するよりも優れた解決策ですか? – Jam0131

+0

コンストラクタが呼び出された後にプロパティを変更することができ、デフォルト値が問題でない場合、コンストラクタに渡す必要はありません。この場合、新しいPoint()は問題になりません。 –

+0

保護されたPictureBox PaddleBoxを削除しました{get;セット; }、コンストラクタで呼び出すと、代わりに_paddleBoxが追加されました。どのように理解すればよいですか、どの部分がエラーですか? – Jam0131

関連する問題