2016-04-15 7 views
2

このゲームはまだ完成していませんが、ボールがボードに当たる部分を作成しましたが、プレーに問題があり、ボールがボードにこびりついています。より良い理解とunderstaningのために、私は下に画像を添付しました、それを見て、このバグをあなたのテイクを知っている。ボードにボールを詰め込んだ(ピンポンゲーム)

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にすると、ボールは決してボールの後部に到達できませんボード。 (同じ原理が画面の右側と2番目のボードに適用されます)。

関連する問題