Qt-based games-battle against bacterial viruses

Source: Internet
Author: User

It seems that bird flu has appeared again recently. People may have experienced SARS, bird flu, and many such epidemics. People seem strange about this. Anyway, I am calm and don't worry about anything in the canteen. Well, I don't need to talk much about it. First, let's make a picture (it's not easy to write, please try it out ):

Main Interface of the bacterial virus war

There are four modes;

Single-user attack mode: Control the direction of red blood cell movement through a, S, D, W, and control the transmitting antibody through the left and right mouse keys to eliminate the flu virus;

Double Attack Mode:Through a, S, D, W and I, J, K, L, the movement direction of the two red blood cells is controlled, and then the two red blood cell transmitting antibodies are controlled through X, C, n, m, respectively;

Single and Double defense modes:Control the red blood cell to avoid viruses (hitting the virus will consume the life value );

This game has sound effects. The longest survival time in double mode is the winner.

Single-user attack mode: (artist OK)

Double Attack Mode:

In-game sound is implemented through the qsound class:

 

Public Member
  • Qsound(Const qstring & filename, qobject * parent = 0, const char * name = 0)
  • ~ Qsound()
  • Int
    Loops() Const
  • Int
    Loopsremaining() Const
  • Void
    Setloops(Int l)
  • Qstring
    Filename() Const
  • Bool
    Isfinished() Const
Public slot
  • Void
    Play()
  • Void
    Stop()
Static Public Member
  • Bool
    Isavailable()
  • Void
    Play(Const qstring & filename)
  • BoolAvailable()
For example, playing background music:

 

 

void BackGroundMusicThread::playBackGroundSound(const QString filename){    if( this->backgroudSound!=NULL )    {        this->backgroudSound->destroyed();        delete this->backgroudSound;        this->backgroudSound = new  QSound(filename);        this->backgroudSound->play();    }}


 

Playing background music, inheriting the qthread class:

 

class BackGroundMusicThread : public QThread{    Q_OBJECTpublic:    explicit BackGroundMusicThread(QObject *parent = 0);    ~BackGroundMusicThread();    void playBackGroundSound();    void playBackGroundSound(const QString fileName);    void setSoudnLoop(int lo);    void run();signals:    public slots:private:    QSound *backgroudSound;    QObject *parentWidget;};

In this way, the abstract function run () can play the desired sound at the end of the game.

 

 

