QT下实现贪吃蛇游戏前期准备

通过查找资料学习对应功能与模块

1.按键的处理

{

    switch(e->key())

    {

    case Qt::Key_Up:if(m_setDiretion!=2)m_setDiretion=1;  break;

    case Qt::Key_W:if(m_setDiretion!=2)m_setDiretion=1;  break;

    case Qt::Key_Down:if(m_setDiretion!=1)m_setDiretion=2;  break;

    case Qt::Key_S:if(m_setDiretion!=1)m_setDiretion=2;  break;

    case Qt::Key_Left:if(m_setDiretion!=4)m_setDiretion=3;  break;

    case Qt::Key_A:if(m_setDiretion!=4)m_setDiretion=3;  break;

    case Qt::Key_Right:if(m_setDiretion!=3)m_setDiretion=4;  break;

    case Qt::Key_D:if(m_setDiretion!=3)m_setDiretion=4;  break;

    default:                   break;

    }

}

2.设置随机数

qsrand(time(NULL));

3.计时

qsrand(time(NULL));

void Gamewidget::M_startPush()

{

    Timer->start(800);

    disconnect(StartPush,SIGNAL(clicked(bool)),this,SLOT(M_startPush()));

    StartPush->setIcon(QIcon(":/Picture/img/Pause.png"));

    connect(StartPush,SIGNAL(clicked(bool)),this,SLOT(M_pausePush()));

}

4.游戏功能

void Gamewidget::M_timeout()

{

    if(snake[0][0]==foodx&&snake[0][1]==foody)

    {

        foodx=(rand()%20)*30;

        foody=(rand()%15)*30;

        bodycount++;

        Difficulty=bodycount/5+1;

        Score=Score+Difficulty;

        ScoreLabelNumber->setText(QString::number(Score)+"分");

        LevelLabelNumber->setText(QString::number(Difficulty)+"级");

        Timer->start(800-Difficulty>0? 800-Difficulty*80:80/(Difficulty-8));

    }


你可能感兴趣的:(QT下实现贪吃蛇游戏前期准备)