Set properties of an object to be relative to another property of the same object

时间:2015-10-31 00:01:51

标签: c#

I have this method that detects a collision between the ball and the left paddle in the classic game "Pong". I have made variables for the different parts of the ball and the paddle, in order to make my collision detection method easier to understand. Here is the method (and it does work). public bool DetectBallPaddle1Collision() { var ballBottom = _ball.Y + Ball.Width; var ballTop = _ball.Y; var ballLeft = _ball.X; var ballRight = _ball.X + Ball.Width; var paddle1Bottom = _paddle1.Y + Paddle.Height; var paddle1Top = _paddle1.Y; var paddle1Left = PlayerPaddle.X; var paddle1Right = PlayerPaddle.X + Paddle.Width; if (ballTop < paddle1Bottom && ballBottom > paddle1Top && ballLeft < paddle1Right) { return true; } else { return false; } } I would now like to refactor this, to have the variables for the different parts of the ball, in the ball class, like so: namespace Pong.Core.Models { public abstract class Ball : IBall { public int Y { get; set; } public int X { get; set; } public int VX { get; set; } public int VY { get; set; } public static int Width; public static int Speed; public int Bottom { get; set; } public int Top { get; set; } public int Left { get; set; } public int Right { get; set; } public Ball() { this.setDirection ("left"); Bottom = Y + Ball.Width; Top = Y; Left = X; Right = X + Ball.Width; } Which enables me to change the ball/paddle1 collision detection method to this: public bool DetectBallPaddle1Collision() { var paddle1Bottom = _paddle1.Y + Paddle.Height; var paddle1Top = _paddle1.Y; var paddle1Left = PlayerPaddle.X; var paddle1Right = PlayerPaddle.X + Paddle.Width; if (_ball.Top < paddle1Bottom && _ball.Bottom > paddle1Top && _ball.Left < paddle1Right) { return true; } else { return false; } } Theoretically this should work, but it doesn't, the ball now goes through the paddle. This should work because a property is a method, and is supposed to keep updating if it is relative to another property, correct? This is why I made the ball.Bottom, .Top, .Left and .Right, properties with getters and setters rather than fields which would not keep updating.

2 个答案:

答案 0 :(得分:1)

When you specify properties with { get; set; } what happens is the compiler is creating a private field and keeps the property value in it. Setting the Top property value to Y in the constructor will put that initial value of Y into that private field and that value will never get updated as Y changes. Instead, implement your properties like this: (This will work assuming that X and Y are being updated during the game) public class Ball : IBall { public int Y { get; set; } public int X { get; set; } public int VX { get; set; } public int VY { get; set; } public static int Width; public static int Speed; public int Top { get { return Y; } } public int Left { get { return X; } } public int Right { get { return X + Ball.Width; } } public int Bottom { get { return Y + Ball.Width; } } }

答案 1 :(得分:0)

因此,您拥有独立的属性和依赖属性。

独立属性是指未计算其值并且在系统中显示degrees of freedom的属性。

另一方面,依赖属性是从其他独立(和依赖)属性计算其值的属性。

我将假设XYWidth以及独立属性。 BottomTopLeftRight是相关属性。

以下是您定义Top

的示例

我会假设XY显示球的中心。

public int Top
{
    get
    {
        return Y - (Height / 2);
    }
    set
    {
        Y = value + (Height / 2);
    }
}

因此,当消费者要求Top属性的值时,我们会从YHeight计算出来。当消费者设置此属性的值时,我们会相应地更改Y属性。

请注意,我假设点(0,0)位于屏幕的左上角。如果情况并非如此,则需要改变方程式。