gpt4 book ai didi

c# - 如果(picturebox.Right == panel.Right)不工作但如果(picturebox.Left == panel.Left)工作

转载 作者:太空宇宙 更新时间:2023-11-03 15:28:59 28 4
gpt4 key购买 nike

我不确定为什么,但每当我处理碰撞时,似乎控件的顶部和左侧属性一切正常,但右侧和底部属性却不太顺利。我不确定是不是因为它们是只读属性,但请有人帮忙。

代码如下

Playground (面板)类

    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# - 如果(picturebox.Right == panel.Right)不工作但如果(picturebox.Left == panel.Left)工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34301040/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com