游戏编程--雪花效果

时间:2023-02-08 20:16:36
void CMainPage::processParticle() {
    VFLOAT rad = 0;
    srand( (unsigned)time( NULL ) );          //初始化随机数
    for(VINT32 i = 0; i < 1; ++i) { //1 is the snow num
        VFLOAT k = rand() % PI; //rand a radius that the snow move, what follows next the function "cos"

        VINT32 iSpeed =  rand()%2 + 1;
        VFLOAT fX = m_pParticle[i]->getX().toFloat() - cos(k);
        m_pParticle[i]->setX(fX);

        VFLOAT fY = m_pParticle[i]->getY().toFloat() + iSpeed; //iSpeed is the rand speed
        m_pParticle[i]->setY(fY);
        if (fY >= 480) {
            m_pParticle[i]->setY(-5);
        }

        if ((fX >= 242) /*|| (fY <= 0)*/) {
            fX = -10 + rand()%450;
            m_pParticle[i]->setX(fX);
            m_pParticle[i]->setY(-5);
        }
    }
}