2016-04-15 100 views
2

这个游戏还没有完成,我创造了球击中板的部分,但是在比赛中有问题,那就是板上的球。为了更好的理解和支持,我已经在下面附上了一张图片,请观看它,并让我知道你对这个bug的看法。卡在板上(乒乓球比赛)

public partial class PingPong : Form 
{ 
    public PingPong() 
    { 
     InitializeComponent(); 
     timer1.Enabled = true; 
     timer1.Interval = 30; 
    } 


    int mx = 5; 
    int my = 5; 
    private void timer1_Tick(object sender, EventArgs e) 
    { 

     if (ball.Bounds.IntersectsWith(boardCenter.Bounds)) 
     { 
      mx = mx * -1; 
      my = my * 1; 
     } 
     else if (ball.Location.Y >= this.ClientSize.Height - ball.Height) 
     { 
      mx = mx * 1; 
      my = my * -1; 
     } 
     else if (ball.Location.Y <= 0) 
     { 
      mx = mx * 1; 
      my = my * -1; 
     } 

     else if (ball.Location.X >= this.ClientSize.Width - ball.Width) 
     { 
      mx = mx * -1; 
      my = my * 1; 
     } 


     ball.Location = new Point(ball.Location.X + mx, ball.Location.Y + my); 

    } 

    private void PingPong_KeyDown(object sender, KeyEventArgs e) 
    { 

     if (e.KeyCode == Keys.Up) 
     { 
      board.Location = new Point(board.Location.X, board.Location.Y - 4); 

     } 
     else if (e.KeyCode == Keys.Down) 
     { 
      board.Location = new Point(board.Location.X, board.Location.Y + 4); 
     } 

    } 

} 

enter image description here

+0

为了更好的理解和支持我已经把视频https://youtu.be/WSZxw42qR-E –

回答

0

这似乎对我(对你提供的视频基地),你的逻辑实际上是正确的,问题看起来是那个球是打从后面,因此碰撞迅速董事会计算在窗口的左侧和电路板的背面之间。

您应该修改代码,以便与窗口左侧的碰撞在与电路板相同的X位置进行计算。例如,如果棋盘X的位置固定在距离屏幕左侧20的位置,则球与球门左侧的碰撞为20,这样球就永远不会到达后面董事会。 (同样的原则适用于屏幕右侧和第二块电路板)。