PONG Collision需要帮助

时间:2017-11-26 19:14:18

标签: c# collision pong

我必须为学校作业做乒乓球。一切都很顺利 直到我来到碰撞部分。有人可以帮助我吗?

我是在视觉工作室c#中做的,我是初学者,所以我不太了解。我尝试了很多不同的解决方案,但似乎没有一个解决方案。

public override void GameStart()
{

}

public override void GameEnd()
{

}

public override void Update()
{
    float DeltaTime = GAME_ENGINE.GetDeltaTime();

    //Frame Rate Unlock
    if (GAME_ENGINE.GetKeyDown(Key.V))
    {
        bool isLocked = GAME_ENGINE.GetVSync();
        GAME_ENGINE.SetVSync(!isLocked);
    }

    //Player 1 Movement
    if (GAME_ENGINE.GetKey(Key.W) && m_PlayerY1 != 0)
    {
        m_PlayerY1 -= 250 * DeltaTime;
    }

    if (GAME_ENGINE.GetKey(Key.S) && m_PlayerY1 != 300)
    {
        m_PlayerY1 += 250 * DeltaTime;
    }

    if (m_PlayerY1 < 0)
    {
        m_PlayerY1 = 0;
    }

    if (m_PlayerY1 > 350)
    {
        m_PlayerY1 = 350;
    }

    //Player 2
    if (GAME_ENGINE.GetKey(Key.Up) && m_PlayerY2 != 0)
    {
        m_PlayerY2 -= 250 * DeltaTime;
    }

    if (GAME_ENGINE.GetKey(Key.Down) && m_PlayerY2 != 300)
    {
        m_PlayerY2 += 250 * DeltaTime;
    }

    if (m_PlayerY2 < 0)
    {
        m_PlayerY2 = 0;
    }

    if (m_PlayerY2 > 350)
    {
        m_PlayerY2 = 350;
    }

    //Ball Stuff
    Random RnD = new Random();
    m_XBallSpeed = RnD.Next(2, 3);
    m_YBallSpeed = RnD.Next(2, 3);


    m_XBall += m_XBallSpeed;
    if (m_XBall < 0)
    {
        m_XBallSpeed = -m_XBallSpeed;
    }
    else if (m_XBall + m_BallWidth > GAME_ENGINE.GetScreenWidth())
    {
        m_XBallSpeed = -m_XBallSpeed;
    }

    m_YBall += m_YBallSpeed;
    if (m_YBall < 0)
    {
        m_YBallSpeed = -m_YBallSpeed;
    }
    else if (m_YBall + m_BallHeight > GAME_ENGINE.GetScreenHeight())
    {
        m_YBallSpeed = -m_YBallSpeed;
    }

    if (m_XBall >= m_PlayerHeight1 && m_YBall >= m_PlayerHeight2)
    {
        m_XBallSpeed += 1;
        m_YBallSpeed += 1;
        m_XBallSpeed = -m_YBallSpeed;
    }

    if (m_YBall >= m_PlayerWidth1 && m_YBall >= m_PlayerWidth2)
    {
        m_XBallSpeed += 1;
        m_YBallSpeed += 1;
        m_XBallSpeed = -m_YBallSpeed;
    }
}

public override void Paint()
{
    //Player 1 and 2 and Ball
    GAME_ENGINE.SetColor(255, 255, 255);
    GAME_ENGINE.FillRectangle(m_PlayerX1, m_PlayerY1, m_PlayerWidth1, m_PlayerHeight1);
    GAME_ENGINE.FillRectangle(m_PlayerX2, m_PlayerY2, m_PlayerWidth2, m_PlayerHeight2);
    GAME_ENGINE.FillRoundedRectangle(m_XBall, m_YBall, m_BallWidth, m_BallHeight, 50, 50);
}

0 个答案:

没有答案