QT-坦克大战游戏

发布时间:2023年12月19日


一、演示效果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、关键程序

#include "score.h"
Score::Score(){

health=30;
maxHealth=health;

    QLabel *label1 = new QLabel(this);
    label1->setFrameStyle(QFrame::Plain | QFrame::Box);
    label1->setStyleSheet("background-color:red;");
    label1->setText("Player1");
    label1->setAlignment(Qt::AlignTop | Qt::AlignCenter);
    label1->setFixedHeight(50);
    QFont font1= label1->font();
    font1.setPixelSize(20);
    label1->setFont(font1);

    label1->setGeometry(0,0,100,100 );
    resize(length*health/maxHealth, this->height(),1);

    QLabel *label2 = new QLabel(this);
    label2->setFrameStyle(QFrame::Plain | QFrame::Box);
    label2->setStyleSheet("background-color:red;");
    label2->setText("Player2");
    label2->setAlignment(Qt::AlignTop | Qt::AlignCenter);
    label2->setFixedHeight(50);
    QFont font2= label2->font();
    font2.setPixelSize(20);
    label2->setFont(font2);
    label2->setGeometry(SCORE_WIDTH - 100,0,100,100 );
    resize(length*health/maxHealth,this->height(),2);

    QPalette p(palette());
    p.setColor(QPalette::Window, QColor(255, 243, 240)); //bricks & box
    //p.setColor(QPalette::Window, QColor(224, 255, 224)); //forest

    this->setAutoFillBackground(true);
    this->setPalette(p);

    this->setFixedHeight(50);
}


void Score::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter1(this);
    QPainter painter2(this);

        painter1.save();
        painter1.setBrush(Qt::blue);
        if(!hp1.isNull())
            painter1.drawRect(hp1);
        painter1.restore();

        painter2.save();
        painter2.setBrush(Qt::green);
        if(!hp2.isNull())
            painter2.drawRect(hp2);
        painter2.restore();
}

void Score::slotDamage(int health,int ID){
   std::ofstream fout;
   if(ID==1){
        resize(length*health/maxHealth, this->height(),1);
        if(health<=0){
              std::ofstream fout;
                fout.open("Results.txt");
                fout << "Player2 WON!!!!";
                emit signalEndOfTheGame(2);
           }
   }
   else if(ID==2){
        resize(length*health/maxHealth,this->height(),2);
        if(health<=0){
                fout.open("Results.txt");
                fout << "Player1 WON!!!!";
                emit signalEndOfTheGame(1);
           }
   }
   fout.close();
}

void Score::resize(int width,int height,int ID){
    if (width < 0) width = 0;

    if(ID==1){
        hp1 = QRectF(100, 0, width,height);
        update();
    }else if(ID==2){
        hp2 = QRectF(SCORE_WIDTH - 100, 0, -width,height);
        update();
    }
}

三、下载链接

https://download.csdn.net/download/u013083044/88632686?spm=1001.2014.3001.5503

文章来源:https://blog.csdn.net/u013083044/article/details/135017740
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。