예제 #1
0
파일: widget.cpp 프로젝트: youngjeff/qt
bool Widget::isGameOver()
{
    for (int i=0;i < 4;i++)
    {
        for (int j = 0;j <3;j++)
        {
            if (array[i][j] == array[i][j+1] || array[i][j] == 0)
            {
                return false;
            }
        }
        if (array [i][3] == 0)
            return false;
    }
    for (int i = 0;i < 3;i++)
    {
        for (int j = 0;j < 4;j++)
        {
            if (array[i][j] == array[i+1][j])
            {
                return false;
            }
        }
    }
    QMessageBox::information(this,"game over","游戏结束!!");
    rebegin();
    return true;
}
예제 #2
0
void roadItem::rebegin()
{
    disconnect(animation, SIGNAL(finished()), this, SLOT(rebegin()));//取消关联
    animation->setDuration(ROAD_TIME * 2);//设置和道路2一样的移动方式
    animation->setStartValue(QPoint(pos().x() + WIDTH * 2 - 7, pos().y()));
    animation->setEndValue(pos());
    animation->setLoopCount(-1);//设置无限循环
    animation->start();
}
예제 #3
0
void roadItem::beginMove()
{
    animation = new QPropertyAnimation(this, "pos");

    if (id == 1)
    {
        animation->setDuration(ROAD_TIME);//道路1和道路2的初试位置相差一个视图的宽度,所以移动时间和距离不同
        animation->setStartValue(pos());
        animation->setEndValue(QPoint(pos().x()-WIDTH, pos().y()));
        animation->start();
        connect(animation, SIGNAL(finished()), this, SLOT(rebegin()));//第一次动画结束后重新定位和动画
    }
    else
    {
        animation->setDuration(ROAD_TIME * 2);
        animation->setStartValue(QPoint(pos().x() - 2, pos().y()));
        animation->setEndValue(QPoint(pos().x() - WIDTH * 2, pos().y()));
        animation->setLoopCount(-1);//道路2直接设置无限循环
        animation->start();
    }
}
예제 #4
0
파일: widget.cpp 프로젝트: youngjeff/qt
void Widget::keyPressEvent(QKeyEvent *event)
{
    // check whether game over
    if (isGameOver()){
        return ;                    }
    // check is it can be done
    memcpy(&arrayprey[0][0],&array[0][0],sizeof(array));
    //
    switch (event->key()) {

    case Qt::Key_A:
    {
       QMessageBox msgbox;
       msgbox.setWindowTitle("~~菜单~~");
       msgbox.setText("~~菜单~~  \nyes 重新开始 \n no是回到游戏");
       msgbox.setStandardButtons(QMessageBox::Yes);
       msgbox.addButton(QMessageBox::No);
       if(msgbox.exec() == QMessageBox::Yes)
       {
           rebegin();
           isok = true;
           break;
       }
       else{
            isok = true;
       }

         break;
    }

    case Qt::Key_Left:
        qDebug("Key_Left");
        for (int i = 0; i < 4; i++)
        {
            int j, k, key = 0;
            // move
            for (j = 0; j < 4; j++)
            {
                if (array[i][j] == 0)
                    continue;
                array[i][key++] = array[i][j];
            }
            // add
            for (j = 0, k = 0; j < key-1; j++, k++)
            {
                if (array[i][j] == array[i][j+1])
                {
                    array[i][k] = array[i][j] * 2;
                    score += array[i][k];
                    j++;
                }
                else
                    array[i][k] = array[i][j];
            }
            if (j == key - 1)
                array[i][k++] = array[i][j];
            // clear others
            for (j = k; j < 4; j++)
                array[i][j] = 0;
        }
        break;
    case Qt::Key_Right:
        qDebug("Key_Right");
        for (int i = 0; i < 4; i++)
        {
            int j, k, key = 0;
            // move
            for (j = 4-1; j >= 0; j--)
            {
                if (array[i][j] == 0)
                    continue;
                array[i][4-1-key++] = array[i][j];
            }
            // add
            for (j = 4-1, k = 4-1; j > 4-key; j--, k--)
            {
                if (array[i][j] == array[i][j-1])
                {
                    array[i][k] = array[i][j] * 2;
                    score += array[i][k];
                    j--;
                }
                else
                    array[i][k] = array[i][j];
            }
            if (j == 4-key)
                array[i][k--] = array[i][j];
            // clear others
            for (j = k; j >= 0; j--)
                array[i][j] = 0;
        }
        break;
    case Qt::Key_Up:
        qDebug("Key_Up");
        for (int j = 0; j < 4; j++)
        {
            int i, k, key = 0;
            // move
            for (i = 0; i < 4; i++)
            {
                if (array[i][j] == 0)
                    continue;
                array[key++][j] = array[i][j];
            }
            // add
            for (i = 0, k = 0; i < key-1; i++, k++)
            {
                if (array[i][j] == array[i+1][j])
                {
                    array[k][j] = array[i][j] * 2;
                    score += array[k][j];
                    i++;
                }
                else
                    array[k][j] = array[i][j];
            }
            if (i == key - 1)
                array[k++][j] = array[i][j];
            // clear others
            for (i = k; i < 4; i++)
                array[i][j] = 0;
        }
        break;
    case Qt::Key_Down:
        qDebug("Key_Down");
        for (int j = 0; j < 4; j++)
        {
            int i, k, key = 0;
            // move
            for (i = 4-1; i >= 0; i--)
            {
                if (array[i][j] == 0)
                    continue;
                array[4-1-key++][j] = array[i][j];
            }
            // add
            for (i = 4-1, k = 4-1; i > 4-key; i--, k--)
            {
                if (array[i][j] == array[i-1][j])
                {
                    array[k][j] = array[i][j] * 2;
                    score += array[k][j];
                    i--;
                }
                else
                    array[k][j] = array[i][j];
            }
            if (i == 4-key)
                array[k--][j] = array[i][j];
            // clear others
            for (i = k; i >= 0; i--)
                array[i][j] = 0;
        }
        break;
    default:
        break;
    }
    // if the array is game as prev,the down is not ok
    if(memcmp(&arrayprey[0][0],&array[0][0],sizeof(array)) == 0)
    {
        return ;
    }
    else{
        qDebug("score:%d",score);
    }
    // give a new rand number




    if(!isok){
    qsrand (QTime(0,0,0).secsTo(QTime::currentTime()));
    int i,j;
    do{
        i = qrand() % 4 ;
        j =qrand() % 4;
    }while(array[i][j] != 0 );
    array[i][j] = (qrand() % 2 +1 ) * 2;
    update(); // repaint the widget
    }
    isok = false;
}