如果(picturebox.Right==panel.Right)不起作用,但如果(picture box.Left==
本文关键字:如果 Right picture box Left picturebox panel 不起作用 | 更新日期: 2023-09-27 18:27:06
我不知道为什么,但每当我处理冲突时,控件的顶部和左侧属性似乎一切都很好,但右侧和底部属性则不然。我不确定是不是因为它们是只读属性,但请有人帮忙。
代码在这里
操场(小组)
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!!!");
}
我们在这个代码中所做的是,我们正在检查操场和它碰撞的球的+-7范围。