类,用于控制图片框(c#窗体)的属性

本文关键字:窗体 属性 用于 控制 | 更新日期: 2023-09-27 18:13:07

我正在尝试写一些"干净"的代码…我想制作一款基于Forms的《Pong》游戏。我想把游戏很好地划分成类。

我想有一个球类,AI从球员类继承,我想使用预制的表单类设置主要的表单属性(宽度等)。

我做了一个这样的玩家类,我想问你是否命名,getter和setter的方法和一般的想法是正确的。是不是有些部分(如果不是全部的话)是多余的或者写得很糟糕,我不想把整个"项目"建立在错误的假设上,并在代码中重复同样的错误。
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类看起来沿着这个现在,也许我应该传递参数,如大小,位置和颜色的构造函数?什么是最好的?

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)
    {
    }
}

}

类,用于控制图片框(c#窗体)的属性

你有问题了:

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;
    }
}

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);
        }
    }   
}

你应该试着在你的播放器类中隔离图片框,这将分离表单和图片框的功能…