void BackGroundMusicThread::run(){        qDebug() << "IN THREAD RUN...............................";        this->setSoudnLoop(2);  //   play this music 2 times        this->playBackGroundSound();        while(true)        {            if( ((Widget*)(this->parentWidget))->isGameOver() )            {                Resource *resource = new Resource();                QSound *gameoverSound = new QSound(resource->picLocation+"gameover.wav");                delete resource;                gameoverSound->play();                if( gameoverSound->isFinished())                {                    delete gameoverSound;                }            }            if( ((Widget*)(this->parentWidget))->isExit())            {                break;            }        }        qDebug() << "THREAD RUN STOP...............................";}

 

The buttons are implemented by inheriting the mouseeven In the Widget:

 

    void mouseMoveEvent(QMouseEvent *);    void mousePressEvent(QMouseEvent *);    void mouseReleaseEvent(QMouseEvent *);    void keyPressEvent(QKeyEvent *);    void keyReleaseEvent(QKeyEvent *);

Code:

 

 

void Widget::mouseMoveEvent(QMouseEvent *event){    if( Startgame )    {            world->setMousePos(event->x(),event->y());    }}void Widget::mousePressEvent(QMouseEvent *event){    qDebug() << "mouse event type :" << event->type();    if( this->world->getMode() == SINGLE_ATTACK_PLAYER)    {        if ( Startgame )        {            world->fireFlag = true;            this->world->setKeyP(event->button());        }    }}void Widget::mouseReleaseEvent(QMouseEvent *event){   qDebug() << "mouse event type :" << event->type();   if( this->world->getMode() == SINGLE_ATTACK_PLAYER)   {       if( Startgame )       {            world->fireFlag = false;            this->world->setKeyR(event->button());       }   }}

In setkeyr and setkeyp, obtain the key values of the key and the release key:

 

 

void World::setKeyP(int k){    switch(this->getMode())    {        case SINGLE_ATTACK_PLAYER:            //play one            if(k==Qt::Key_W)  //up                key['w'] =  true;            if(k==Qt::Key_S) //down                key['s'] =  true;            if(k==Qt::Key_A) //left                key['a'] =  true;            if(k==Qt::Key_D) //right                key['d'] =  true;            if(k==Qt::LeftButton)    //right click, bullet state 1            {                //play shot sound                this->playShotSound();                this->me->fireflag=true;                this->me->bulletstate=1;            }            else if(k==Qt::RightButton) // left click, bullet state 2            {                //play bomb sound                this->playBombSound();                this->me->fireflag=true;                this->me->bulletstate=2;            }            // bullets status            if(k== Qt::Key_1)                this->bulletState = 1;            if(k==Qt::Key_3)                this->bulletState = 3;            if(k==Qt::Key_4)                this->bulletState = 4;            break;        case SINGLE_DEFENCE_PLAYER:            //play one            if(k==Qt::Key_W)  //up                key['w'] =  true;            if(k==Qt::Key_S) //down                key['s'] =  true;            if(k==Qt::Key_A) //left                key['a'] =  true;            if(k==Qt::Key_D) //right                key['d'] =  true;            this->fireFlag = false;            break;        case DOUBLE_ATTACK_PLAYER:            //play one            if(k==Qt::Key_W)  //up                key['w'] =  true;            if(k==Qt::Key_S) //down                key['s'] =  true;            if(k==Qt::Key_A) //left                key['a'] =  true;            if(k==Qt::Key_D) //right                key['d'] =  true;            //player  tow            if(k==Qt::Key_I) //up                key['i'] =  true;            if(k==Qt::Key_K) //down                key['k'] =  true;            if(k==Qt::Key_J) //left                key['j'] =  true;            if(k==Qt::Key_L) //right                key['l'] =  true;            // fire keys            if(k==Qt::Key_N) //N key fire, bullet state 1            {                //play shot sound                this->playShotSound();                this->you->fireflag=true;                this->you->bulletstate=1;            }            if(k==Qt::Key_M) //M key fire, bullet state 2            {                //play bomb sound                this->playBombSound();                this->you->fireflag=true;                this->you->bulletstate=2;            }            if(k==Qt::Key_C) //C key fire, bullet state 1            {                //play shot sound                this->playShotSound();                this->me->fireflag=true;                this->me->bulletstate=1;            }            else if(k==Qt::Key_X) // X key fire, bullet state 2            {                //play bomb sound                this->playBombSound();                this->me->fireflag=true;                this->me->bulletstate=2;            }            // bullets status            if(k== Qt::Key_1)                this->bulletState = 1;            if(k==Qt::Key_3)                this->bulletState = 3;            if(k==Qt::Key_4)                this->bulletState = 4;            break;        case DOUBLE_DEFENCE_PLAYER:            //play one            if(k==Qt::Key_W)  //up                key['w'] =  true;            if(k==Qt::Key_S) //down                key['s'] =  true;            if(k==Qt::Key_A) //left                key['a'] =  true;            if(k==Qt::Key_D) //right                key['d'] =  true;            //player  tow            if(k==Qt::Key_I) //up                key['i'] =  true;            if(k==Qt::Key_K) //down                key['k'] =  true;            if(k==Qt::Key_J) //left                key['j'] =  true;            if(k==Qt::Key_L) //right                key['l'] =  true;            this->fireFlag = false;            break;        default:            break;    }    //  move Sound effect is bad , so wape it out, if you like it, add it    /*if( k==Qt::Key_W ||  k==Qt::Key_S  || k== Qt::Key_A  || k==Qt::Key_D        || k==Qt::Key_I ||  k==Qt::Key_J  || k== Qt::Key_K  || k==Qt::Key_L    )    {        this->playMoveSound();    }    */}void World::setKeyR(int k){    switch(this->getMode())    {        case SINGLE_ATTACK_PLAYER:            // player one            if(k==Qt::Key_W) //up                 key['w'] =  false;             if(k==Qt::Key_S) //down                 key['s'] =  false;             if(k==Qt::Key_A) //left                 key['a'] =  false;             if(k==Qt::Key_D) //right                 key['d'] =  false;             if(k==Qt::LeftButton)    //release left, bullet state 1             {                 this->me->fireflag=false;                 this->me->bulletstate=1;             }             else if(k==Qt::RightButton) //release right, bullet state 2             {                 this->me->fireflag=false;                 this->me->bulletstate=2;             }            break;        case SINGLE_DEFENCE_PLAYER:            // player one            if(k==Qt::Key_W) //up                 key['w'] =  false;             if(k==Qt::Key_S) //down                 key['s'] =  false;             if(k==Qt::Key_A) //left                 key['a'] =  false;             if(k==Qt::Key_D) //right                 key['d'] =  false;            break;        case DOUBLE_ATTACK_PLAYER:            // player one            if(k==Qt::Key_W) //up                 key['w'] =  false;             if(k==Qt::Key_S) //down                 key['s'] =  false;             if(k==Qt::Key_A) //left                 key['a'] =  false;             if(k==Qt::Key_D) //right                 key['d'] =  false;             //player  tow             if(k==Qt::Key_I)  //up                 key['i'] =  false;             if(k==Qt::Key_K)  // down                 key['k'] =  false;             if(k==Qt::Key_J)  //left                 key['j'] =  false;             if(k==Qt::Key_L)  //right                 key['l'] =  false;             if(k==Qt::Key_N)// bullet state 1             {                     this->you->fireflag=false;            //        this->you->bulletstate=1;             }                 //key['[']=true;             if(k==Qt::Key_M) //bullet state 2             {                 this->you->fireflag=false;            //       this->you->bulletstate=2;             }             if(k==Qt::Key_C)  //bullet state 1             {                 this->me->fireflag=false;                 this->me->bulletstate=1;             }             else if(k==Qt::Key_X) //bullet state 2             {                 this->me->fireflag=false;                 this->me->bulletstate=2;             }            break;        case DOUBLE_DEFENCE_PLAYER:            // player one            if(k==Qt::Key_W) //up                 key['w'] =  false;             if(k==Qt::Key_S) //down                 key['s'] =  false;             if(k==Qt::Key_A) //left                 key['a'] =  false;             if(k==Qt::Key_D) //right                 key['d'] =  false;             //player  tow             if(k==Qt::Key_I)  //up                 key['i'] =  false;             if(k==Qt::Key_K)  // down                 key['k'] =  false;             if(k==Qt::Key_J)  //left                 key['j'] =  false;             if(k==Qt::Key_L)  //right                 key['l'] =  false;            break;        default:            break;    }}

The Code is a little long and will not be pasted down. (You can download all the project files here:
Click to open the link)

 

The main idea of this game is:

Animation effect implementation: Inherits the paint method of the widget, and calculates the location of 'Influenza virus 'and 'Red cell' during each re-painting.

The effect of virus on red blood cells:Each influenza virus has its own motion direction attribute. During each re-painting, the angle between the motion direction of each influenza virus and the coordinate line between its own coordinates and the red blood cell is calculated, then, reduce the angle by a certain percentage, and then calculate the new coordinates of the influenza virus. After re-painting multiple times, the effect of influenza virus on red blood cells can be displayed.

In the project file QT game is compiled successfully in Windows (of course, it is also possible in Linux), the tool is: qt-windows-opensource-5.0.0-msvc2010_32-x86-offline (installed by default vs2010 ), below is a click to open the link. If you cannot download the Internet, there are also a lot of links, you need to compile it again after installation:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.