检查动画是否位于winform屏幕的边缘

时间:2013-09-25 07:26:54

标签: c# winforms animation

我正在制作一个从窗户的一侧移动到另一侧的船只的动画,但是当船只接触到屏幕的边缘时我必须通知用户因为他赢了。任何人都知道如何做到这一点? 这就是我到目前为止所做的:

  private void playerTimer_Tick(object sender, EventArgs e)
    {
        for (int i = 0; i < 6; i++)
        {
            if (PlayersActive[i])
            {
                Players[i].Move(randomNumber.Next(3,10));
                 //HERE IS WHERE I SHOULD CHECK TO SEE IF A BIKE HAS WON
                 //If player location is at the edge of the screen

            }
        }
        this.Invalidate();
    }

移动船只的代码:

    private void GameForm_Paint(object sender, PaintEventArgs e)
    {

        Graphics g = e.Graphics;

        // Set variables used to store logic to default values.
        int playersStillRacing = 0;


        for (int i = 0; i < 6; i++)
        {
            // Check to see if the current player is still racing.
            if (playersRacing[i])
            {
                // Incremement playersStillRacing.
                playersStillRacing++;

                // Set the playerPictureBox associated with this player to the new location
                //  the player is located at.
                playerPictureBoxes[i].Image = Players[i].Image;
                playerPictureBoxes[i].Left = Players[i].X;
                playerPictureBoxes[i].Top = Players[i].Y;


                g.DrawImage(playerPictureBoxes[i].Image, Players[i].X, Players[i].Y);

            }
         }

     }

1 个答案:

答案 0 :(得分:3)

这可以解决这个问题吗?我根据你给我的最新信息做了一些编辑

private void playerTimer_Tick(object sender, EventArgs e)
{
    for (int i = 0; i < 6; i++)
    {
        if (PlayersActive[i])
        {
            Players[i].Move(randomNumber.Next(3,10));

            // EDIT BELOW in the if statement!
            // if the edge of the picture touches the end of the screen.
            if (Players[i].X + Players[i].Image.Width >= this.Width)
                MessageBox.Show("Congrats! Player" + i.ToString() + "won!");

            //Players[i].X is the X cordinates(The length) from the left side of the winform.
            //Players[i].Image.Width is the width of the picture ofcourse :D.
            // if X cordinates + picture width is greater than or equal to screen width. YOU WON :D
        }
    }
    this.Invalidate();
}

或者你想要别的吗?