我不知道为什么,但是无论何时我与碰撞打交道,似乎控件的top和left属性都运行良好,而right和bottom属性却没那么好。我不确定是否是因为它们是只读属性,但是请有人帮忙。

继承人的代码

游乐场(面板)类

    namespace final_pong_game_phase_3
{
class PlayGround
{
    public Panel Panel;
    Size PlayGroundSize;
    public int Top;
    public int Bottom;
    public int Right;
    public int Left;
    public PlayGround(Panel panel,Size size, Point location)
    {
        this.Panel = panel;
        this.PlayGroundSize = size;
        this.Panel.Size = this.PlayGroundSize;
        this.Panel.BackColor = Color.Black;
        this.Panel.Location = location;
        this.Panel.SendToBack();

        this.Top = panel.Top;
        this.Bottom = panel.Bottom;
        this.Right = panel.Right;
        this.Left= panel.Left;
    }



}
}


球(画框)类

    namespace final_pong_game_phase_3
  {
class Ball
{
    Size BallSize;
    Point Location;
    int TopSpeedInterval;
    int LeftSpeedInterval;
    PictureBox BallGraphic;
    public int Top;
    public int Left;
    public int Bottom;
    public int Right;

    public Ball(Point location, int topspeedinterval,int leftspeedinterval, Size ballsize, PictureBox ballgraphic)
    {
        this.Location = location;
        this.TopSpeedInterval = topspeedinterval;
        this.LeftSpeedInterval = leftspeedinterval;
        this.BallSize = ballsize;
        this.BallGraphic = ballgraphic;

        this.BallGraphic.Location = this.Location;
        this.BallGraphic.BackColor = Color.White;
        this.BallGraphic.Size = this.BallSize;
        this.BallGraphic.BringToFront();

        this.Top = BallGraphic.Top;
        this.Left = BallGraphic.Left;
        this.Bottom = ballgraphic.Bottom;

    }

    public void start()
    {
        Top -= TopSpeedInterval;
        Left -= LeftSpeedInterval;

        BallGraphic.Top = Top;
        BallGraphic.Left = Left;
        Bottom = BallGraphic.Bottom;
        Right = BallGraphic.Right;


    }

    public void SwitchTopDirection()
    {
        TopSpeedInterval *=-1;

    }

    public void SwitchLeftDirection()
    {
        LeftSpeedInterval *= -1;
    }

    public void Hit()
    {
        TopSpeedInterval+= 5;
        LeftSpeedInterval+= 5;
    }
}
 }


GameWorld(控制和监视游戏,每次计时器计时时,该类方法中的所有事件都会发生)

    //ball to wall collision
        if (playground.Top == ball.Top)
        {
            ball.SwitchTopDirection();
            Console.Beep(1000, 100);
        }

        if (playground.Left == ball.Left)
        {
            Console.Beep(1500, 700);
            MessageBox.Show("Player 2 Wins!!!");

        }

        if (playground.Right == ball.Right)
        {
            MessageBox.Show("Player 2 Wins!!!");
        }

        if (ball.Bottom >= playground.Bottom)
        {
            ball.SwitchTopDirection();
            Console.Beep(1000, 100);
        }

最佳答案

这可能会帮你

if (Math.Abs(playground.Right - ball.Right)<=7)
{
    MessageBox.Show("Player 2 Wins!!!");
}


我们在此代码中所做的是,我们正在检查Playground和碰撞的Ball的+ -7范围。

关于c# - if(picturebox.Right == panel.Right)不起作用,但是if(picturebox.Left == panel.Left)起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34301040/

10-13 06:26