If(picturebox.Right == panel.Right) 不工作但 if(picturebox.Left == panel.Left) 工作
If(picturebox.Right == panel.Right) not working but if(picturebox.Left == panel.Left) works
我不确定为什么,但每当我处理碰撞时,似乎控件的顶部和左侧属性一切正常,但右侧和底部属性却不太顺利。我不确定是不是因为它们是只读属性,但请有人帮忙。
这是代码
操场(面板)class
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;
}
}
}
球(图片框)Class
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(控制和监视游戏中的所有内容 class 方法在每次计时器滴答时发生)Class
//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 范围。
我不确定为什么,但每当我处理碰撞时,似乎控件的顶部和左侧属性一切正常,但右侧和底部属性却不太顺利。我不确定是不是因为它们是只读属性,但请有人帮忙。
这是代码
操场(面板)class
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;
}
}
}
球(图片框)Class
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(控制和监视游戏中的所有内容 class 方法在每次计时器滴答时发生)Class
//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 范围。